我有以下gulp文件: -
"use strict";
const gulp = require("gulp");
const clean = require("gulp-clean");
const tap = require("gulp-tap");
const tslint = require("gulp-tslint");
const typescript = require("gulp-typescript");
const util = require("gulp-util");
const tsconfig = require("./tsconfig.json");
function timeStamp() {
let now = new Date();
let date = [now.getFullYear(), now.getMonth() + 1, now.getDate()];
let time = [now.getHours(), now.getMinutes(), now.getSeconds()];
for (let i = 0; i < 3; i++) {
if (time[i] < 10) {
time[i] = "0" + time[i];
}
if (date[i] < 10 ) {
date[i] = "0" + date[i];
}
}
return date.join("") + "-" + time.join("");
}
gulp.task("prodClean", function() {
return gulp
.src("../prod/**/*", {read: true})
.pipe(clean({force: true}))
.pipe(gulp.dest("../backup/build/prod-" + timeStamp()));
});
gulp.task("prodCompile", function() {
return gulp
.src(tsconfig.files)
.pipe(tap(function(file) {
util.log(util.colors.white("compiling ") + util.colors.yellow(file.path));
}))
.pipe(tslint({formatter: "verbose"}))
.pipe(tslint.report())
.pipe(typescript(tsconfig.compilerOptions))
.pipe(gulp.dest("../prod"));
});
let cleanEnv = function(env) {
return gulp
.src("../" + env + "/**/*", {read: true})
.pipe(clean({force: true}))
.pipe(gulp.dest("../backup/build/" + env + "-" + timeStamp()));
};
let buildEnv = function(env) {
return gulp
.src(tsconfig.files)
.pipe(tap(function(file) {
util.log(util.colors.white("compiling ") + util.colors.yellow(file.path));
}))
.pipe(tslint({formatter: "verbose"}))
.pipe(tslint.report())
.pipe(typescript(tsconfig.compilerOptions))
.pipe(gulp.dest("../" + env));
};
gulp.task("build", ["prodClean", "prodCompile"]);
gulp.task("build01", () => { cleanEnv("prod"); buildEnv("prod"); } );
任务&#39; build&#39;工作正常,但我试图重用代码,所以我可以传入一个包含构建环境的字符串。我已经设置了两个函数cleanEnv和buildEnv来执行此操作。
然而,当我运行 gulp build 时,我得到以下输出
[15:28:13] Using gulpfile ~/Desktop/projects/ExpressBase/src/gulpfile.js
[15:28:13] Starting 'prodClean'...
[15:28:13] Starting 'prodCompile'...
[15:28:13] compiling /home/mfc/Desktop/projects/ExpressBase/src/index.ts
[15:28:13] compiling /home/mfc/Desktop/projects/ExpressBase/src/index1.ts
[15:28:14] Finished 'prodClean' after 1.29 s
[15:28:14] Finished 'prodCompile' after 1.28 s
[15:28:14] Starting 'build'...
[15:28:14] Finished 'build' after 16 μs
当我运行 gulp build01 时,我得到了
[15:29:33] Using gulpfile ~/Desktop/projects/ExpressBase/src/gulpfile.js
[15:29:33] Starting 'build01'...
[15:29:33] Finished 'build01' after 14 ms
[15:29:33] compiling /home/mfc/Desktop/projects/ExpressBase/src/index.ts
[15:29:33] compiling /home/mfc/Desktop/projects/ExpressBase/src/index1.ts
虽然构建似乎正在产生正确的结果,但如何解决早期中止的gulp任务?