如果 gulp 任务失败,如何向 Appveyor 发送失败信号?

How to send fail signal to Appveyor if gulp task fails?

我为一个 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))