From 83f8806f24acd425c110e603dc3d3bccd0f06099 Mon Sep 17 00:00:00 2001 From: Colin Rotherham Date: Wed, 23 Nov 2022 13:05:22 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Don=E2=80=99t=20log=20npm=20script=20`--sil?= =?UTF-8?q?ent`=20flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We only add `--silent` as we have the Gulp runner’s “Starting” and “Finishing” output already --- gulpfile.mjs | 14 +++++++------- tasks/run.mjs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gulpfile.mjs b/gulpfile.mjs index 57c1f9c148..6119386608 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -14,8 +14,8 @@ import { npmScriptTask } from './tasks/run.mjs' * Runs JavaScript code quality checks, documentation, compilation */ gulp.task('scripts', gulp.series( - npmScriptTask('lint:js', ['--silent']), - npmScriptTask('build:jsdoc', ['--silent']), + npmScriptTask('lint:js'), + npmScriptTask('build:jsdoc'), compileJavaScripts )) @@ -24,8 +24,8 @@ gulp.task('scripts', gulp.series( * Runs Sass code quality checks, documentation, compilation */ gulp.task('styles', gulp.series( - npmScriptTask('lint:scss', ['--silent']), - npmScriptTask('build:sassdoc', ['--silent']), + npmScriptTask('lint:scss'), + npmScriptTask('build:sassdoc'), compileStylesheets )) @@ -36,8 +36,8 @@ gulp.task('styles', gulp.series( gulp.task('compile', gulp.series( compileJavaScripts, compileStylesheets, - npmScriptTask('build:jsdoc', ['--silent']), - npmScriptTask('build:sassdoc', ['--silent']) + npmScriptTask('build:jsdoc'), + npmScriptTask('build:sassdoc') )) /** @@ -48,7 +48,7 @@ gulp.task('dev', gulp.series( clean, 'compile', watch, - npmScriptTask('serve', ['--silent', '--workspace', 'app']) + npmScriptTask('serve', ['--workspace', 'app']) )) /** diff --git a/tasks/run.mjs b/tasks/run.mjs index 3be0026e7d..a0a3537f5b 100644 --- a/tasks/run.mjs +++ b/tasks/run.mjs @@ -12,7 +12,7 @@ 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]) script.stdout.on('data', (data) => console.log(data.toString())) script.stderr.on('data', (data) => console.error(data.toString())) From e3665b43b9394874da2fa8e677ff286a483e1464 Mon Sep 17 00:00:00 2001 From: Colin Rotherham Date: Wed, 23 Nov 2022 14:47:46 +0000 Subject: [PATCH 2/2] Ensure npm scripts (via Gulp) emit errors --- tasks/run.mjs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tasks/run.mjs b/tasks/run.mjs index a0a3537f5b..895402551d 100644 --- a/tasks/run.mjs +++ b/tasks/run.mjs @@ -14,14 +14,33 @@ export async function npmScript (name, args = []) { return new Promise((resolve, reject) => { 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())) + }) // 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) + }) }) }