diff --git a/lib/run-task.js b/lib/run-task.js index 18d8aab..5a12931 100644 --- a/lib/run-task.js +++ b/lib/run-task.js @@ -133,68 +133,69 @@ function cleanTaskArg (arg) { * This promise object has an extra method: `abort()`. * @private */ -module.exports = async function runTask (task, options) { +module.exports = function runTask (task, options) { let cp = null - const ansiStyles = (await ansiStylesPromise).default const promise = new Promise((resolve, reject) => { - const stdin = options.stdin - const stdout = wrapLabeling(task, options.stdout, options.labelState, ansiStyles) - const stderr = wrapLabeling(task, options.stderr, options.labelState, ansiStyles) - const stdinKind = detectStreamKind(stdin, process.stdin) - const stdoutKind = detectStreamKind(stdout, process.stdout) - const stderrKind = detectStreamKind(stderr, process.stderr) - const spawnOptions = { stdio: [stdinKind, stdoutKind, stderrKind] } - - // Print task name. - if (options.printName && stdout != null) { - stdout.write(createHeader( - task, - options.packageInfo, - options.stdout.isTTY, - ansiStyles - )) - } - - // Execute. - const npmPath = options.npmPath || path.basename(process.env.npm_execpath).startsWith('npx') // eslint-disable-line no-process-env - ? path.join(path.dirname(process.env.npm_execpath), path.basename(process.env.npm_execpath).replace('npx', 'npm')) // eslint-disable-line no-process-env - : process.env.npm_execpath // eslint-disable-line no-process-env - const npmPathIsJs = typeof npmPath === 'string' && /\.m?js/.test(path.extname(npmPath)) - const execPath = (npmPathIsJs ? process.execPath : npmPath || 'npm') - const isYarn = process.env.npm_config_user_agent && process.env.npm_config_user_agent.startsWith('yarn') // eslint-disable-line no-process-env - const spawnArgs = ['run'] - - if (npmPathIsJs) { - spawnArgs.unshift(npmPath) - } - if (!isYarn) { - Array.prototype.push.apply(spawnArgs, options.prefixOptions) - } else if (options.prefixOptions.indexOf('--silent') !== -1) { - spawnArgs.push('--silent') - } - Array.prototype.push.apply(spawnArgs, parseArgs(task).map(cleanTaskArg)) - - cp = spawn(execPath, spawnArgs, spawnOptions) - - // Piping stdio. - if (stdinKind === 'pipe') { - stdin.pipe(cp.stdin) - } - if (stdoutKind === 'pipe') { - cp.stdout.pipe(stdout, { end: false }) - } - if (stderrKind === 'pipe') { - cp.stderr.pipe(stderr, { end: false }) - } - - // Register - cp.on('error', (err) => { - cp = null - reject(err) - }) - cp.on('close', (code, signal) => { - cp = null - resolve({ task, code, signal }) + ansiStylesPromise.then(({ default: ansiStyles }) => { + const stdin = options.stdin + const stdout = wrapLabeling(task, options.stdout, options.labelState, ansiStyles) + const stderr = wrapLabeling(task, options.stderr, options.labelState, ansiStyles) + const stdinKind = detectStreamKind(stdin, process.stdin) + const stdoutKind = detectStreamKind(stdout, process.stdout) + const stderrKind = detectStreamKind(stderr, process.stderr) + const spawnOptions = { stdio: [stdinKind, stdoutKind, stderrKind] } + + // Print task name. + if (options.printName && stdout != null) { + stdout.write(createHeader( + task, + options.packageInfo, + options.stdout.isTTY, + ansiStyles + )) + } + + // Execute. + const npmPath = options.npmPath || path.basename(process.env.npm_execpath).startsWith('npx') // eslint-disable-line no-process-env + ? path.join(path.dirname(process.env.npm_execpath), path.basename(process.env.npm_execpath).replace('npx', 'npm')) // eslint-disable-line no-process-env + : process.env.npm_execpath // eslint-disable-line no-process-env + const npmPathIsJs = typeof npmPath === 'string' && /\.m?js/.test(path.extname(npmPath)) + const execPath = (npmPathIsJs ? process.execPath : npmPath || 'npm') + const isYarn = process.env.npm_config_user_agent && process.env.npm_config_user_agent.startsWith('yarn') // eslint-disable-line no-process-env + const spawnArgs = ['run'] + + if (npmPathIsJs) { + spawnArgs.unshift(npmPath) + } + if (!isYarn) { + Array.prototype.push.apply(spawnArgs, options.prefixOptions) + } else if (options.prefixOptions.indexOf('--silent') !== -1) { + spawnArgs.push('--silent') + } + Array.prototype.push.apply(spawnArgs, parseArgs(task).map(cleanTaskArg)) + + cp = spawn(execPath, spawnArgs, spawnOptions) + + // Piping stdio. + if (stdinKind === 'pipe') { + stdin.pipe(cp.stdin) + } + if (stdoutKind === 'pipe') { + cp.stdout.pipe(stdout, { end: false }) + } + if (stderrKind === 'pipe') { + cp.stderr.pipe(stderr, { end: false }) + } + + // Register + cp.on('error', (err) => { + cp = null + reject(err) + }) + cp.on('close', (code, signal) => { + cp = null + resolve({ task, code, signal }) + }) }) }) @@ -205,5 +206,5 @@ module.exports = async function runTask (task, options) { } } - return await promise + return promise }