1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/development/build/task.js
Mark Stacey b124ac29fc
Improve detection of task process exit (#10776)
Our build script waits for the `close` event to determine whether the
task has exited. The `exit` event is a better representation of this,
because if a stream is shared between multiple processes, the process
may exit without the `close` event being emitted.

We aren't sharing streams between processes, so this edge case doesn't
apply to us. This just seemed like a more suitable event to listen to,
since we care about the process exiting not the stream ending.

See this description of the `close` event from the Node.js
documentation [1]:

>The `'close'` event is emitted when the stdio streams of a child
>process have been closed. This is distinct from the `'exit'` event,
>since multiple processes might share the same stdio streams.

And see this description of the `exit` event:

>The `'exit'` event is emitted after the child process ends.

[1]: https://nodejs.org/docs/latest-v14.x/api/child_process.html#child_process_event_exit
2021-03-31 13:13:17 -02:30

126 lines
3.1 KiB
JavaScript

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