我怎样才能“中止”一口气build造

在我的gulp构build中,如果我的服务器unit testing失败,我想“中止”构build过程,但是我不知道如何完成此任务。

目前,我正在使用节点的request模块来运行一些服务器端unit testing,如下所示:

 gulp.task("run-server-tests", function(){ var serverTestUrl = "http://myurl"; // returns test results in json format request(serverTestUrl, function (error, response, body) { var responseData = JSON.parse(body); if(responseData.isSuccess){ console.log(responseData.message); // nice! continue with rest of build (js, css tasks, etc.) } else { open(serverTestUrl + "&render"); // show unit test failures // err.... gulp.abortProcessing(); ???? } }); }); 

================================================== ============

**更新

在马丁和尼克的有益回应之后,我把自己的构build剥离到了最基本的例子,我可以想出两个方面的build议,其中:

  1. 使用自动传递给任务函数的callback方法来中止构build
  2. 使用承诺放弃构build

这里是我更新的gulpfile.js:

 var gulp = require("gulp"); var plugins = require('gulp-load-plugins')(); var Q = require("q"); gulp.task("task-1-option-1", function(callback){ plugins.util.log("task 1 running"); callback("unit test failed, stop build"); // let's just assume the unit tests fail }); gulp.task("task-1-option-2", function(callback){ plugins.util.log("task 1 running"); var deferred = Q.defer(); setTimeout(function(){ deferred.reject("unit test failed, stop build"); // again, let's assume the unit tests fail }, 2000); return deferred.promise; }); gulp.task("task-2-option-1", ["task-1-option-1"], function(){ // if this runs, the build didn't abort as expected plugins.util.log("task 2 running"); }); gulp.task("task-2-option-2", ["task-1-option-2"], function(){ // if this runs, the build didn't abort as expected plugins.util.log("task 2 running"); }); // trigger option-1 (Nick's suggestion) gulp.task("option-1", ["task-1-option-1", "task-2-option-1"]); // trigger option-2 (Martin's suggestion) gulp.task("option-2", ["task-1-option-2", "task-2-option-2"]); 

================================================== ==============

问题是,虽然这两种方法似乎“中止”构build,但它们都会导致吞吐量在terminal(cygwin)中抛出一个“硬性错误”。 我想知道这是否是因为我正在Windows上运行这些testing,因为从我有限的经验来看,所有的节点/吞咽似乎都在Windows上突破。

这是我收到的每个任务的输出。 首先,callback选项:

 $ gulp option-1 [10:45:32] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js [10:45:32] Starting 'task-1-option-1'... [10:45:32] task 1 running [10:45:32] 'task-1-option-1' errored after 79 ms [10:45:32] Error: unit test failed, stop build at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at Gulp.emit (events.js:107:17) at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at cb (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3) at Gulp.<anonymous> (C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js:9:2) at module.exports (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7) at Gulp.Orchestrator._runTask (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:273:3) 

这里是承诺选项的输出:

 $ gulp option-2 [10:46:50] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js [10:46:50] Starting 'task-1-option-2'... [10:46:50] task 1 running [10:46:52] 'task-1-option-2' errored after 2.08 s [10:46:52] Error: unit test failed, stop build at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at Gulp.emit (events.js:107:17) at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:45:4 at _rejected (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:804:24) at C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:830:30 at Promise.when (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:1064:31) 

所以这些build议似乎是正确的,因为他们停止了第二项任务的运行,但是由于吞噬了一些硬性错误,似乎还是有些错误。 任何人都可以为我阐明这一点吗?

我不是很确定,我明白你要用你的testingurl来完成什么,但是你可以用一个非null来调用传递给你的任务的callback,让它通过通知中止任务:

 gulp.task("run-server-tests", function(callback) { var serverTestUrl = "http://myurl"; request(serverTestUrl, function (error, response, body) { var responseData = JSON.parse(body); if (responseData.isSuccess) { // snip... callback(); } else { open(serverTestUrl + "&render"); // show unit test failures callback('Unit test error'); } }); }); 

吞噬文件

一个吞咽任务可以返回一个承诺,表明正在发生一些asynchronous工作。

如果你拒绝承诺,它会中止进一步处理。

在你当前的代码中,你不会告诉有关asynchronous工作,所以它不能取消。

当调用你的callback或者拒绝你的延期时,不要传递一个string参数,而是传递一个new Error('error msg') 。 这将停止您的构build并打印您刚创build的错误的堆栈跟踪。

在处理任务中的pipe道时,为了处理来自第三方插件的错误,我build议你阅读这篇很棒的文章: http : //www.artandlogic.com/blog/2014/05/error-handling-in-gulp/

另外,为了获得好的彩色日志消息,请尝试在gulp-utils中使用颜色实用程序。 像plugins.util.log(plugins.util.colors.bgRed.white('unit test failed, stop build');将打印带有红色背景和白色文本的消息。在terminal中更容易发现。