-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
298 lines (250 loc) · 6.56 KB
/
index.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var path = require("path");
var RestartRunner = require("./lib/restart-runner");
var QueueRunner = require("./lib/queue-runner");
var AlivePassThrough = require("./lib/alive-pass-through");
var Watcher = require("./lib/watcher");
function _interpolateCombinedCmd(cmd, options) {
options = options || [];
var filePaths = options.filePaths || [];
if (cmd.indexOf("%") !== -1) {
var cwd = path.resolve(".")
var relFiles = filePaths.join(" ");
var files = filePaths.map(function (f) { return path.resolve(f); }).join(" ");
cmd = cmd
.replace("%cwd", cwd)
.replace("%relFiles", relFiles || "")
.replace("%files", files || "");
}
return cmd;
}
function _interpolateSeparateCmd(cmd, options) {
var filePath = options.filePath;
var action = options.action;
var cmd;
if (cmd.indexOf("%") !== -1) {
var cwd = path.resolve(".")
var relFile = filePath;
var file = path.resolve(filePath);
var relDir = path.dirname(filePath);
var dir = path.resolve(path.dirname(filePath));
cmd = cmd
.replace("%cwd", cwd)
.replace("%event", action || "")
.replace("%relFile", relFile || "")
.replace("%file", file || "")
.replace("%relDir", relDir || "")
.replace("%dir", dir || "");
}
return cmd;
};
var watcher = {};
var execWatcherDefaults = {
debounce: 50,
throttle: 0,
reglob: 1000,
events: ["create", "change", "delete"],
combineEvents: true,
runOnStart: false,
checkMD5: false,
checkMtime: true,
deleteCheckInterval: 25,
deleteCheckTimeout: 100,
debug: false
};
var execRunnerDefaults = {
waitDone: true,
restartOnError: false,
parallelLimit: 8,
combineEvents: true,
shell: true,
stdio: [null, "ignore", "ignore"],
debug: false
};
var killDefaults = {
signal: ["SIGTERM", "SIGTERM", "SIGKILL"]
}
function applyDefaults(options, defaults) {
var copy = {};
for (var key in defaults) {
if (key in options) {
copy[key] = options[key];
} else {
copy[key] = defaults[key];
}
}
return copy;
}
/*
watcher.exec(globs, cmd)
watcher.exec(globs, options, cmd)
*/
watcher.exec = function (globs) {
var options, cmd;
if (arguments.length == 2) {
options = {};
cmd = arguments[1];
} else if (arguments.length == 3) {
options = arguments[1];
cmd = arguments[2];
}
if (options.debug && options.kill && !("debug" in options.kill)) {
options.kill.debug = true;
}
var watcherOptions = applyDefaults(options, execWatcherDefaults);
var runnerOptions = applyDefaults(options, execRunnerDefaults);
runnerOptions.kill = Object.assign({}, killDefaults, options.kill);
var _callback;
if (cmd) {
if (watcherOptions.combineEvents) {
if (runnerOptions.waitDone) {
runnerOptions.parallelLimit = 1;
}
runnerOptions.cmd = function (task) {
return _interpolateCombinedCmd(cmd, task);
};
runnerOptions.reducer = function (queue) {
var last = queue.pop();
var filePaths = last.filePaths.filter(function (f) {
return !queue.find(function (e) { return e.filePaths.indexOf(f) != -1; });
});
if (filePaths.length) {
queue.push({ filePaths });
}
return queue;
};
_callback = function (filePaths) {
w._queueRunner.push({ filePaths });
};
} else {
runnerOptions.cmd = function (task) {
return _interpolateSeparateCmd(cmd, task);
};
runnerOptions.reducer = function (queue) {
var last = queue.pop();
var found = queue.find(function (e) { return e.filePath == last.filePath; });
if (found) {
found.action = last.action;
} else {
queue.push(last);
}
return queue;
};
runnerOptions.skip = function (entry, running) {
if (runnerOptions.waitDone) {
var inProcessing = running.find(function (r) { return r.filePath == entry.filePath; });
return inProcessing;
} else {
return false;
}
};
_callback = function (filePath, action) {
w._queueRunner.push({ filePath, action });
};
}
} else if (callback) {
_callback = callback;
}
var w = new Watcher(globs, watcherOptions, _callback);
if (cmd) {
w._queueRunner = new QueueRunner(runnerOptions);
// HACK: substitute ee to passthrough events
w._queueRunner.ee = w.ee;
if (runnerOptions.stdio) {
if (runnerOptions.stdio[1] == "pipe") {
w.stdout = new AlivePassThrough();
w._queueRunner.stdout.pipe(w.stdout);
}
if (runnerOptions.stdio[2] == "pipe") {
w.stderr = new AlivePassThrough();
w._queueRunner.stderr.pipe(w.stderr);
}
}
w.on("start", function () {
w._queueRunner.start();
if (options.runOnStart) {
w._queueRunner.push({ filePaths: ["."] });
}
});
var _stop = w.stop;
w.stop = function (callback) {
w._queueRunner.stop(function () {
_stop.call(w, callback);
});
};
}
w.start();
return w;
}
var restartWatcherDefaults = {
debounce: 50,
throttle: 0,
reglob: 1000,
events: ["create", "change", "delete"],
checkMD5: false,
checkMtime: true,
deleteCheckInterval: 25,
deleteCheckTimeout: 100,
debug: false
};
var restartRunnerDefaults = {
restartOnError: true,
restartOnSuccess: true,
shell: true,
stdio: [null, "ignore", "ignore"],
debug: false
};
/*
watcher.restart(globs, cmd)
watcher.restart(globs, options, cmd)
*/
watcher.restart = function (globs) {
var options, cmd;
if (arguments.length == 2) {
options = {};
cmd = arguments[1];
} else if (arguments.length == 3) {
options = arguments[1];
cmd = arguments[2];
}
if (options.debug && options.kill && !("debug" in options.kill)) {
options.kill.debug = true;
}
var watcherOptions = applyDefaults(options, restartWatcherDefaults);
var runnerOptions = applyDefaults(options, restartRunnerDefaults);
runnerOptions.kill = Object.assign({}, killDefaults, options.kill);
watcherOptions.callOnStart = true;
watcherOptions.combineEvents = true;
var callback = function (filePaths) {
w._restartRunner.restart(filePaths);
};
var w = new Watcher(globs, watcherOptions, callback);
runnerOptions.cmd = function (task) {
return _interpolateCombinedCmd(cmd, task);
};
w._restartRunner = new RestartRunner(runnerOptions);
// HACK: substitute ee to passthrough events
w._restartRunner.ee = w.ee;
if (runnerOptions.stdio) {
if (runnerOptions.stdio[1] == "pipe") {
w.stdout = new AlivePassThrough();
w._restartRunner.stdout.pipe(w.stdout);
}
if (runnerOptions.stdio[2] == "pipe") {
w.stderr = new AlivePassThrough();
w._restartRunner.stderr.pipe(w.stderr);
}
}
w.on("start", function () {
w._restartRunner.start();
});
var _stop = w.stop;
w.stop = function (callback) {
w._restartRunner.stop(function () {
_stop.call(w, callback);
});
};
w.start();
return w;
}
watcher.Watcher = Watcher;
module.exports = watcher;