我已经为一个Web应用程序的前端创建了一个非常复杂的构建过程,该过程正在Appveyor上进行测试。如果应用程序的某些部分无法使用gulp正确构建,如果某些gulp任务失败,我如何向Appveyor发出构建完全失败的信号?
为了解决此问题,我使用了this article中的说明。我需要将构建过程分为两个相似的部分:一个用于开发环境,另一个用于生产环境。主要区别在于,如果在某些任务中发现错误,生产环境应始终中断。 Feodor Fitsner建议该过程以错误代码非零退出。
结合这两个解决方案,我创建了这个小JS模块,该模块应该用作gulp任务的包装器:
const msg = require('bit-message-box')
const chalk = require('chalk')
module.exports = (taskFn, production = false) => function(done) {
let onSuccess = () => {
done()
}
let onError = (err) => {
if (production) {
// If build process is initiated in production env, it should always break
// on error with exit code higher than zero. This is especially important
// for Appveyor CI
msg.error(`ERROR! BUILD PROCESS ABORTED!`)
console.error(chalk.bgRed.white(err))
process.exit(1)
}
else { done() }
}
let outStream = taskFn(onSuccess, onError);
if (outStream && typeof outStream.on === 'function') {
outStream.on('end', onSuccess);
}
}
然后在gulp本身中,您可以导入该模块并以以下方式使用它:
const gulp = require('gulp')
const handleCI = require('./handleCI')
const sass = require('gulp-sass')
const PRODUCTION = true // use your own system to decide if this is true or false
gulp.task('styles', handleCI((success, error) => {
return gulp.src('./scss/style.scss')
.pipe(
sass()
.on('error', error) // Add this to handle errors
)
.pipe(
gulp.dest('./styles/')
.on('error', error)
)
}, PRODUCTION))