1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/development/build/task.js
Erik Marks 413700afc7
Rationalize build system arguments (#12047)
This rationalizes how arguments are passed to and parsed by the build system. To accomplish this, everything that isn't an environment variable from `.metamaskrc` or our CI environment is now passed as an argument on the command line.

Of such arguments, the `entryTask` is still expected as a positional argument in the first position (i.e. `process.argv[2]`), but everything else must be passed as a named argument. We use `minimist` to parse the arguments, and set defaults to preserve existing behavior.

Arguments are parsed in a new function, `parseArgv`, in `development/build/index.js`. They are assigned to environment variables where convenient, and otherwise returned from `parseArgv` to be passed to other functions invoked in the same file.

This change is motivated by our previous inconsistent handling of arguments to the build system, which will grow increasingly problematic as the build system grows in complexity. (Which it will very shortly, as we introduce Flask builds.)

Miscellaneous changes:
- Adds a build system readme at `development/build/README.md`
- Removes the `beta` package script. Now, we can instead call: `yarn dist --build-type beta`
- Fixes the casing of some log messages and reorders some parameters in the build system
2021-09-09 12:44:57 -07:00

135 lines
3.3 KiB
JavaScript

const EventEmitter = require('events');
const spawn = require('cross-spawn');
const tasks = {};
const taskEvents = new EventEmitter();
module.exports = {
tasks,
taskEvents,
createTask,
runTask,
composeSeries,
composeParallel,
runInChildProcess,
};
const { setupTaskDisplay } = require('./display');
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, { buildType, isLavaMoat }) {
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 () => {
let childProcess;
// Use the same build type for subprocesses, and only run them in LavaMoat
// if the parent process also ran in LavaMoat.
if (isLavaMoat) {
childProcess = spawn(
'yarn',
['build', taskName, '--build-type', buildType, '--skip-stats'],
{
env: process.env,
},
);
} else {
childProcess = spawn(
'yarn',
['build:dev', taskName, '--build-type', buildType, '--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()));
};
}