1 |
|
2 | 'use strict';
|
3 |
|
4 | const platform = require('os').platform();
|
5 |
|
6 | const path = require('path');
|
7 |
|
8 | const anymatch = require('anymatch');
|
9 |
|
10 | const micromatch = require('micromatch');
|
11 |
|
12 | const walker = require('walker');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | exports.DEFAULT_DELAY = 100;
|
18 | exports.CHANGE_EVENT = 'change';
|
19 | exports.DELETE_EVENT = 'delete';
|
20 | exports.ADD_EVENT = 'add';
|
21 | exports.ALL_EVENT = 'all';
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | exports.assignOptions = function (watcher, opts) {
|
32 | opts = opts || {};
|
33 | watcher.globs = opts.glob || [];
|
34 | watcher.dot = opts.dot || false;
|
35 | watcher.ignored = opts.ignored || false;
|
36 |
|
37 | if (!Array.isArray(watcher.globs)) {
|
38 | watcher.globs = [watcher.globs];
|
39 | }
|
40 |
|
41 | watcher.hasIgnore =
|
42 | Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
|
43 | watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
|
44 |
|
45 | if (opts.watchman && opts.watchmanPath) {
|
46 | watcher.watchmanPath = opts.watchmanPath;
|
47 | }
|
48 |
|
49 | return opts;
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.isFileIncluded = function (globs, dot, doIgnore, relativePath) {
|
61 | if (doIgnore(relativePath)) {
|
62 | return false;
|
63 | }
|
64 |
|
65 | return globs.length
|
66 | ? micromatch.some(relativePath, globs, {
|
67 | dot
|
68 | })
|
69 | : dot || micromatch.some(relativePath, '**/*');
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.recReaddir = function (
|
83 | dir,
|
84 | dirCallback,
|
85 | fileCallback,
|
86 | endCallback,
|
87 | errorCallback,
|
88 | ignored
|
89 | ) {
|
90 | walker(dir)
|
91 | .filterDir(currentDir => !anymatch(ignored, currentDir))
|
92 | .on('dir', normalizeProxy(dirCallback))
|
93 | .on('file', normalizeProxy(fileCallback))
|
94 | .on('error', errorCallback)
|
95 | .on('end', () => {
|
96 | if (platform === 'win32') {
|
97 | setTimeout(endCallback, 1000);
|
98 | } else {
|
99 | endCallback();
|
100 | }
|
101 | });
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function normalizeProxy(callback) {
|
113 | return (filepath, stats) => callback(path.normalize(filepath), stats);
|
114 | }
|