可以咕嘟咕嘟覆盖所有的src的文件吗? [英] Can Gulp overwrite all src files?
问题描述
比方说,我要替换一堆文件,其中许多住在子目录中的版本号。我将通过管道吞掉替换运行正则表达式替换功能;但我最终想要的覆盖所有原始文件的
Let's say I want to replace the version number in a bunch of files, many of which live in subdirectories. I will pipe the files through gulp-replace to run the regex-replace function; but I will ultimately want to overwrite all the original files.
该任务可能是这个样子:
The task might look something like this:
gulp.src([
'./bower.json',
'./package.json',
'./docs/content/data.yml',
/* ...and so on... */
])
.pipe(replace(/* ...replacement... */))
.pipe(gulp.dest(/* I DONT KNOW */);
所以,我怎么能结束它让每个的src
文件只是覆盖本身,在其原来的位置?有什么我可以传递给 gulp.dest()
将做到这一点?
So how can I end it so that each src
file just overwrites itself, at its original location? Is there something I can pass to gulp.dest()
that will do this?
推荐答案
我能想到的两个解决方案:
I can think of two solutions:
-
添加一个选项,
基
你的gulp.src
像这样:
gulp.src([...files...], {base: './'}).pipe(...)...
这将告诉一饮而尽,以preserve整个相对路径。然后,只需通过./
到 gulp.dest()
来覆盖原文件。 (注:这是未经测试,你应该确保你有一个备份,以防它不工作)
This will tell gulp to preserve the entire relative path. Then simply pass './'
into gulp.dest()
to overwrite the original files. (Note: this is untested, you should make sure you have a backup in case it doesn't work.)
使用功能。咕嘟咕嘟的只是JavaScript,因此你可以这样做:
Use functions. Gulp's just JavaScript, so you can do this:
[...files...].forEach(function(file) {
var path = require('path');
gulp.src(file).pipe(rename(...)).pipe(gulp.dest(path.dirname(file)));
}
如果你需要异步运行这些,首先会容易得多,因为你需要使用像事件stream.merge
并映射到数据流数组。它看起来像
If you need to run these asynchronously, the first will be much easier, as you'll need to use something like event-stream.merge
and map the streams into an array. It would look like
var es = require('event-stream');
...
var streams = [...files...].map(function(file) {
// the same function from above, with a return
return gulp.src(file) ...
};
return es.merge.apply(es, streams);
这篇关于可以咕嘟咕嘟覆盖所有的src的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!