Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Jul 4, 2023
1 parent fd110df commit 79e2c97
Showing 1 changed file with 62 additions and 61 deletions.
123 changes: 62 additions & 61 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
})
})

Expand All @@ -205,5 +206,5 @@ module.exports = async function runTask (task, options) {
}
}

return await promise
return promise
}

0 comments on commit 79e2c97

Please sign in to comment.