-
Notifications
You must be signed in to change notification settings - Fork 670
/
watch.js
65 lines (55 loc) · 1.56 KB
/
watch.js
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
const chalk = require("chalk");
const shelljs = require("shelljs");
const chokidar = require("chokidar");
function debounce(func, wait, immediate) {
let timeout;
function debounced(...args) {
const ctx = this;
if (timeout) clearTimeout(timeout);
if (immediate) {
const callNow = !timeout;
timeout = setTimeout(() => {
// 这里只有在 wait 时长之后,timeout 为 null,然后触发防抖函数才能立即执行,
// 否则 callNow 为 false,不会立即执行,计时器会重新计时
timeout = null;
}, wait);
if (callNow) func.apply(ctx, args);
} else {
timeout = setTimeout(() => {
func.apply(ctx, args);
}, wait);
}
}
debounced.cancel = function cancel() {
clearTimeout(timeout);
timeout = null;
};
return debounced;
}
function build() {
console.log();
console.log(chalk.green("-> building"));
shelljs.exec("cross-env NODE_ENV=production && npx babel src --out-dir lib --copy-files");
console.log(chalk.green("-> build finished"));
}
const dBuild = debounce(build, 1000);
function setupWatch() {
const watcher = chokidar.watch("./src", {
ignoreInitial: true,
});
watcher.on("change", dBuild);
watcher.on("add", dBuild);
watcher.on("ready", () => {
dBuild();
});
process.on("SIGINT", function() {
watcher.close();
process.exit(0);
});
watcher.on("error", (error) => {
console.error("Watcher failure", error);
process.exit(1);
});
}
console.log(chalk.green("rebuild while code under src/ changed"));
setupWatch();