-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunnable.ts
72 lines (57 loc) · 1.5 KB
/
runnable.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
import { Process, WatchOptions } from "./deps.ts";
import { render } from "./render.ts";
export type Runnable = {
type: string;
name: string;
run(): Promise<any>;
};
export type Task = Runnable & {
cmd: string;
watch(opts?: Partial<WatchOptions>): any;
};
const cache = new Map<string, Runnable>();
function task(name: string, cmd: string): Task {
const proc = new Process({ name, cmd });
const run = async () => runProcess(proc);
const watch = (opts?: WatchOptions) => {
proc.watch(opts);
return { run };
};
const runnable = { type: "task", name, cmd, run, watch };
cache.set(name, runnable);
return runnable;
}
function series(name: string, runnables: (Runnable | string)[]) {
const run = async () => {
for (const r of runnables) {
await getRunnable(r).run();
}
};
const runnable = { type: "series", name, run };
cache.set(name, runnable);
return runnable;
}
function parallel(name: string, runnables: (Runnable | string)[]) {
const run = async () => {
return Promise.all(runnables.map(r => {
return getRunnable(r).run();
}));
};
const runnable = { type: "parallel", name, run };
cache.set(name, runnable);
return runnable;
}
async function runProcess(process: Process) {
await process
.on("stdout", render)
.on("stderr", render)
.start()
.complete();
}
function getRunnable(r: Runnable | string): Runnable {
return (
cache.get(r as string) ||
r as Runnable
);
}
export { task, series, parallel, cache };