-
Notifications
You must be signed in to change notification settings - Fork 14
/
watch.ts
45 lines (38 loc) · 1.36 KB
/
watch.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
import { Operation, Task, main, on, sleep, spawn } from 'effection';
import { exec, daemon, Process, StdIO } from '@effection/process';
import { watch } from 'chokidar';
main(function* (scope: Task) {
if (!process.env.PORT) process.env.PORT = '4000';
let watcher = watch(['../server/src/**/*.ts', './src/**/*.ts'], { ignoreInitial: true, ignored: 'dist' });
try {
let process: Task = yield scope.spawn(buildAndRun);
yield on(watcher, 'all').forEach(function*() {
yield process.halt();
process = yield scope.spawn(function*() {
yield sleep(10);
console.log('rebuilding.....');
yield buildAndRun;
});
});
} finally {
watcher.close();
}
});
function* executeAndOut(command: string): Operation<void> {
let { stdout, stderr, expect }: Process = yield exec(`npm run ${command}`);
yield spawn(stdout.forEach(line => { process.stdout.write(line); }))
yield spawn(stderr.forEach(line => { process.stderr.write(line); }))
yield expect();
}
function* buildAndRun() {
try {
yield executeAndOut('clean');
yield executeAndOut('prepack');
let server: StdIO = yield daemon('node dist/start.js');
yield spawn(server.stdout.forEach(line => { process.stdout.write(line); }))
yield spawn(server.stderr.forEach(line => { process.stderr.write(line); }))
} catch (err) {
console.error(err);
}
yield;
}