Gulp 可以覆盖所有的 src 文件吗? [英] Can Gulp overwrite all src files?
问题描述
假设我想替换一堆文件中的版本号,其中许多文件位于子目录中.我将通过 gulp-replace 管道文件来运行 regex-replace 函数;但我最终会想要覆盖所有原始文件.
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.
任务可能如下所示:
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:
将
base
的选项添加到您的gulp.src
中,如下所示:
Add an option for
base
to yourgulp.src
like so:
gulp.src([...files...], {base: './'}).pipe(...)...
这将告诉 gulp 保留整个相对路径.然后将 './'
传入 gulp.dest()
以覆盖原始文件.(注意:这是未经测试的,你应该确保你有一个备份,以防它不起作用.)
This will tell gulp to preserve the entire relative path. Then 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.)
使用函数.Gulp 只是 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)));
}
如果您需要异步运行这些,第一个会容易得多,因为您需要使用 event-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);
这篇关于Gulp 可以覆盖所有的 src 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!