Gulp 吞咽任务排序
我已经问了这个问题的一个简化版本,但是答案对我的大问题不起作用,所以我将在这里发布整个问题,看看是否有任何结果 项目结构 我有很多项目要处理,它们都有这样的结构(更多的子目录/子级别,但你知道了): 启动新项目时,我想将Gulp 吞咽任务排序,gulp,Gulp,我已经问了这个问题的一个简化版本,但是答案对我的大问题不起作用,所以我将在这里发布整个问题,看看是否有任何结果 项目结构 我有很多项目要处理,它们都有这样的结构(更多的子目录/子级别,但你知道了): 启动新项目时,我想将build的内容复制到一个新目录,然后运行一个任务,将EN的所有实例在文件/目录名称和文件内容中都更改为FR 复制并重命名 我写的任务做得很好。像这样的调用gulp copy dirs--match EN--replacement FR指定哪些字符串应该替换什么,并且都是好的,但
build
的内容复制到一个新目录,然后运行一个任务,将EN
的所有实例在文件/目录名称和文件内容中都更改为FR
复制并重命名
我写的任务做得很好。像这样的调用gulp copy dirs--match EN--replacement FR
指定哪些字符串应该替换什么,并且都是好的,但将原始EN
文件与新的FR
文件一起留给我:
var gulp = require("gulp");
var foreach = require("gulp-foreach");
var args = require("yargs").argv;
var rename = require("gulp-rename");
var replace = require("gulp-replace-task");
gulp.task("copy-dirs", function(){
return gulp.src("./build/*"+args.match+"*")//Original parent dirs to copy
.pipe(foreach(function(pDirStream, pDirFile){//Loop parent dirs
var newDirName = pDirFile.relative.replace(args.match, args.replacement);//Name of parent dir with replacement applied
gulp.src("./build/"+pDirFile.relative+"/**/*")//Get contents of dir
.pipe(foreach(function(childStream, childFile){//Loop child files
var newFileName = childFile.relative.replace(args.match, args.replacement);//Name of child with replacement applied
childStream.pipe(replace({usePrefix: false, patterns:[
{
match: args.match,
replacement: args.replacement
}
]}))
.pipe(rename(newFileName));
return childStream;
}))
.pipe(gulp.dest("./build/"+newDirName));//Save it to new location
return pDirStream;
}));
});
清理
因此,我编写了第二个小任务来清理,如果我用gulpclean--match EN
调用它,它会清除原始的EN
文件:
var del = require("del");
gulp.task("clean", function(){
return del(["./build/*"+args.match+"*/**"]);
});
问题
轰!完成。除非只有在运行复制目录
然后手动运行清理
时,此选项才有效。我想做的是按如下顺序排列它们:
gulp.task("project-rename", ["clean"]);
gulp.task("clean", ["copy-dirs"], function(){
return del(["./build/*"+args.match+"*/**"]);
});
…但当我尝试此操作并调用gulp project rename--match EN--replacement FR
时,我得到错误:enoint,lstat
跟随一个指向资产的文件路径,并留下一个半复制/删除项目的选择
黑客
我在copy dirs
结束时暂停了一秒钟,试图解决问题,但这感觉真的很糟糕,因此我想问:
- 我的方法明智吗
- 为什么我的
任务在尝试按顺序运行时失败clean
- 我该如何解决这个问题
你在沮丧的绝望中:)你正在尝试做Gulp及其插件应该为你做的事情 我将以下解决方案建立在你对你想要做什么的散文描述上,而不是你展示的代码上,因此它的行为可能会有点不同。我已经对代码进行了注释,解释了它的功能。此外,我还使用一个文件树对其进行了测试,该文件树复制了您在问题中显示的内容,并发现它可以工作。有一件事我会做得不同,但您似乎想要的是,我不想要一个
gulpfile.js
将其初始输入和最终输出写入同一目录。这对我来说似乎很危险
var gulp = require("gulp");
var args = require("yargs").argv;
var rename = require("gulp-rename");
var replace = require("gulp-replace-task");
var del = require("del");
var gulpFilter = require('gulp-filter');
var match = args.match;
var replacement = args.replacement;
var match_re = RegExp(match, "g");
gulp.task("copy-dirs", function () {
// Set a filter that we'll use later. We have `restore: true` so
// that we can restore the stream to its original set of files
// after we are done with the filter.
var filter = gulpFilter("**/*.txt", {restore: true});
// We match all descendants of any directory in ./build that has
// the string specified by `match` in it.
return gulp.src("./build/*" + match + "*/**")
// In file paths every instance of the string contained by the
// `match` option will be replaced with the string specified by
// the `replacement` option.
.pipe(rename(function (path) {
path.dirname = path.dirname.replace(match_re, replacement);
path.basename = path.basename.replace(match_re, replacement);
// If you really want to change extensions, you'll also
// have to process path.ext.
}))
// Filter it down to only the files for which text replacement
// makes sense.
.pipe(filter)
.pipe(replace({usePrefix: false, patterns:[
{
match: match,
replacement: replacement
}
]}))
// Go back to the whole set of files so that we can store them.
.pipe(filter.restore)
.pipe(gulp.dest("./build"));
});
gulp.task("clean", ["copy-dirs"], function(){
return del(["./build/*"+match+"*/**"]);
});
gulp.task("project-rename", ["clean"]);
这太棒了!它不仅解决了我的问题,而且还为我以前编写的一些其他任务提供了一些指导。我怀疑我一直在看有点偏斜的大口啤酒,这真的帮助了我的理解。再次感谢你花时间写了一个非常好的答案!关于您关于写入源目录的评论-我可以理解为什么这可能是危险的,但这仅仅是因为覆盖了文件还是因为它实际上可能会破坏任务本身?
var gulp = require("gulp");
var args = require("yargs").argv;
var rename = require("gulp-rename");
var replace = require("gulp-replace-task");
var del = require("del");
var gulpFilter = require('gulp-filter');
var match = args.match;
var replacement = args.replacement;
var match_re = RegExp(match, "g");
gulp.task("copy-dirs", function () {
// Set a filter that we'll use later. We have `restore: true` so
// that we can restore the stream to its original set of files
// after we are done with the filter.
var filter = gulpFilter("**/*.txt", {restore: true});
// We match all descendants of any directory in ./build that has
// the string specified by `match` in it.
return gulp.src("./build/*" + match + "*/**")
// In file paths every instance of the string contained by the
// `match` option will be replaced with the string specified by
// the `replacement` option.
.pipe(rename(function (path) {
path.dirname = path.dirname.replace(match_re, replacement);
path.basename = path.basename.replace(match_re, replacement);
// If you really want to change extensions, you'll also
// have to process path.ext.
}))
// Filter it down to only the files for which text replacement
// makes sense.
.pipe(filter)
.pipe(replace({usePrefix: false, patterns:[
{
match: match,
replacement: replacement
}
]}))
// Go back to the whole set of files so that we can store them.
.pipe(filter.restore)
.pipe(gulp.dest("./build"));
});
gulp.task("clean", ["copy-dirs"], function(){
return del(["./build/*"+match+"*/**"]);
});
gulp.task("project-rename", ["clean"]);