我如何 "abort" 构建 gulp
how can I "abort" a gulp build
在我的 gulp 构建中,如果我的服务器单元测试失败,我想 "abort" 构建过程,但我不确定如何完成此操作。
目前,我正在使用节点的 request
模块来 运行 一些服务器端单元测试,如下所示:
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(); ????
}
});
});
=========================================== =================
**更新
在 Martin 和 Nick 的有用回复之后,我将我的构建简化为最基本的示例,我可以想出尝试这两个建议,其中:
- 使用自动传递给任务函数的回调方法中止构建,并且
- 使用承诺中止构建
这是我的更新 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"]);
=========================================== ===================
问题是,虽然这两种方法似乎 "abort" 构建,但它们都会导致 gulp 在终端 (cygwin) 中抛出 "hard error"。我想知道这是否是因为我 运行 在 windows 上进行这些测试,因为好吧,根据我有限的经验,node/gulp 似乎在 windows 上对我来说一切都坏了.
这是我收到的每项任务的输出。一、回调选项:
$ 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)
这里是 promises 选项的输出:
$ 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)
所以这些建议似乎是正确的,因为他们停止了 运行ning 的第二个任务,但由于 gulp 正在引发某种硬错误,所以似乎仍然有问题。任何人都可以为我阐明这一点吗?
一个 gulp 任务可以 return 一个表明一些异步工作正在进行的承诺。
如果您随后拒绝承诺,它将中止进一步处理。
在您当前的代码中,您没有告诉 gulp 异步工作,因此无法取消。
我不太确定我是否理解您试图通过测试完成什么 url,但是您可以使用非空调用传递到您的任务中的回调以使其中止任务一条通知:
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');
}
});
});
当调用您的回调或拒绝您的延迟时,不是传递字符串参数,而是传递 new Error('error msg')
。这将停止您的构建并为您刚刚创建的错误打印堆栈跟踪。
关于在管道中处理来自第 3 方插件的错误,我建议您阅读这篇很棒的文章: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');
之类的内容会打印出红色背景和白色文本的消息。在终端中更容易发现。
在我的 gulp 构建中,如果我的服务器单元测试失败,我想 "abort" 构建过程,但我不确定如何完成此操作。
目前,我正在使用节点的 request
模块来 运行 一些服务器端单元测试,如下所示:
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(); ????
}
});
});
=========================================== =================
**更新
在 Martin 和 Nick 的有用回复之后,我将我的构建简化为最基本的示例,我可以想出尝试这两个建议,其中:
- 使用自动传递给任务函数的回调方法中止构建,并且
- 使用承诺中止构建
这是我的更新 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"]);
=========================================== ===================
问题是,虽然这两种方法似乎 "abort" 构建,但它们都会导致 gulp 在终端 (cygwin) 中抛出 "hard error"。我想知道这是否是因为我 运行 在 windows 上进行这些测试,因为好吧,根据我有限的经验,node/gulp 似乎在 windows 上对我来说一切都坏了.
这是我收到的每项任务的输出。一、回调选项:
$ 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)
这里是 promises 选项的输出:
$ 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)
所以这些建议似乎是正确的,因为他们停止了 运行ning 的第二个任务,但由于 gulp 正在引发某种硬错误,所以似乎仍然有问题。任何人都可以为我阐明这一点吗?
一个 gulp 任务可以 return 一个表明一些异步工作正在进行的承诺。
如果您随后拒绝承诺,它将中止进一步处理。
在您当前的代码中,您没有告诉 gulp 异步工作,因此无法取消。
我不太确定我是否理解您试图通过测试完成什么 url,但是您可以使用非空调用传递到您的任务中的回调以使其中止任务一条通知:
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');
}
});
});
当调用您的回调或拒绝您的延迟时,不是传递字符串参数,而是传递 new Error('error msg')
。这将停止您的构建并为您刚刚创建的错误打印堆栈跟踪。
关于在管道中处理来自第 3 方插件的错误,我建议您阅读这篇很棒的文章: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');
之类的内容会打印出红色背景和白色文本的消息。在终端中更容易发现。