timkendrick/recursive-copy

Ability to recursively go through all sub directories

zizther opened this issue · 4 comments

Is it possible to recursively go through all sub directories rather than just the top folder being copied?

Also what is the best way to exclude files from being copied, rather than saying the ones to be copied.
It would be good be good to provide an array of files with directories to not include.
For example:

['.gitignore', 'readme.md', 'CONTRIBUTING.md', 'phpunit.xml', 'package.json', 'gulpfile.js', 'public/css', 'public/fonts', 'public/favicon.ico']

The copy operation should be recursive, and it should copy any subfolders etc into the target directory. If this isn't working then something's gone badly wrong... if it's not working for you, can you give me an example of what you are trying to achieve (source folder directory listing, expected output directory listing etc)?

To exclude a list of files, you can use a filter function as follows:

copy('path/to/src', 'path/to/dest', {
  filter: function(filePath) {
    var excludedFiles = ['.gitignore', 'readme.md', 'CONTRIBUTING.md', 'phpunit.xml', 'package.json', 'gulpfile.js', 'public/css', 'public/fonts', 'public/favicon.ico'];
    return excludedFiles.indexOf(filePath) === -1;
  }
);

Thanks Tim.

All files are being copied over, I wasn't sure if it recursively went through the sub folders, so that when using the filter function I could specify a file in a sub directory to be excluded.

Your exclude function works great. Thanks

No problem, glad I could help. Good luck!

FYI I've just published v2 of recursive-copy, where the glob array matching behavior is much more sensible. This means if you run npm install --save recursive-copy@2 in your project, you should now be able to do the following:

copy('path/to/src', 'path/to/dest', {
  filter: [
    '**/*',
    '!.gitignore',
    '!readme.md',
    '!CONTRIBUTING.md',
    '!phpunit.xml',
    '!package.json',
    '!gulpfile.js',
    '!public/css',
    '!public/fonts',
    '!public/favicon.ico'
  ]
});

Let me know if you end up trying this and come across problems.