-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tasks.js
409 lines (355 loc) · 12 KB
/
tasks.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
'use strict';
const Task = require('./task');
const Timer = require('./timer');
const Events = require('events');
const { createOptions, flatten, noop } = require('./utils');
/**
* Factory for creating a custom `Tasks` class that extends the
* given `Emitter`. Or, simply call the factory function to use
* the built-in emitter.
*
* ```js
* // custom emitter
* const Emitter = require('events');
* const Tasks = require('composer/lib/tasks')(Emitter);
* // built-in emitter
* const Tasks = require('composer/lib/tasks')();
* const composer = new Tasks();
* ```
* @name .factory
* @param {function} `Emitter` Event emitter.
* @return {Class} Returns a custom `Tasks` class.
* @api public
*/
const factory = (Emitter = Events) => {
/**
* Create an instance of `Tasks` with the given `options`.
*
* ```js
* const Tasks = require('composer').Tasks;
* const composer = new Tasks();
* ```
* @class
* @name Tasks
* @param {object} `options`
* @api public
*/
class Tasks extends Emitter {
constructor(options = {}) {
super(!Emitter.name.includes('Emitter') ? options : null);
this.options = options;
this.taskStack = new Map();
this.tasks = new Map();
this.taskId = 0;
if (this.off === void 0 && typeof this.removeListener === 'function') {
this.off = this.removeListener.bind(this);
}
}
/**
* Define a task. Tasks run asynchronously, either in series (by default) or parallel
* (when `options.parallel` is true). In order for the build to determine when a task is
* complete, _one of the following_ things must happen: 1) the callback must be called, 2) a
* promise must be returned, or 3) a stream must be returned. Inside tasks, the "this"
* object is a composer Task instance created for each task with useful properties like
* the task name, options and timing information, which can be useful for logging, etc.
*
* ```js
* // 1. callback
* app.task('default', cb => {
* // do stuff
* cb();
* });
* // 2. promise
* app.task('default', () => {
* return Promise.resolve(null);
* });
* // 3. stream (using vinyl-fs or your stream of choice)
* app.task('default', function() {
* return vfs.src('foo/*.js');
* });
* ```
* @name .task
* @param {String} `name` The task name.
* @param {Object|Array|String|Function} `deps` Any of the following: task dependencies, callback(s), or options object, defined in any order.
* @param {Function} `callback` (optional) If the last argument is a function, it will be called after all of the task's dependencies have been run.
* @return {undefined}
* @api public
*/
task(name, ...rest) {
if (typeof name !== 'string') {
throw new TypeError('expected task "name" to be a string');
}
const { options, tasks } = createOptions(this, false, ...rest);
const callback = typeof tasks[tasks.length - 1] === 'function' ? tasks.pop() : noop;
return this.setTask(name, options, tasks, callback);
}
/**
* Set a task on `app.tasks`
* @name .setTask
* @param {string} name Task name
* @param {object} name Task options
* @param {object|array|string|function} `deps` Task dependencies
* @param {Function} `callback` (optional) Final callback function to call after all task dependencies have been run.
* @return {object} Returns the instance.
*/
setTask(name, options = {}, deps = [], callback) {
const task = new Task({ name, options, deps, callback, app: this });
const emit = (key = 'task') => this.emit(key, task);
task.on('error', this.emit.bind(this, 'error'));
task.on('preparing', () => emit('task-preparing'));
task.on('starting', task => {
this.taskStack.set(task.name, task);
emit();
});
task.on('finished', task => {
this.taskStack.delete(task.name);
emit();
});
this.tasks.set(name, task);
task.status = 'registered';
emit('task-registered');
return this;
}
/**
* Get a task from `app.tasks`.
* @name .getTask
* @param {string} name
* @return {object} Returns the task object.
*/
getTask(name) {
if (!this.tasks.has(name)) {
throw this.formatError(name, 'task');
}
return this.tasks.get(name);
}
/**
* Returns true if all values in the array are registered tasks.
* @name .isTasks
* @param {array} tasks
* @return {boolean}
*/
isTasks(arr) {
return Array.isArray(arr) && arr.every(name => this.tasks.has(name));
}
/**
* Create an array of tasks to run by resolving registered tasks from the values
* in the given array.
* @name .expandTasks
* @param {...[string|function|glob]} tasks
* @return {array}
*/
expandTasks(...args) {
let vals = flatten(args).filter(Boolean);
let keys = [...this.tasks.keys()];
let tasks = [];
for (let task of vals) {
if (typeof task === 'function') {
let name = `task-${this.taskId++}`;
this.task(name, task);
tasks.push(name);
continue;
}
if (typeof task === 'string') {
if (/\*/.test(task)) {
let matches = match(keys, task);
if (matches.length === 0) {
throw new Error(`glob "${task}" does not match any registered tasks`);
}
tasks.push.apply(tasks, matches);
continue;
}
tasks.push(task);
continue;
}
let msg = 'expected task dependency to be a string or function, but got: ';
throw new TypeError(msg + typeof task);
}
return tasks;
}
/**
* Run one or more tasks.
*
* ```js
* const build = app.series(['foo', 'bar', 'baz']);
* // promise
* build().then(console.log).catch(console.error);
* // or callback
* build(function() {
* if (err) return console.error(err);
* });
* ```
* @name .build
* @param {object|array|string|function} `tasks` One or more tasks to run, options, or callback function. If no tasks are defined, the default task is automatically run.
* @param {function} `callback` (optional)
* @return {undefined}
* @api public
*/
async build(...args) {
let state = { status: 'starting', time: new Timer(), app: this };
state.time.start();
this.emit('build', state);
args = flatten(args);
let cb = typeof args[args.length - 1] === 'function' ? args.pop() : null;
let { options, tasks } = createOptions(this, true, ...args);
if (!tasks.length) tasks = ['default'];
let each = options.parallel ? this.parallel : this.series;
let build = each.call(this, options, ...tasks);
let promise = build()
.then(() => {
state.time.end();
state.status = 'finished';
this.emit('build', state);
});
return resolveBuild(promise, cb);
}
/**
* Compose a function to run the given tasks in series.
*
* ```js
* const build = app.series(['foo', 'bar', 'baz']);
* // promise
* build().then(console.log).catch(console.error);
* // or callback
* build(function() {
* if (err) return console.error(err);
* });
* ```
* @name .series
* @param {object|array|string|function} `tasks` Tasks to run, options, or callback function. If no tasks are defined, the `default` task is automatically run, if one exists.
* @param {function} `callback` (optional)
* @return {promise|undefined} Returns a promise if no callback is passed.
* @api public
*/
series(...args) {
let stack = new Set();
let compose = this.iterator('series', async(tasks, options, resolve) => {
for (let ele of tasks) {
let task = this.getTask(ele);
task.series = true;
if (task.skip(options) || stack.has(task)) {
continue;
}
task.once('finished', () => stack.delete(task));
task.once('starting', () => stack.add(task));
let run = task.run(options);
if (task.deps.length) {
let opts = Object.assign({}, options, task.options);
let each = opts.parallel ? this.parallel : this.series;
let build = each.call(this, ...task.deps);
await build();
}
await run();
}
resolve();
});
return compose(...args);
}
/**
* Compose a function to run the given tasks in parallel.
*
* ```js
* // call the returned function to start the build
* const build = app.parallel(['foo', 'bar', 'baz']);
* // promise
* build().then(console.log).catch(console.error);
* // callback
* build(function() {
* if (err) return console.error(err);
* });
* // example task usage
* app.task('default', build);
* ```
* @name .parallel
* @param {object|array|string|function} `tasks` Tasks to run, options, or callback function. If no tasks are defined, the `default` task is automatically run, if one exists.
* @param {function} `callback` (optional)
* @return {promise|undefined} Returns a promise if no callback is passed.
* @api public
*/
parallel(...args) {
let stack = new Set();
let compose = this.iterator('parallel', (tasks, options, resolve) => {
let pending = [];
for (let ele of tasks) {
let task = this.getTask(ele);
task.parallel = true;
if (task.skip(options) || stack.has(task)) {
continue;
}
task.once('finished', () => stack.delete(task));
task.once('starting', () => stack.add(task));
let run = task.run(options);
if (task.deps.length) {
let opts = Object.assign({}, options, task.options);
let each = opts.parallel ? this.parallel : this.series;
let build = each.call(this, ...task.deps);
pending.push(build().then(() => run()));
} else {
pending.push(run());
}
}
resolve(Promise.all(pending));
});
return compose(...args);
}
/**
* Create an async iterator function that ensures that either a promise is
* returned or the user-provided callback is called.
* @param {function} `fn` Function to invoke inside the promise.
* @return {function}
*/
iterator(type, fn) {
return (...args) => {
let { options, tasks } = createOptions(this, true, ...args);
return cb => {
let promise = new Promise(async(resolve, reject) => {
if (tasks.length === 0) {
resolve();
return;
}
try {
let p = fn(tasks, options, resolve);
if (type === 'series') await p;
} catch (err) {
reject(err);
}
});
return resolveBuild(promise, cb);
};
};
}
/**
* Format task and generator errors.
* @name .formatError
* @param {String} `name`
* @return {Error}
*/
formatError(name) {
return new Error(`task "${name}" is not registered`);
}
/**
* Static method for creating a custom Tasks class with the given `Emitter.
* @name .create
* @param {Function} `Emitter`
* @return {Class} Returns the custom class.
* @api public
* @static
*/
static create(Emitter) {
return factory(Emitter);
}
}
return Tasks;
};
function resolveBuild(promise, cb) {
if (typeof cb === 'function') {
promise.then(val => cb(null, val)).catch(cb);
} else {
return promise;
}
}
function match(keys, pattern) {
let chars = [...pattern].map(ch => ({ '*': '.*?', '.': '\\.' }[ch] || ch));
let regex = new RegExp(chars.join(''));
return keys.filter(key => regex.test(key));
}
module.exports = factory();