-
Notifications
You must be signed in to change notification settings - Fork 38
/
run.ts
89 lines (78 loc) · 2.21 KB
/
run.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Command, flags } from "@oclif/command";
import * as path from "path";
import * as childProcess from "child_process";
import { cli } from "cli-ux";
import { Env, getEnv } from "../../lib/env";
export const task = async (fn: (env: Env) => Promise<void>) => {
try {
await fn(
getEnv(
process.env.configPath || "",
process.env.keysPath || "",
process.env.refsPath || "",
process.env.network || ""
)
);
} catch (err) {
if (err instanceof Error) {
cli.error(err);
}
if (typeof err === "string") {
cli.error(err);
}
cli.error(`${err}`);
}
};
export default class Run extends Command {
static description = "run predefined task";
static flags = {
network: flags.string({ default: "localterra" }),
"config-path": flags.string({ default: "config.terrain.json" }),
"refs-path": flags.string({ default: "refs.terrain.json" }),
"keys-path": flags.string({ default: "keys.terrain.js" }),
};
static args = [{ name: "task" }];
async run() {
const { args, flags } = this.parse(Run);
const fromCwd = (p: string) => path.join(process.cwd(), p);
runScript(
fromCwd(`tasks/${args.task}.js`),
{
configPath: fromCwd(flags["config-path"]),
keysPath: fromCwd(flags["keys-path"]),
refsPath: fromCwd(flags["refs-path"]),
network: flags.network,
},
(err) => {
if (err) throw err;
}
);
}
}
function runScript(
scriptPath: string,
env: {
configPath: string;
keysPath: string;
refsPath: string;
network: string;
},
callback: (err?: Error) => void
) {
// keep track of whether callback has been invoked to prevent multiple invocations
let invoked = false;
const cProcess = childProcess.fork(scriptPath, { env });
// listen for errors as they may prevent the exit event from firing
cProcess.on("error", function (err) {
if (invoked) return;
invoked = true;
callback(err);
});
// execute the callback once the process has finished running
cProcess.on("exit", function (code) {
if (invoked) return;
invoked = true;
const err = code === 0 ? undefined : new Error("exit code " + code);
callback(err);
});
}