2021-02-04 19:15:23 +01:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const spawn = require('cross-spawn');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tasks = {};
|
|
|
|
const taskEvents = new EventEmitter();
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
module.exports = {
|
|
|
|
detectAndRunEntryTask,
|
|
|
|
tasks,
|
|
|
|
taskEvents,
|
|
|
|
createTask,
|
|
|
|
runTask,
|
|
|
|
composeSeries,
|
|
|
|
composeParallel,
|
|
|
|
runInChildProcess,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { setupTaskDisplay } = require('./display');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function detectAndRunEntryTask() {
|
2020-03-09 01:55:02 +01:00
|
|
|
// get requested task name and execute
|
2021-02-04 19:15:23 +01:00
|
|
|
const taskName = process.argv[2];
|
2020-03-09 01:55:02 +01:00
|
|
|
if (!taskName) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`MetaMask build: No task name specified`);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const skipStats = process.argv[3] === '--skip-stats';
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
runTask(taskName, { skipStats });
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function runTask(taskName, { skipStats } = {}) {
|
2020-03-09 01:55:02 +01:00
|
|
|
if (!(taskName in tasks)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`MetaMask build: Unrecognized task name "${taskName}"`);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
if (!skipStats) {
|
2021-02-04 19:15:23 +01:00
|
|
|
setupTaskDisplay(taskEvents);
|
|
|
|
console.log(`running task "${taskName}"...`);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await tasks[taskName]();
|
2020-03-09 01:55:02 +01:00
|
|
|
} catch (err) {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.error(
|
|
|
|
`MetaMask build: Encountered an error while running task "${taskName}".`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
taskEvents.emit('complete');
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function createTask(taskName, taskFn) {
|
2020-03-09 01:55:02 +01:00
|
|
|
if (taskName in tasks) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`MetaMask build: task "${taskName}" already exists. Refusing to redefine`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const task = instrumentForTaskStats(taskName, taskFn);
|
|
|
|
task.taskName = taskName;
|
|
|
|
tasks[taskName] = task;
|
|
|
|
return task;
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function runInChildProcess(task) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const taskName = typeof task === 'string' ? task : task.taskName;
|
2020-03-09 01:55:02 +01:00
|
|
|
if (!taskName) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`MetaMask build: runInChildProcess unable to identify task name`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
return instrumentForTaskStats(taskName, async () => {
|
2020-11-24 04:26:43 +01:00
|
|
|
const childProcess = spawn('yarn', ['build', taskName, '--skip-stats'], {
|
|
|
|
env: process.env,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
// forward logs to main process
|
|
|
|
// skip the first stdout event (announcing the process command)
|
|
|
|
childProcess.stdout.once('data', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
childProcess.stdout.on('data', (data) =>
|
|
|
|
process.stdout.write(`${taskName}: ${data}`),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
childProcess.stderr.on('data', (data) =>
|
|
|
|
process.stderr.write(`${taskName}: ${data}`),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
// await end of process
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
childProcess.once('close', (errCode) => {
|
|
|
|
if (errCode !== 0) {
|
2020-11-03 00:41:28 +01:00
|
|
|
reject(
|
|
|
|
new Error(
|
2020-11-24 04:26:43 +01:00
|
|
|
`MetaMask build: runInChildProcess for task "${taskName}" encountered an error ${errCode}`,
|
2020-11-03 00:41:28 +01:00
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return;
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function instrumentForTaskStats(taskName, asyncFn) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const start = Date.now();
|
|
|
|
taskEvents.emit('start', [taskName, start]);
|
|
|
|
await asyncFn();
|
|
|
|
const end = Date.now();
|
|
|
|
taskEvents.emit('end', [taskName, start, end]);
|
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function composeSeries(...subtasks) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const realTasks = subtasks;
|
2020-03-09 01:55:02 +01:00
|
|
|
for (const subtask of realTasks) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await subtask();
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function composeParallel(...subtasks) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const realTasks = subtasks;
|
|
|
|
await Promise.all(realTasks.map((subtask) => subtask()));
|
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|