Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp - merge 2 folders, choose action for file having same name

Tags:

gulp

I need to copy the content of 2 folders in an another folder. If there are files having same name, and depending on the file extension, a function is executed: - concat : JS, LESS ... - merge : json - overwrite : images, HTML ...

To copy the 2 folders is not the problem, it's the second part :(

var route = 'bower_components/any-folder';
gulp.task('test', function() {
    return gulp.src([route + '**/*', 'source/**/*', route + '!bower_components{,/**}', route + '!node_modules{,/**}'])
        .pipe( $.replace( '@@_APP_@@', 'myApp' ) )
        .pipe(gulp.dest('./temp'));
});

Can someone can provide any help please.

EDIT : A diagram of my infrastructure that I would

|-gulpfile.js
|-bower_components
|   |-module
|   |   |-bower.json
|   |   |-package.json
|   |   |-index.less
|   |   |-hello.png
|-source
|   |-bower.json
|   |-package.json
|   |-index.less
|   |-hello.png

This is what I want :

|-public
|   |-bower.json (merge between bower_components/module & source)
|   |-package.json (merge between bower_components/module & source)
|   |-index.less (concat between bower_components/module & source)
|   |-hello.png (provide from source, overwrite the copy from bower_components/module)
like image 412
Bertrand Avatar asked Nov 08 '22 10:11

Bertrand


1 Answers

The case where you want to overwrite the files is the simplest, as gulp.dest overwrites by default, and gulp.src processes the specified files in order. You want the file from source to be used in preference to the one in bower_components. So something like this should work

gulp.task('overwrite', () => {
  gulp.src('./bower_components/**/*.png', './source/**/*.png')
  .pipe(gulp.dest('./temp'));
});

So if the file exists in both locations, the one from source will overwrite the one from bower_components in ./temp.

Give that a try for a start

like image 64
Mark Chorley Avatar answered Jan 04 '23 02:01

Mark Chorley