Replies: 2 comments
-
@rootwork I missed this one (feel free to |
Beta Was this translation helpful? Give feedback.
-
Hi @rootwork and @phated , just to say iu have the exact same question, and am very much interested in those recommandations : @phated does your answer means the best practice is to just Here is my use case ( hugo is https://gohugo.io ) : import child_process from 'child_process';
gulp.task("build:hugo:prod", (done) => {
let hugo = child_process.spawn(`hugo`, [`-b`, `${hugoBaseURL}`]) // https://nodejs.org/api/child_process.html
let hugoLogger = function (buffer) {
buffer.toString()
.split(/\n/)
.forEach(function (message) {
if (message) {
gutil.log("GoHugo.io: " + ` >>>>>>>>>>>> >> {hugoBaseURL|HUGO_BASE_URL}=[${hugoBaseURL}]`);
gutil.log("GoHugo.io: " + ` >>>>>>>>>>>> ${message}`);
}
});
};
hugo.stdout.on("data", hugoLogger);
hugo.stderr.on("data", hugoLogger);
hugo.on("close", (hugoExitCode) => { // exact same pattern as described at https://nodejs.org/api/child_process.html
done(); // let gulp know the task has completed (before or after throwing an Error ?)
if ( hugoExitCode != 0 ) { // If hugo build fails, throw an error with appropriate error message
// https://github.com/gulpjs/gulp/discussions/2601#discussioncomment-2473502
let errorMessage = `An error occured during the hugo build !! Hugo process exited with exite code ${hugoExitCode}`;
gutil.log("GoHugo.io: " + ` >>>>>>>>>>>> ${errorMessage}`);
throw new Error(errorMessage)
}
}
);
}); |
Beta Was this translation helpful? Give feedback.
-
Using the recommended
pipeline()
method for Gulp tasks requires including error handling, per Node documentation. If all you do is provide console output, like this:You will usually get an additional
Error: premature close
console message from Node, and Gulp will be silent, which seems correct.However, sometimes -- it's not clear to me exactly when, but it must depend on what you're doing in the functional part of the gulp task -- doing only the above results in Gulp complaining:
The following tasks did not complete...
#2081 suggests Gulp means to set an exit code on an error -- and perhaps it's poorly-coded things on top of it that prevent it from doing so sometimes -- but it seems I can't rely on Gulp exiting on its own.
Some resources I found suggest to include an explicit call in the error handling, like this:
But in my experience that doesn't work reliably either, and in fact Node documentation recommends against it. Instead, Node docs suggest setting
process.exitCode
:which returns you to the expected
Error: premature close
console message from Node and a silent Gulp.But there's not a lot written about this that I can find, so I want to be sure I'm doing things right. What's the recommended way to tell Gulp to end the process on errors?
I realize this is almost just a question about Node itself, but because Gulp sometimes seems unable to set the exit code, I want to know what the best practice is from the Gulp side of things.
Beta Was this translation helpful? Give feedback.
All reactions