1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/development/build/task.js
kumavis 7686edadb0
Build system refactor (#8140)
* build - start static asset task cleanup

* build - simplify manifest tasks

* build - refactor + rename some tasks

* build - various cleanups

* manifest - fix ref from controller

* build - drop gulp for simple async tasks

* build - breakout gulpfile into multiple files

* build - rename some tasks

* build - use task fn refs instead of string names

* build - bundle all scripts first, except for contentscript

* build - improve task timeline

* deps - update lock

* build - improve task time printout

* build/scripts - remove intermediate named task

* build - use 'yarn build' for task entry points

* build - properly run tasks via runTask for timeline display

* development/announcer - fix manifest path + clean

* build - lint fix

* build - make all defined tasks possible entry points

* build/task - properly report errors during task

* ci - fix sesify/lavamoat-viz build command

* build/scripts - run each bundle in separate processes

* lint fix

* build - forward childProcess logs to console

* build/task - fix parallel/series stream end event

* build/scripts refactor contentscript+inpage into a single task

* build/static - use the fs for 150x speedup zomg

* lint fix

* build/static - fix css copy

* Update development/build/scripts.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/scripts.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/index.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* deps - remove redundant mkdirp

* deps - remove unused pumpify

* deps - remove redundant merge-deep

* deps - prefer is-stream of isstream

* deps - remove clone for lodash.cloneDeep

* clean - remove commented code

* build/static - use fs.copy + fast-glob instead of linux cp for better platform support

* build/manifest - standardize task naming

* build/display - clean - remove unused code

* bugfix - fix fs.promises import

* build - create "clean" as named task for use as entrypoint

* build/static - fix for copying dirs

* Update development/build/task.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/display.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/display.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/display.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* build - use task refs, tasks only return promises not streams, etc

* lint fi bad merge + lint

* build - one last cleanup + refactor

* build - add comments introducing file

* build/manifest - fix bug + subtasks dont beed to be named

* Update package.json

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* build/task - remove unused fn

* Update package.json

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/styles.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

* Update development/build/styles.js

Co-Authored-By: Mark Stacey <markjstacey@gmail.com>

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2020-03-09 08:55:02 +08:00

102 lines
2.9 KiB
JavaScript

const EventEmitter = require('events')
const { spawn } = require('child_process')
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[3] === '--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'])
// 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('close', (errCode) => {
if (errCode !== 0) {
reject(new Error(`MetaMask build: runInChildProcess for task "${taskName}" encountered an error`))
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()))
}
}