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 */);
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:
Add an option for
base
to yourgulp.src
like so:This will tell gulp to preserve the entire relative path. Then pass
'./'
intogulp.dest()
to overwrite the original files. (Note: this is untested, you should make sure you have a backup in case it doesn't work.)Use functions. Gulp's just JavaScript, so you can do this:
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 likeif you are using
gulp-rename
, here's another workaround:and if you want source and target to be absolute paths,
Tell gulp to write to the base directory of the file in question, just like so:
(The data argument is a vinyl file object)
The advantage of this approach is that if your have files from multiple sources each nested at different levels of the file structure, this approach allows you to overwrite each file correctly. (As apposed to set one base directory in the upstream of your pipe chain)