forked from ubiquity/ubiquibot-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-keys.ts
47 lines (39 loc) · 1.52 KB
/
deploy-keys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import fs from "fs";
import { Readable } from "stream";
export const readEnvironmentFile = async () => {
try {
const environmentData = JSON.parse(fs.readFileSync("environment.json", "utf8"));
const keys = Object.keys(environmentData);
for (const key of keys) {
const value = environmentData[key];
const command = `echo '${value}' | wrangler secret put ${key.toUpperCase()}`;
console.log(`Running command: ${command}`);
const secretCommand = spawn(command, { shell: true, stdio: "pipe" });
// Promisify child process events
const onData = waitForEvent(secretCommand.stdout, "data");
const onError = waitForEvent(secretCommand.stderr, "data");
const onClose = waitForEvent(secretCommand, "close");
// Wait for stdout, stderr, and close events
await Promise.all([onData, onError, onClose]);
// Continue the loop after each command is finished
console.log(`Command for "${key}" completed.`);
}
} catch (err) {
console.error("Error reading or executing commands:", err);
}
};
export const waitForEvent = (emitter: Readable | ChildProcessWithoutNullStreams, event: string) => {
return new Promise((resolve) => {
emitter.on(event, (data) => {
console.log(data.toString());
});
emitter.once("error", (error) => {
console.error(error.toString());
});
emitter.once("close", (code: number) => {
resolve(code);
});
});
};
void readEnvironmentFile();