/
run-task.js
66 lines (53 loc) · 1.71 KB
/
run-task.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
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const { logStartTask, logEndTask, logEndBuild } = require('./logging');
module.exports = function runTasks(tasks) {
const package = getPackage();
if (!package) {
return;
}
const packageName = package.name;
const isProduction = process.argv.indexOf('--production') > -1;
// Filter disabled tasks if specified in the package.json.
if (package.disabledTasks) {
tasks = tasks.filter(task => package.disabledTasks.indexOf(task) < 0);
}
if (process.argv.length >= 3 && process.argv[2].indexOf('--') === -1) {
tasks = [process.argv[2]];
}
let promise = Promise.resolve();
let hasFailures = false;
let buildStartTime = new Date().getTime();
tasks.forEach(task => {
promise = promise.then(() => runTask(task));
});
promise.then(() => {
if (hasFailures) {
process.exitCode = 1;
}
logEndBuild(packageName, !hasFailures, buildStartTime);
});
function runTask(task) {
let taskStartTime = new Date().getTime();
return Promise.resolve().then(
() =>
!hasFailures &&
Promise.resolve()
.then(() => logStartTask(packageName, task))
.then(() => require('./tasks/' + task)({ isProduction, argv: process.argv }))
.then(() => logEndTask(packageName, task, taskStartTime))
.catch(e => {
hasFailures = true;
logEndTask(packageName, task, taskStartTime, e);
})
);
}
function getPackage() {
let packagePath = path.resolve(process.cwd(), 'package.json');
if (fs.existsSync(packagePath)) {
return JSON.parse(fs.readFileSync(packagePath, 'utf8'));
}
return undefined;
}
};