davidtheclark davidtheclark - 1 year ago 556
Node.js Question

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:

/* ...and so on... */
.pipe(replace(/* ...replacement... */))
.pipe(gulp.dest(/* I DONT KNOW */);

So how can I end it so that each
file just overwrites itself, at its original location? Is there something I can pass to
that will do this?

Answer Source

I can think of two solutions:

  1. Add an option for base to your gulp.src like so:

    gulp.src([...files...], {base: './'}).pipe(...)...

    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.)

  2. Use functions. Gulp's just JavaScript, so you can do this:

    [...files...].forEach(function(file) {
        var path = require('path');

    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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download