-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
672 lines (585 loc) · 18.7 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
'use strict';
var mout = require('mout');
var fs = require('fs');
var assert = require('assert');
var async = require('async');
var glob = require('glob');
var path = require('path');
var inter = require('./lib/string/interpolate');
var castInter = require('./lib/string/castInterpolate');
var Logger = require('./lib/Logger');
var TaskBuilder = require('./lib/TaskBuilder');
var GruntRunner = require('./lib/grunt/Runner');
/**
* Automaton.
*
* Available options:
* - verbosity - 0 means no logging
* 1 means only 1 deep level tasks and so on..
* -1 means log every level
* - debug - true to log debug messages, false otherwise
* - color - true to keep colors in messages, false otherwise
*
* @param {Object} [options] The options
*/
function Automaton(options) {
var dirs;
var dirname = __dirname;
var rootDir = path.resolve('/');
this._tasks = [];
this._options = options;
// load core tasks
dirs = glob.sync(dirname + '/node_modules/autofile-*');
// find up autofile's directory because latest npm >=v4 have node_modules tree flattened
while (dirname !== rootDir && dirs.length === 0) {
dirname = path.resolve(dirname, '..');
dirs = glob.sync(dirname + '/autofile-*');
}
dirs.forEach(function (dir) {
var errors = this.loadTasks(dir, 'autofile.js');
if (errors.length) {
throw new Error(errors[0]);
}
}, this);
}
/**
* Add new task.
* If the task already exists, it will be replaced.
* The task will be validated only before run.
*
* @param {Object|Function} task The task definition
*
* @return {Automaton} Chainable!
*/
Automaton.prototype.addTask = function (task) {
task = Automaton.getTaskDefinition(task);
TaskBuilder.validate(task);
assert(task.id, 'Can only add tasks with an id');
this._tasks[task.id] = task;
return this;
};
/**
* Remove task.
*
* @param {String} id The task id
*
* @return {Automaton} Chainable!
*/
Automaton.prototype.removeTask = function (id) {
assert(mout.lang.isString(id), 'Invalid task id provided');
delete this._tasks[id];
return this;
};
/**
* Retrieve a task definition by its id.
*
* @param {String} taskId The task id
*
* @return {Object} The task definition or null if not loaded
*/
Automaton.prototype.getTask = function (taskId) {
var task = this._tasks[taskId];
return task ? task : null;
};
/**
* Retrieve the loaded tasks.
*
* @return {Object} The tasks
*/
Automaton.prototype.getTasks = function () {
return this._tasks;
};
/**
* Load tasks in a given folder.
* If the folder contain tasks that are not valid, it results in an error being pushed to
* an array. That array is returned by the function.
*
* @param {String} folder The folder to search for tasks
* @param {String} [pattern] The pattern to use when searching for files, defaults to '*.js'
*
* @return {Array} An array of errors
*/
Automaton.prototype.loadTasks = function (folder, pattern) {
assert(mout.lang.isString(folder), 'Expected folder to be a string');
pattern = pattern || '*.js';
folder = fs.realpathSync(folder) + '/';
var filenames = glob.sync(folder + '/' + pattern),
errors = [];
filenames.forEach(function (file) {
try {
this.addTask(require(file));
} catch (e) {
errors.push(new Error('Unable to add task "' + file + '": ' + e.message));
}
}, this);
return errors;
};
/**
* Run a task.
* The callback follows the node style.
*
* @param {String|Object} task The task id or definition
* @param {Object} [options] The task options
* @param {Function} [callback] A callback to be called when the task completes
*
* @return {Stream} A read stream where logging will be done
*/
Automaton.prototype.run = function (task, options, callback) {
var batch,
handle,
context,
stream;
// function to handle the completion of the task
handle = function (err) {
// kill the grunt worker
context.gruntRunner.kill();
if (err) {
// if error is not actually an error, attempt to fix it
if (!(err instanceof Error)) {
err = new Error(err + '');
}
// log the error
context.log.errorln(err.message);
}
// signal the end of the stream
stream.emit('end');
// call callback if any
if (callback) {
if (err) {
err.message = Logger.removeColors(err.message); // Remove any colors from the message
}
callback(err);
}
return this;
}.bind(this);
// setup an unique context for the task
context = {};
context.log = new Logger(this._options);
context.gruntRunner = new GruntRunner(context);
context.string = {};
context.string.interpolate = inter;
context.string.castInterpolate = castInter;
stream = context.log.getStream();
// catch any error while getting the batch
// and report it with node style callback
batch = this._batchTask(this._createTaskDefinition({
task: task,
options: options,
context: context
}));
// run the batch
process.nextTick(batch.bind(batch, handle));
return stream;
};
/**
* Create a batch for a task.
* The batch is a sequence of functions that form the task.
*
* @param {Object} task The task definition
*
* @return {Function} The batch
*/
Automaton.prototype._batchTask = function (def) {
var batch = [],
preTaskFunc,
posTaskFunc,
option
;
// if task is an id
if (mout.lang.isString(def.task)) {
// grab its real definition
this._assertTaskLoaded(def.task);
def.task = this.getTask(def.task);
} else {
// if task is a function then needs a builder
if (mout.lang.isFunction(def.task)) {
def.task = Automaton.getTaskDefinition(def.task);
}
// trigger validation if is the root task
if (def.depth === 1) {
TaskBuilder.validate(def.task);
}
}
// fill in the options with default values where the option was not provided
for (option in def.task.options) {
if (def.options[option] === undefined && def.task.options[option].default !== undefined) {
def.options[option] = def.task.options[option].default;
}
}
// pre-task
preTaskFunc = function (next) {
var prevNext = next;
// replace options
this._replaceOptions(def.options, def.parentOptions, { skipUnescape : true });
// skip task if disabled
if (!this._isTaskEnabled(def)) {
return process.nextTick(next.bind(next, null, true));
}
// report task
this._onBeforeTask(def);
// after running the setup, we need to replace options again
// because parent setup might have added options that are placeholders
// also we valid if the all mandatory task options are ok
next = function (err) {
if (err) {
return process.nextTick(prevNext.bind(prevNext, err));
}
this._replaceOptions(def.options, def.parentOptions);
process.nextTick(prevNext.bind(prevNext, this._validateTaskOptions(def.task, def.options)));
}.bind(this);
// run setup
if (def.task.setup) {
def.task.setup.call(def.context, def.options, def.context, next);
} else {
next();
}
}.bind(this);
// tasks
def.task.tasks.forEach(function (subtask) {
var subtaskDef = this._createTaskDefinition(subtask, def);
// if it's a grunt task
if (subtaskDef.grunt) {
batch.push(this._batchGruntTask(subtaskDef));
// if it's a inline function
} else if (mout.lang.isFunction(subtaskDef.task) && subtaskDef.task.length !== 1) {
batch.push(this._batchFunctionTask(subtaskDef));
// then it must be another task
} else {
batch.push(this._batchTask(subtaskDef));
}
}, this);
// post task
posTaskFunc = function (err, next) {
err = this._onAfterTask(def, err);
// handle teardown
if (def.task.teardown) {
def.task.teardown.call(def.context, def.options, def.context, function (teardownErr) {
teardownErr = this._onAfterTask(def, teardownErr);
process.nextTick(next.bind(next, err || teardownErr));
}.bind(this));
} else {
process.nextTick(next.bind(next, err));
}
}.bind(this);
// return a final function which calls everything in order
return function (next) {
// run pre-task
preTaskFunc(function (err, disabled) {
// if task is disabled, call next
if (disabled) {
return next();
}
// if there was an error in the pre-task, run pos-task immediately
if (err) {
return posTaskFunc(err, next);
}
// run each subtask
async.series(batch, function (err) {
// finally run the pos-task, even if there was an error
posTaskFunc(err, next);
});
});
};
};
/**
* Create a batch for single function task.
*
* @param {Object} task The task definition
*
* @return {Function} The batch
*/
Automaton.prototype._batchFunctionTask = function (def) {
return function (next) {
this._replaceOptions(def.options, def.parentOptions);
// skip task if disabled
if (!this._isTaskEnabled(def)) {
return process.nextTick(next);
}
// Report task
this._onBeforeTask(def);
// run the function
// note that the options are the parent options
def.task.call(def.context, def.parentOptions, def.context, function (err) {
err = this._onAfterTask(def, err);
process.nextTick(next.bind(next, err));
}.bind(this));
}.bind(this);
};
/**
* Create a batch for a grunt task.
*
* @param {Object} task The task definition
*
* @return {Function} The batch
*/
Automaton.prototype._batchGruntTask = function (def) {
return function (next) {
// replace options
this._replaceOptions(def.options, def.parentOptions);
// skip task if disabled
if (!this._isTaskEnabled(def)) {
return process.nextTick(next);
}
// report task
this._onBeforeTask(def);
// run grunt task in the grunt runner
def.grunt = !mout.lang.isObject(def.grunt) ? {} : def.grunt;
def.context.gruntRunner
.run(def.task, def.options, def.grunt)
.on('data', def.context.log.write.bind(def.context.log))
.on('error', function (err) {
err = this._onAfterTask(def, err);
process.nextTick(next.bind(next, err));
}.bind(this))
.on('end', function () {
process.nextTick(next);
});
}.bind(this);
};
/**
* Creates a task definition for a task.
*
* @param {Object} task The task
* @param {Object} parentTaskDef The parent task definition
*
* @return {Object} The task definition
*/
Automaton.prototype._createTaskDefinition = function (task, parentTaskDef) {
var def = mout.object.mixIn({}, task);
def.options = def.options ? mout.lang.deepClone(def.options) : {};
def.depth = 1;
if (parentTaskDef) {
def.parentOptions = parentTaskDef.options;
def.context = parentTaskDef.context;
def.depth += parentTaskDef.depth;
} else {
def.parentOptions = def.options;
}
return def;
};
/**
* Function to run before each task.
* Reports the task that will run and sets up the logger.
*
* @param {Object} task The task definition
*/
Automaton.prototype._onBeforeTask = function (def) {
var desc,
logger = def.context.log,
inline = mout.lang.isFunction(def.task) || def.grunt;
// try out to extract the description, falling back to the name
desc = def.description !== undefined ? def.description : def.task.description || def.task.name;
if (mout.lang.isFunction(desc)) {
desc = desc(def.parentOptions) + '';
}
if (desc !== null) {
if (!desc) {
// if is an inline function that has no description, then simply do not report
if (inline) {
desc = null;
} else {
// otherwise assume '??'
desc = '??';
}
}
}
// set the logger depth
logger.setDepth(def.depth);
// log task that will run
// if desc is null, simply do not report it
if (desc != null) {
desc = this._replacePlaceholders(desc, def.parentOptions, { purge: true });
logger.infoln('> ' + desc);
}
// mute the logger if task is marked as muted and logger is unmuted
if (!logger.isMuted() && this._isTaskMuted(def)) {
logger.mute();
def.mutedLogger = true;
}
};
/**
* Function to run after each task.
* Finishes up the logger and handle fatal.
*
* @param {Object} task The task definition
* @param {Error} err The task error or null if none
*
* @return {Error} The error after processing
*/
Automaton.prototype._onAfterTask = function (def, err) {
var name;
// handle fatal
if (err && !this._isTaskFatal(def, err)) {
name = def.task.id || def.task.name || 'unknown';
def.context.log.debugln('Task "' + name + '" silently failed: ' + err.message);
err = null;
}
// unmute the logger if this task muted the logger
if (def.mutedLogger) {
def.context.log.unmute();
delete def.mutedLogger;
}
return err;
};
/**
* Check if a task is fatal.
* Disabled tasks should be skipped.
*
* @param {Object} def The task definition
* @param {Error} err The task error or null if none
*
* @return {Boolean} True if enabled, false otherwise
*/
Automaton.prototype._isTaskFatal = function (def, err) {
if (def.hasOwnProperty('fatal')) {
if (mout.lang.isString(def.fatal)) {
return !!this._replacePlaceholders(def.fatal, def.parentOptions, { purge: true });
} else if (mout.lang.isFunction(def.fatal)) {
return !!def.fatal.call(def.context, err, def.parentOptions, def.context);
} else {
return !!def.fatal;
}
}
return true;
};
/**
* Check if a task is enabled.
* Disabled tasks should be skipped.
*
* @param {Object} def The task definition
*
* @return {Boolean} True if enabled, false otherwise
*/
Automaton.prototype._isTaskEnabled = function (def) {
if (def.hasOwnProperty('on')) {
if (mout.lang.isString(def.on)) {
return !!this._replacePlaceholders(def.on, def.parentOptions, { purge: true });
} else if (mout.lang.isFunction(def.on)) {
return !!def.on.call(def.context, def.parentOptions, def.context);
} else {
return !!def.on;
}
}
return true;
};
/**
* Check if a task is muted.
* Muted tasks do not log messages.
*
* @param {Object} def The task definition
*
* @return {Boolean} True if muted, false otherwise
*/
Automaton.prototype._isTaskMuted = function (def) {
if (def.hasOwnProperty('mute')) {
if (mout.lang.isString(def.mute)) {
return !!this._replacePlaceholders(def.mute, def.parentOptions, { purge: true });
} else if (mout.lang.isFunction(def.mute)) {
return !!def.mute.call(def.context, def.parentOptions, def.context);
} else {
return !!def.mute;
}
}
return false;
};
/**
* Replace target placeholders with their correspondent options value.
* If the target is an array or an object, it will replace them
* recursively.
*
* @param {Mixed} target The target which will get its values replaced
* @param {Object} values The values
* @param {Object} [options] The interpolation options
*
* @return {Mixed} The passed target
*/
Automaton.prototype._replaceOptions = function (target, values, options) {
var k,
newK;
if (mout.lang.isObject(target)) {
for (k in target) {
newK = this._replacePlaceholders(k, values, options) + '';
target[newK] = this._replaceOptions(target[k], values, options);
if (newK !== k) {
delete target[k];
}
}
} else if (mout.lang.isArray(target)) {
for (k = target.length - 1; k >= 0; --k) {
target[k] = this._replaceOptions(target[k], values, options);
}
} else if (mout.lang.isString(target)) {
target = this._replacePlaceholders(target, values, options);
}
return target;
};
/**
* Replace placeholders in a string with their correspondent values
*
* @param {String} str The string
* @param {Object} values The values
* @param {Object} [options] The interpolate options
*
* @return {String} The replaced string
*/
Automaton.prototype._replacePlaceholders = function (str, values, options) {
return castInter(str, values, options);
};
/**
* Validate the task options.
* Detects if a task is missing required options.
*
* @param {Object} task The task definition
* @param {Object} options The task options
*
* @return {Error} The error if any
*/
Automaton.prototype._validateTaskOptions = function (task, options, verbose) {
var option;
for (option in task.options) {
// if option was not provided to the task, abort
if (options[option] === undefined) {
return new Error('Missing option \'' + option + '\' in \'' + task.id + '\' task', verbose);
}
}
};
/**
* Assert task is loaded.
*
* @param {String} taskId The task id
*/
Automaton.prototype._assertTaskLoaded = function (taskId) {
assert(this.getTask(taskId), 'Could not find any task handler suitable for \'' + taskId + '\'');
};
/**
* Get the task object in case the task is a function that will build the object.
*
* @param {Object|Function} task The task object or the builder
*
* @return {Object} The task
*/
Automaton.getTaskDefinition = function (task) {
var builder;
if (mout.lang.isFunction(task)) {
builder = new TaskBuilder();
try {
task(builder);
} catch (e) {
throw new Error('Unable to get task from builder: ' + e.message);
}
task = builder.toObject();
}
return task;
};
/**
* Creates a new automaton instance.
* Please see the constructor for more info about the available options.
*
* @param {Object} [options] The options
*
* @return {Automaton} A new automaton instance
*/
Automaton.create = function (options) {
return new Automaton(options);
};
module.exports = Automaton;