-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathrunner.ts
141 lines (120 loc) · 3.31 KB
/
runner.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as cp from "child_process";
import {field, Logger, logger, time} from "@coder/logger";
export interface CommandResult {
readonly exitCode: number;
readonly stdout: string;
readonly stderr: string;
}
const execute = (command: string, args: string[] = [], options: cp.SpawnOptions, logger: Logger): Promise<CommandResult> => {
let resolve: (result: CommandResult) => void;
const prom = new Promise<CommandResult>((res): void => {
resolve = res;
});
const stdout: string[] = [];
const stderr: string[] = [];
const complete = (exitCode: number): void => {
resolve({
stderr: stderr.join(""),
stdout: stdout.join(""),
exitCode,
});
};
logger.info(`Executing '${command} ${JSON.stringify(args)}'`, field("options", options));
const proc = cp.spawn(command, args.length > 0 ? args : [], options);
proc.on("close", (code) => {
complete(code);
});
proc.on("exit", (code) => {
complete(code!);
});
proc.stdout.on("data", (d) => {
stdout.push(d.toString());
logger.debug("stdio", field("stdout", d.toString()));
});
proc.stderr.on("data", (d) => {
stderr.push(d.toString());
logger.debug("stdio", field("stderr", d.toString()));
});
return prom;
};
export type TaskFunction = (runner: Runner) => void | Promise<void>;
export interface Runner {
cwd: string;
execute(command: string, args?: string[], env?: object): Promise<CommandResult>;
}
export interface Task {
readonly name: string;
readonly func: TaskFunction;
}
const tasks = new Map<string, Task>();
const activated = new Map<string, Promise<void>>();
export const register = (name: string, func: TaskFunction): () => void | Promise<void> => {
if (tasks.has(name)) {
throw new Error(`Task "${name}" already registered`);
}
tasks.set(name, {
name,
func,
});
return (): void | Promise<void> => {
return run(name);
};
};
export const run = (name: string = process.argv[2]): void | Promise<void> => {
const task = tasks.get(name);
if (!task) {
logger.error("Task not found.", field("name", name), field("available", Array.from(tasks.keys())));
return process.exit(1);
}
if (activated.has(name)) {
return activated.get(name);
}
let cwd: string = process.cwd();
const log = logger.named(name);
const timer = time(Number.MAX_SAFE_INTEGER);
let outputTimer: NodeJS.Timer | undefined;
log.info("Starting...");
const prom = task.func({
set cwd(path: string) {
cwd = path;
},
execute(command: string, args: string[] = [], env?: object): Promise<CommandResult> {
const prom = execute(command, args, {
cwd,
env: env as NodeJS.ProcessEnv,
}, log);
return prom.then((result: CommandResult) => {
if (result.exitCode != 0) {
log.error("failed",
field("exitCode", result.exitCode),
field("stdout", result.stdout),
field("stderr", result.stderr)
);
}
return result;
});
},
});
if (prom) {
activated.set(name, prom);
const doOutput = (): void => {
outputTimer = setTimeout(() => {
log.info("Still running...");
doOutput();
}, 60 * 1000 * 5);
};
doOutput();
prom.then(() => {
if (outputTimer) {
clearTimeout(outputTimer);
}
log.info("Completed!", field("time", timer));
}).catch((ex) => {
activated.delete(name);
log.error(`Failed: ${ex.message}`);
log.error(`Stack: ${ex.stack}`);
return process.exit(1);
});
}
return prom;
};