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 = {
|
|
|
|
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');
|
2022-08-06 09:33:35 +02:00
|
|
|
const { logError } = require('./utils');
|
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);
|
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 21:44:57 +02:00
|
|
|
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
|
|
|
);
|
2022-08-06 09:33:35 +02:00
|
|
|
logError(err);
|
2021-02-04 19:15:23 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-16 05:18:28 +02:00
|
|
|
function runInChildProcess(
|
|
|
|
task,
|
2022-05-03 00:35:52 +02:00
|
|
|
{ applyLavaMoat, buildType, isLavaMoat, policyOnly, shouldLintFenceFiles },
|
2021-09-16 05:18:28 +02:00
|
|
|
) {
|
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
|
|
|
}
|
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 21:44:57 +02:00
|
|
|
|
2020-03-09 01:55:02 +01:00
|
|
|
return instrumentForTaskStats(taskName, async () => {
|
2022-05-03 00:35:52 +02:00
|
|
|
const childProcess = spawn(
|
|
|
|
'yarn',
|
|
|
|
[
|
|
|
|
// Use the same build type for subprocesses, and only run them in
|
|
|
|
// LavaMoat if the parent process also ran in LavaMoat.
|
|
|
|
isLavaMoat ? 'build' : 'build:dev',
|
|
|
|
taskName,
|
|
|
|
`--apply-lavamoat=${applyLavaMoat ? 'true' : 'false'}`,
|
|
|
|
`--build-type=${buildType}`,
|
|
|
|
`--lint-fence-files=${shouldLintFenceFiles ? 'true' : 'false'}`,
|
|
|
|
`--policyOnly=${policyOnly ? 'true' : 'false'}`,
|
|
|
|
'--skip-stats=true',
|
|
|
|
],
|
|
|
|
{
|
|
|
|
env: process.env,
|
|
|
|
},
|
|
|
|
);
|
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 21:44:57 +02: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
|
|
|
);
|
|
|
|
});
|
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 21:44:57 +02: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
|
|
|
);
|
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 21:44:57 +02:00
|
|
|
|
2020-03-09 01:55:02 +01:00
|
|
|
// await end of process
|
|
|
|
await new Promise((resolve, reject) => {
|
2021-03-31 17:43:17 +02:00
|
|
|
childProcess.once('exit', (errCode) => {
|
2020-03-09 01:55:02 +01:00
|
|
|
if (errCode !== 0) {
|
2020-11-03 00:41:28 +01:00
|
|
|
reject(
|
|
|
|
new Error(
|
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 21:44:57 +02: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
|
|
|
}
|