1 | const path = require("path");
|
2 | const gulp = require("gulp");
|
3 | const chalk = require("chalk");
|
4 | const log = require("./logger");
|
5 | const compile = require("./compile");
|
6 | const fs = require("fs-extra");
|
7 | const blue = chalk.keyword("green");
|
8 |
|
9 | module.exports = function (options) {
|
10 | const filter = options.filter;
|
11 |
|
12 | const watcher = gulp.watch(options.glob, {
|
13 | cwd: options.appSrc,
|
14 | allowEmpty: true,
|
15 | delay: 300,
|
16 | });
|
17 |
|
18 | watcher.on("change", async function (file, stats) {
|
19 | if (filter && !filter(file)) {
|
20 | return;
|
21 | }
|
22 |
|
23 | await compile(file, options);
|
24 | log("文件修改:%s", blue(file));
|
25 | log.flush();
|
26 | });
|
27 |
|
28 | watcher.on("add", async function (file, stats) {
|
29 | if (filter && !filter(file)) {
|
30 | return;
|
31 | }
|
32 |
|
33 | await compile(file, options);
|
34 | log("文件新增:%s", blue(file));
|
35 | log.flush();
|
36 | });
|
37 |
|
38 | watcher.on("unlink", async function (file, stats) {
|
39 | if (filter && !filter(file)) {
|
40 | return;
|
41 | }
|
42 |
|
43 | fs.removeSync(path.resolve(options.appSrc, file));
|
44 | log("文件删除:%s", blue(file));
|
45 | log.flush();
|
46 | });
|
47 |
|
48 | watcher.on("error", async function () {
|
49 |
|
50 | });
|
51 | };
|