-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.watch.js
More file actions
158 lines (135 loc) · 5.01 KB
/
cli.watch.js
File metadata and controls
158 lines (135 loc) · 5.01 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
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
154
155
156
157
158
/* c8 ignore start */ // Remove this line when we've found a way to cover the bits of this code that do not work across all platforms
const fs = require('fs');
const path = require('path');
const common = require('./common');
const cwd = process.cwd();
const cwds = path.join(cwd, '/');
const debounceTimeout = 500;
// RegExps
const reJsFile = /\.js$/;
async function watchFiles(arg0, runTests, isVerbose, stopSignal, onScan) {
let testsFiles = [];
let nonTestFiles = [];
let fileWatchers = [];
let allFiles = [];
let custom = false;
if (arg0) {
let reGlob = common.microMatch(arg0);
if (typeof reGlob === 'string') {
// Not a regexp, just try to testrun the file
testsFiles.push(arg0);
} else {
allFiles = await common.getFiles(cwd);
testsFiles = allFiles.filter(f => f.replace(cwds, '').match(reGlob));
}
custom = true;
}
// Watch
const watch = (file, callback) => {
let watcher = fs.watch(file, callback);
fileWatchers.push(watcher);
};
// Collect & debounce
let requestTimeout;
let requested = [];
const requestRun = f => {
if (!requested.includes(f)) {
requested.push(f);
}
clearTimeout(requestTimeout);
requestTimeout = setTimeout(_ => {
console.log(' ');
console.log(common.Color.gray('[watch] Changes detected, running tests...'));
if (isVerbose) {
console.log(requested.map(r => path.basename(r)).join(', '));
}
console.log(' ');
runTests(requested);
requested.length = 0;
}, debounceTimeout);
};
const requestRuns = files => files.forEach(requestRun);
// Scan and watch
const scanAndWatch = common.debounce(async _ => {
clearTimeout(requestTimeout);
fileWatchers.forEach(w => w.close());
fileWatchers.length = 0;
if (!custom) {
allFiles = await common.getFiles(cwd);
testsFiles = common.filterFiles(allFiles, common.REGEXP_TEST_FILES, common.REGEXP_IGNORE_FILES);
}
nonTestFiles = allFiles.filter(f => !testsFiles.includes(f));
nonTestFiles = common.filterFiles(nonTestFiles, [reJsFile], common.REGEXP_IGNORE_FILES);
// Watch test files
testsFiles.forEach(tf => {
watch(tf, (type, fileName) => {
if (isVerbose) {
console.log('Watch triggered for test file:', type, fileName);
}
requestRun(tf);
});
});
// Watch non-test files and simply run all tests when these change
nonTestFiles.forEach(ntf => {
watch(ntf, (type, fileName) => {
let ext = path.extname(fileName);
let base = path.basename(fileName, ext);
if (isVerbose) {
console.log('Watch triggered for non-test file:', type, fileName);
}
let relevantTestFiles = testsFiles.filter(tf => path.basename(tf).startsWith(base));
if (relevantTestFiles.length > 0) {
requestRuns(relevantTestFiles);
} else {
// Test all files
requestRuns(testsFiles);
}
});
});
console.log(common.Color.gray('[watch] aqa - watcher active, waiting for file changes...'));
if (typeof onScan === 'function') {
onScan(testsFiles, nonTestFiles);
}
}, debounceTimeout);
// Watch dir
let dirWatcher = null;
try {
dirWatcher = fs.watch(cwd, { recursive: true }, (type, fileName) => {
if (fileName == null) {
return;
}
let resolvedFileName = path.join(cwd, fileName);
if (isVerbose) {
console.log('Watch triggered for dir:', type, cwd, resolvedFileName);
}
let filtered = common.filterFiles([resolvedFileName], [reJsFile], common.REGEXP_IGNORE_FILES);
if (filtered.length === 0) return;
if (!testsFiles.includes(resolvedFileName) && !nonTestFiles.includes(resolvedFileName)) {
clearTimeout(requestTimeout);
requested.length = 0;
console.log('New file detected:', resolvedFileName, '- rescanning');
scanAndWatch();
}
});
} catch (e) {
// Recursive watch is probably not supported on this platform
console.log(common.Color.yellow('[watch] Warning: ' + e.message));
}
scanAndWatch();
// Initial run
//testsFiles.forEach(tf => requestRun(tf));
return new Promise(resolve => {
if (stopSignal) {
stopSignal.stop = function () {
fileWatchers.forEach(w => w.close());
if (dirWatcher) {
dirWatcher.close();
}
resolve();
};
}
});
}
module.exports = {
watchFiles
};