-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
54 lines (50 loc) · 1.36 KB
/
main.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
import { each, main, scoped, spawn } from "effection";
import { watch } from "./watch.ts";
import { parser } from "zod-opts";
import { z } from "zod";
import process from "node:process";
import denoJson from "./deno.json" with { type: "json" };
await main(function* (args) {
let options = parser()
.name("watch")
.description(
"run a command, and restart it every time a source file in a directory changes",
)
.options({
command: {
type: z.string(),
alias: "c",
description: "the command to run",
},
})
.version(denoJson.version)
.parse(args);
let { command } = options;
let watcher = watch({
path: process.cwd(),
cmd: command,
});
for (let start of yield* each(watcher)) {
process.stdout.write(`${command}\n`);
yield* scoped(function* () {
if (start.ok) {
let proc = start.value;
yield* spawn(function* () {
for (let chunk of yield* each(proc.stdout)) {
process.stdout.write(chunk);
yield* each.next();
}
});
yield* spawn(function* () {
for (let chunk of yield* each(proc.stderr)) {
process.stderr.write(chunk);
yield* each.next();
}
});
} else {
console.error(`failed to start: ${start.error}`);
}
yield* each.next();
});
}
});