common.js
2.44 KB
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
'use strict';
const walker = require('walker');
const anymatch = require('anymatch');
const micromatch = require('micromatch');
const path = require('path');
const platform = require('os').platform();
/**
* Constants
*/
exports.DEFAULT_DELAY = 100;
exports.CHANGE_EVENT = 'change';
exports.DELETE_EVENT = 'delete';
exports.ADD_EVENT = 'add';
exports.ALL_EVENT = 'all';
/**
* Assigns options to the watcher.
*
* @param {NodeWatcher|PollWatcher|WatchmanWatcher} watcher
* @param {?object} opts
* @return {boolean}
* @public
*/
exports.assignOptions = function(watcher, opts) {
opts = opts || {};
watcher.globs = opts.glob || [];
watcher.dot = opts.dot || false;
watcher.ignored = opts.ignored || false;
if (!Array.isArray(watcher.globs)) {
watcher.globs = [watcher.globs];
}
watcher.hasIgnore =
Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
if (opts.watchman && opts.watchmanPath) {
watcher.watchmanPath = opts.watchmanPath;
}
return opts;
};
/**
* Checks a file relative path against the globs array.
*
* @param {array} globs
* @param {string} relativePath
* @return {boolean}
* @public
*/
exports.isFileIncluded = function(globs, dot, doIgnore, relativePath) {
if (doIgnore(relativePath)) {
return false;
}
return globs.length
? micromatch.some(relativePath, globs, { dot: dot })
: dot || micromatch.some(relativePath, '**/*');
};
/**
* Traverse a directory recursively calling `callback` on every directory.
*
* @param {string} dir
* @param {function} dirCallback
* @param {function} fileCallback
* @param {function} endCallback
* @param {*} ignored
* @public
*/
exports.recReaddir = function(
dir,
dirCallback,
fileCallback,
endCallback,
errorCallback,
ignored
) {
walker(dir)
.filterDir(currentDir => !anymatch(ignored, currentDir))
.on('dir', normalizeProxy(dirCallback))
.on('file', normalizeProxy(fileCallback))
.on('error', errorCallback)
.on('end', () => {
if (platform === 'win32') {
setTimeout(endCallback, 1000);
} else {
endCallback();
}
});
};
/**
* Returns a callback that when called will normalize a path and call the
* original callback
*
* @param {function} callback
* @return {function}
* @private
*/
function normalizeProxy(callback) {
return (filepath, stats) => callback(path.normalize(filepath), stats);
}