2020-03-09 01:55:02 +01:00
|
|
|
const EventEmitter = require('events')
|
2020-10-20 06:07:23 +02:00
|
|
|
const spawn = require('cross-spawn')
|
2020-03-09 01:55:02 +01:00
|
|
|
|
|
|
|
const tasks = {}
|
|
|
|
const taskEvents = new EventEmitter()
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
module.exports = {
|
|
|
|
detectAndRunEntryTask,
|
|
|
|
tasks,
|
|
|
|
taskEvents,
|
|
|
|
createTask,
|
|
|
|
runTask,
|
|
|
|
composeSeries,
|
|
|
|
composeParallel,
|
|
|
|
runInChildProcess,
|
|
|
|
}
|
2020-03-09 01:55:02 +01:00
|
|
|
|
|
|
|
const { setupTaskDisplay } = require('./display')
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function detectAndRunEntryTask() {
|
2020-03-09 01:55:02 +01:00
|
|
|
// 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[3] === '--skip-stats'
|
|
|
|
|
|
|
|
runTask(taskName, { skipStats })
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
throw new Error(`MetaMask build: Unrecognized task name "${taskName}"`)
|
|
|
|
}
|
|
|
|
if (!skipStats) {
|
|
|
|
setupTaskDisplay(taskEvents)
|
|
|
|
console.log(`running task "${taskName}"...`)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await tasks[taskName]()
|
|
|
|
} catch (err) {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.error(
|
|
|
|
`MetaMask build: Encountered an error while running task "${taskName}".`,
|
|
|
|
)
|
2020-03-09 01:55:02 +01:00
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
taskEvents.emit('complete')
|
|
|
|
}
|
|
|
|
|
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`,
|
|
|
|
)
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
const task = instrumentForTaskStats(taskName, taskFn)
|
|
|
|
task.taskName = taskName
|
|
|
|
tasks[taskName] = task
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function runInChildProcess(task) {
|
2020-03-09 01:55:02 +01:00
|
|
|
const taskName = typeof task === 'string' ? task : task.taskName
|
|
|
|
if (!taskName) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`MetaMask build: runInChildProcess unable to identify task name`,
|
|
|
|
)
|
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,
|
|
|
|
})
|
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}`),
|
|
|
|
)
|
2020-03-09 01:55:02 +01:00
|
|
|
})
|
2020-11-03 00:41:28 +01:00
|
|
|
childProcess.stderr.on('data', (data) =>
|
|
|
|
process.stderr.write(`${taskName}: ${data}`),
|
|
|
|
)
|
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
|
|
|
),
|
|
|
|
)
|
2020-03-09 01:55:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function instrumentForTaskStats(taskName, asyncFn) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
|
|
|
const start = Date.now()
|
|
|
|
taskEvents.emit('start', [taskName, start])
|
|
|
|
await asyncFn()
|
|
|
|
const end = Date.now()
|
|
|
|
taskEvents.emit('end', [taskName, start, end])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function composeSeries(...subtasks) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
|
|
|
const realTasks = subtasks
|
|
|
|
for (const subtask of realTasks) {
|
|
|
|
await subtask()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function composeParallel(...subtasks) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return async () => {
|
|
|
|
const realTasks = subtasks
|
|
|
|
await Promise.all(realTasks.map((subtask) => subtask()))
|
|
|
|
}
|
|
|
|
}
|