Yahya Uddin
Yahya Uddin

Reputation: 28861

Gulp: Recursively copy a file to every sub-directory

I have a file called foo and I want to copy it to every sub-directory.

For example if the current directory structure is:

- files
    - A/
    - B/
    - C/
    - D/
        - D1/
        - D2/

Then after the operation, it should be:

-files
    - foo
    - A/
        - foo
    - B/
        - foo
    - C/
        - foo
    - D/
        - foo
        - D1/
            - foo
        - D2/
            - foo

How can I do this using Gulp

Note that I do not know what the sub-directories will be before hand, so it needs to be done dynamically, and the paths cannot be hard-coded.

Upvotes: 1

Views: 66

Answers (1)

Zwei
Zwei

Reputation: 1279

You can accomplish this using the gulp-multi-dest and glob packages:

const gulp = require('gulp'),
    multiDest = require('gulp-multi-dest'),
    glob = require('glob');

function copyToAll(done) {
    glob('files/**/', (err, matches) => {
        if (err) {
            console.log('Error', err);
        } else {
            gulp.src('files/foo').pipe(multiDest(matches));
        }
        done();
    });
}

exports.default = copyToAll;

Upvotes: 1

Related Questions