-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure npm scripts (via Gulp) emit errors #3033
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,35 @@ export async function npmScript (name, args = []) { | |
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm' | ||
|
||
return new Promise((resolve, reject) => { | ||
const script = spawn(command, ['run', name, ...args]) | ||
const script = spawn(command, ['run', name, '--silent', ...args]) | ||
|
||
// Send output to console | ||
script.stdout.on('data', (data) => console.log(data.toString())) | ||
script.stderr.on('data', (data) => console.error(data.toString())) | ||
|
||
// Emit errors to error listener | ||
script.stderr.on('data', (data) => { | ||
script.emit('error', new Error(data.toString())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By emitting an error here it ensures we hit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Won't that trigger multiple times if a script outputs to
question: Why are we emitting an error rather than directly rejecting? That feels an unnecessary extra step there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's confusing, probably why we wanted to remove Gulp 😭 Gulp sources are Node.js They extend Node.js task.emit('end') // Gulp piped task has finished processing
stream.emit('error') // Gulp stream has encountered an error If we only By emitting events we can ensure the Gulp runner stays in control (logs the error and exits) but it also lets us listen via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The first error written will hit |
||
}) | ||
|
||
// Reject on actual script errors to exit `gulp dev` | ||
script.on('error', reject) | ||
|
||
// Resolve all exit codes to continue `gulp dev` | ||
script.on('close', resolve) | ||
// Check for exit codes or continue `gulp dev` | ||
script.on('close', (code) => { | ||
let error | ||
|
||
// Closed with errors | ||
if (code > 0) { | ||
error = new Error(`Task for npm script '${name}' exit code ${code}`) | ||
|
||
// Hide error info (already written to `stderr`) | ||
error.showProperties = false | ||
error.showStack = false | ||
} | ||
|
||
// Reject on errors | ||
error ? reject(error) : resolve(code) | ||
}) | ||
}) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still add
--silent
behind the scenes because we don't want both npm AND Gulp logging progress to the console—one CLI tool is enoughNote: Output from the npm task (linting for example) is not silenced