3

I have two folders and I just want to merge them (overwriting BaseFolder files with ExtendedFolder ones when present)

Example:

BaseFolder ---main.js ---test.js

ExtendedFolder ---main.js

Result expected:

ResultFolder ---main.js (from ExtendedFolder) ---test.js (from BaseFolder)

I similar question has been asked but without a satisfying answer: Grunt. Programmatically merge corresponding files in parallel folders with concat

Community
  • 1
  • 1
Shprink
  • 780
  • 8
  • 16

2 Answers2

1

EDIT: I just realized my previous answer wouldn't work. I am posting new code.

If you just have two specified folders and want to merge their content, it would be pretty straightforward with gulp (this assumes that the folder names are known before and don't change):

var gulp = require('gulp');

gulp.task('moveBase',function(){
    return gulp.src('BaseFolder/*')
        .pipe(gulp.dest('ResultFolder/'));
});
gulp.task('moveExtended',['moveBase'],function(){
    return gulp.src('ExtendedFolder/*')
        .pipe(gulp.dest('ResultFolder/'));
});
gulp.task('mergeFolders',['moveBase','moveExtended']);

The files in BaseFolder having the same names as files in ExtendedFolder get overwritten. The key here is the order of copying. First, copy the folder whose files should be overwritten in case of a conflict. You can split the copying into two tasks and take advantage of the dependency system - task moveExtended depends on moveBase which ensures that it will be copied later.

mz8i
  • 672
  • 1
  • 7
  • 11
  • Thanks for the answer, this could work, but I wanted to be able to stream the merged files without having to dump the result in a temp folder. The answer I gave using event-stream works fine :) Cheers – Shprink Jul 08 '14 at 16:41
1

I was able to do exactly what I wanted using https://www.npmjs.org/package/event-stream

var gulp = require('gulp')
        , es = require('event-stream');

gulp.task('web_dev', function () {
    es.merge(gulp.src('./BaseFolder/**/*')
            , gulp.src('./ExtendedFolder/**/*'))
            .pipe(gulp.dest('out'));
});
Shprink
  • 780
  • 8
  • 16
  • This doesn't work for me. Regardless of how I order the sourced directories, the files written to `out` seem to come from `BaseFolder`. The documentation for `merge` says that there is no ordering for the data that is emitted from it, and so there's no way to ensure that the files from `ExtendedFolder` will overwrite the files from `BaseFolder`. While not performing the overwrites in-stream, @matt.kovasky's answer works for me. – Ziewvater Jun 24 '15 at 23:23