Gulp returns 0 when tasks fail

后端 未结 6 1259
庸人自扰
庸人自扰 2021-01-01 09:09

I\'m using Gulp in my small project in order to run tests and lint my code. When any of those tasks fail, Gulp always exits with return code 0. If I run jshint by hand, it e

6条回答
  •  时光说笑
    2021-01-01 09:35

    @robrich is right, you have to keep track of exit codes yourself, but there's no need for a forceful approach. The process global is an EventEmitter which you can bind your exit function to.

    var exitCode = 0
    
    gulp.task('test', function (done) {
      return require('child_process')
        .spawn('npm', ['test'], {stdio: 'pipe'})
        .on('close', function (code, signal) {
          if (code) exitCode = code
          done()
        })
    })
    
    gulp.on('err', function (err) {
      process.emit('exit') // or throw err
    })
    
    process.on('exit', function () {
      process.nextTick(function () {
        process.exit(exitCode)
      })
    })
    

提交回复
热议问题