-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
447 lines (409 loc) · 12.4 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
'use strict';
const execa = require('execa');
const chalk = require('chalk');
const trimNewlines = require('trim-newlines');
const prettyMs = require('pretty-ms');
const chokidar = require('chokidar');
const handlebars = require('handlebars');
const beeper = require('beeper');
const FileSet = require('./lib/fileSet');
const GueTasks = require('./lib/GueTasks');
const gueEvents = require('./lib/GueEvents');
/**
* Gue - The main class for the gue task runner
*/
class Gue {
/**
* This doesn't take anything interesting.
*
* @example
* const gue = require('gue');
*/
constructor() {
this.options = {};
this.fileSet = new FileSet();
this.gueTasks = new GueTasks();
this.registerEventHandlers();
}
/**
* Create a new task
*
* Tasks are the core of gue. They are the bits that contain the
* work you want done. A gue task consists of a name and one or more
* things to do. Each task may define a list of dependencies that will
* run to completion prior to running the current task. Each task may
* also specify a function to run.
*
* Tasks are just javascript. You don't need to just use ```gue.shell```.
*
*
* - Tasks should return a promise.
*
* @param {string} name Name of the task
* @param {array} deps Array of task dependencies
* @param {function} func The function to execute for this task
*
* @example
* // Create a task that just calls dep1 and dep2
* task('mytask', ['dep1','dep2']);
*
* // Create a task that runs tests and code coverage
* task('coverage', () =>{
* return gue.shell('nyc mocha tests/*.js')
* })
*
* // Create a task that calls a linter task prior to coverage
* task('coverage', ['lint'], () =>{
* return gue.shell('nyc mocha tests/*.js')
* })
*
*/
task(name, deps, func) {
this.gueTasks.addTask(name, deps, func);
}
/**
* Runs a shell command and prints the output
*
* Shell commands print their buffer when the task is completed. If a shell
* command exits with a non zero status a flag is set so that gue exits
* with 1. STDERR is printed in red.
*
* The command string is actually a
* [handlebars](https://www.npmjs.com/package/handlebars) template.
*
* The following helpers are provided:
* - ```{{files "fileSet"}}```: expands to the files matching the glob in
* fileSet. **the quotes are required**
* - ```{{globs "fileSet"}}```: expands to the glob(s) in the fileSet.
* **the quotes are required**
*
* @param {string} command The shell command to run
* @param {object} value passed to handlebars render
* @return {promise} Promise containing the
* [execa](https://www.npmjs.com/package/execa) result
*
* @example
* gue.fileSet.add('exampleSet', 'README.*');
*
* // README.md
* gue.shell('echo {{files "exampleSet"}}');
*
* // *.md
* gue.shell('echo {{globs "exampleSet"}}');
*
* // woot
* gue.shell('echo {{myString}}', {myString: 'woot'});
*/
shell(command, value) {
return this._shell('print', command, value);
}
/**
* same as shell but doesn't print any output
*
* @param {string} command The shell command to run
* @param {array} value Additional values that get passed to the template
*
* @return {promise} Promise containing the
* [execa](https://www.npmjs.com/package/execa) result
*/
silentShell(command, value) {
return this._shell('silent', command, value);
}
/**
* watch = watch the specified files and run taskList when a change is
* detected
*
* This is just a passthrough to _watch. Done to make it easier to
* maintain API compatibility.
*
* @param {glob} glob [chokidar](https://github.com/paulmillr/chokidar)
* compatible glob
* @param {tasklist} taskList tasks to run when a file in files changes
*
* @return {Promise} A promise for this._watch
*
* @example
* // Run lint and coverage tasks if a file matching src/*.js changes
* gue.watch('src/*.js', ['lint','coverage']);
*
* // Run coverage task if a file matching tests/*.js changes
* gue.watch('tests/*.js', 'coverage');
*
*/
watch(glob, taskList) {
this.log('Started. ^c to stop', 'watch');
return new Promise(() => {
return this._watch(glob, taskList);
});
}
/**
* smartWatch - Watch all files specified in the fileset, run the appropriate
* tasks when one of the files is changed
*
* @param {fileSet} fileSet the fileset object that contains the files to
* watch
*
* @return {Promise} returns a promise for this._smartWatch
*
*/
smartWatch(fileSet) {
this.log('Started. ^c to stop', 'smartWatch');
return new Promise(() => {
this._smartWatch(fileSet);
});
}
/**
* Return an array of the defined tasks
*
* @return {array} Array of the defined tasks
*/
taskList() {
return Object.keys(this.gueTasks.tasks);
}
/**
* Prints a log message
*
* - If only message is passed it behaves like console.log
* - If taskname is passed it's added as a tag to the message
* and the message is colorized
* - If duration is passed the duration of the task is printed
*
* @param {string} message The string to log
* @param {string} taskname The name of the task
* @param {type} duration The task duration in ms
*
*/
log(message, taskname, duration) {
if (!taskname && !duration) {
this._log('clean', message);
} else {
this._log('normal', message, taskname, duration);
}
}
/**
* Prints an error message
*
* Decorates like log, but message is printed in red
*
* @param {string} message The string to log
* @param {string} taskname The name of the task
* @param {int} duration The task duration in ms
*
*/
errLog(message, taskname, duration) {
this._log('error', message, taskname, duration);
}
/**
* Prints a debug message
*
* @param {string} message The message to print
* @param {string} taskname The name of the task
*
*/
debugLog(message, taskname) {
if (this.debug) {
this._log('debug', message, taskname);
}
}
/**
* This is what actually does the shell execution for ```shell```
* and ```silentShell```
*
* See the documentation for '''shell''' for more information
*
* @param {string} mode 'print' or 'silent'
* @param {string} command The shell command/shell command template to run
* @param {object} values values to pass to the command template
*
* @return {promise} Promise containing the
* [execa](https://www.npmjs.com/package/execa) result
*/
_shell(mode, command, values) {
const that = this;
this.debugLog(command, 'debug');
const shellOpts = {
env: {
FORCE_COLOR: 'true',
PATH: process.env.PATH,
},
};
const compiledCmd = buildCmd(that, command);
this.debugLog(compiledCmd(values), 'debug');
return execa.shell(compiledCmd(values), shellOpts)
.then((result) => {
if (mode === 'print') {
this.errLog(trimNewlines(result.stderr));
this.log(trimNewlines(result.stdout));
}
return result;
})
.catch((result) => {
if (mode === 'print') {
this.errLog(trimNewlines(result.stderr));
this.log(trimNewlines(result.stdout));
}
throw result;
});
}
/**
* Uses the fileset object passed to figure out which tasks to run
* based on the files that have changed.
*
* @param {Object} fileSet fileSet object
*
* @return {Object} chokidar watcher
*/
_smartWatch(fileSet) {
const chokidarOpts = {
ignoreInitial: true,
};
const watcher = chokidar.watch(fileSet.getAllFiles(), chokidarOpts);
// This shares a very similar structure to the handler in
// _watch. Only way I could figure out how to extract it involved
// passing two closures which seemed to be worse than repeating myself
// a bit
watcher.on('all', (event, path) => {
const tasks = fileSet.getTasks(path);
this.log(path + ' ' + event + ' running [' + tasks.join(',') + ']',
'smartWatch');
// Stop the watch, then restart after tasks have run
// this fixes looping issues if files are modified
// during the run (as with jscs fix)
watcher.close();
this.gueTasks.runTaskParallel(tasks, true)
.catch(() => {
// don't let errors stop the restart
})
.then(() => {
this._smartWatch(fileSet);
});
});
return watcher;
}
/**
* Watch the specified files and run taskList when a change is detected
*
* @param {glob} glob [chokidar](https://github.com/paulmillr/chokidar)
* compatible glob
* @param {(string|string[])} taskList tasks to run when a file in files
* changes
* @return {object} Returns the chokidar watcher
* @example
* // Run lint and coverage tasks if a file matching src/*.js changes
* gue._watch('src/*.js', ['lint','coverage']);
*
* // Run coverage task if a file matching tests/*.js changes
* gue._watch('tests/*.js', 'coverage');
*
*/
_watch(glob, taskList) {
const chokidarOpts = {
ignoreInitial: true,
};
const watcher = chokidar.watch(glob, chokidarOpts);
watcher.on('all', (event, path) => {
this.log('\n');
this.log(path + ' ' + event, 'watch');
// Stop the watch, then restart after tasks have run
// this fixes looping issues if files are modified
// during the run (as with jscs fix)
watcher.close();
this.gueTasks.runTaskParallel(taskList, true)
.catch(() => {
// don't let errors stop the restart
})
.then(() => {
this._watch(glob, taskList);
});
});
return watcher;
}
/**
* does the actual printing for ```log``` and ```errLog```
*
* - Error type prints the message in red
* - Debug prints the message in yellow
* - Normal type prints the message in cyan
* - Clean type prints the message without any coloring
*
* @param {string} type error normal clean
* @param {string} message The string to log
* @param {string} taskname The name of the task
* @param {int} duration The task duration in ms
*
*/
_log(type, message, taskname, duration) {
let composedMessage = '';
if (!message || message === '') {
return;
}
if (taskname && taskname !== undefined) {
composedMessage += chalk.bold.green('[' + taskname + '] ');
}
if (type === 'error') {
composedMessage += chalk.red(message);
} else if (type === 'debug') {
composedMessage += chalk.yellow(message);
} else if (type === 'normal') {
composedMessage += chalk.cyan(message);
} else if (type === 'clean') {
composedMessage += message;
} else {
composedMessage += message;
}
if (duration !== undefined && duration > 0.0 &&
process.env.NODE_ENV !== 'snapshot') {
composedMessage += ' ' + chalk.white(prettyMs(duration));
}
console.log(composedMessage);
}
/**
* registerEventHandlers - Register the event handlers
*
* These mainly handle ensuring that logs are emitted
* and the exit code is set correctly
*
*/
registerEventHandlers() {
// Log task start
gueEvents.on('GueTask.taskStarted', (task) => {
if (task.name !== 'default') {
this.log('started', task.name, 'normal');
}
});
// Log task stop and task duration
gueEvents.on('GueTask.taskFinished', (task) => {
if (task.name !== 'default') {
this.log('finished in', task.name, task.getTaskDuration());
}
});
// Print stderr and the task finish notification on error
gueEvents.on('GueTask.taskFinished.error', (task, message) => {
process.exitCode = 1;
beeper(1);
this.errLog('finished with error in', task.name,
task.getTaskDuration());
});
}
}
module.exports = new Gue();
//
// Some helpers
//
/**
* buildCmd - Generates the handlebars template
*
* @param {Gue} that The Gue object instance to work on
* @param {string} command The template string
*
* @return {handlebars} Handlebars template object
*/
function buildCmd(that, command) {
handlebars.registerHelper('files', (setName) => {
return that.fileSet.getFiles(setName);
});
handlebars.registerHelper('globs', (setName) => {
return that.fileSet.getGlobs(setName);
});
return handlebars.compile(command);
}