/
watch.ts
153 lines (144 loc) · 4.49 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
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
142
143
144
145
146
147
148
149
150
151
152
153
import { NullaryFn, UnaryFn, Empty } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import chokidar from 'chokidar';
import debounce from 'debounce';
import { Task } from '../../definitions';
import { stringifyError } from '../../helpers/stringify';
import { run } from '../../utils/run';
import { log } from '../stdio/log';
import { clear } from '../stdio/clear';
import { create } from '../creation/create';
import { series } from '../aggregate/series';
export interface WatchOptions {
/** Parse globs in paths */
glob?: boolean;
/** Runs the task once when ready to wait for changes */
prime?: boolean;
/** Finalizes the watch effort if a given task fails */
fail?: boolean;
/** Clear stdout before tasks execution */
clear?: boolean;
/** Paths to include */
include?: string | string[];
/** Paths to exclude */
exclude?: string | string[];
/** Doesn't cancel tasks in execution when a new task runs */
parallel?: boolean;
/** Avoids rapid task restarts by debouncing by a set number of ms */
debounce?: number;
/** Limits how many subdirectories will be traversed */
depth?: number;
/** If set, it will use polling every given ms interval */
poll?: number;
/** Whether to follow symlinks */
symlinks?: boolean;
}
/**
* Watches `paths` for changes and runs a given
* `task` for every change event.
* @returns Task
*/
export function watch(options: WatchOptions | Empty, task: Task): Task.Async {
return create((ctx) => {
const opts = into(
{
glob: false,
prime: false,
fail: false,
clear: false,
include: ['./'],
exclude: ['node_modules'],
parallel: false,
debounce: -1,
depth: -1,
poll: -1,
symlinks: false
},
(defaults) => shallow(defaults, options || undefined),
({ include, exclude, ...options }) => ({
...options,
include: Array.isArray(include) ? include : [include],
exclude: Array.isArray(exclude) ? exclude : [exclude]
})
);
return series(log('debug', 'Watch:', opts.include), async () => {
const watcher = chokidar.watch(opts.include, {
persistent: true,
ignored: opts.exclude,
ignorePermissionErrors: false,
ignoreInitial: true,
cwd: ctx.cwd,
disableGlobbing: !opts.glob,
depth: opts.depth >= 0 ? opts.depth : undefined,
usePolling: opts.poll >= 0,
interval: opts.poll,
binaryInterval: opts.poll,
followSymlinks: opts.symlinks
});
const cbs: NullaryFn[] = [];
function cancel(): void {
while (cbs.length) {
const cb = cbs.shift();
if (cb) cb();
}
}
let i = -1;
let current: Promise<void> = Promise.resolve();
const onEvent = debounce(
(onError: UnaryFn<Error>): void => {
if (!opts.parallel) cancel();
const after = opts.parallel ? Promise.resolve() : current;
current = after
.then(() => {
i += 1;
return run(
{
...ctx,
route: opts.parallel
? ctx.route.concat(String(i))
: ctx.route,
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
},
series(i > 0 && opts.clear ? clear() : null, task)
);
})
.catch((err) => {
return opts.fail
? onError(err)
: run(
ctx,
series(log('trace', err), log('error', stringifyError(err)))
);
});
},
opts.debounce >= 0 ? opts.debounce : 0
);
const promises: Array<Promise<void>> = [];
await new Promise<void>((resolve, reject) => {
if (opts.prime) {
watcher.on('ready', () => {
promises.push(
run(ctx, log('debug', 'Watch event:', 'prime')).catch(reject)
);
onEvent(reject);
});
}
watcher.on('all', (event) => {
promises.push(
run(ctx, log('debug', 'Watch event:', event)).catch(reject)
);
onEvent(reject);
});
watcher.on('error', reject);
ctx.cancellation.finally(resolve);
}).finally(() => {
watcher.close();
cancel();
});
await Promise.all(promises);
});
});
}