ncp icon indicating copy to clipboard operation
ncp copied to clipboard

Filter is not working

Open Exocomp opened this issue 6 years ago • 4 comments

Here is my example, I've set it up based on the documentation but can't get it to work. In this example index.html from folder1 should be copied to folder2.

node -v v8.9.4

var options = {};
options.filter = new RegExp('index.html');

ncp('folder1', '../folder2', options, 
    function (err) {
        if (err) {
            return console.error(err);
        }
        console.log('done!');
    }
);

Exocomp avatar Mar 20 '18 02:03 Exocomp

This happens because ncp also applies the filter to directories. So, in your example, the filter will first be applied to <abs-path-to>/folder1. Since this path doesn't contain the string index.html processing stops and nothing is copied.

It would be nice to either change the behaviour of the filter to not apply to directories or to add another option (say, fileFilter) which is only applied to the basename of file paths.

pgraham avatar Apr 13 '18 15:04 pgraham

So what is the solution here? When you add folder name to the regexp it will copy all files within. Any way to specify folder as a target and filter out certain files only?

Melmoth-the-Wanderer avatar Apr 19 '19 11:04 Melmoth-the-Wanderer

well i managed to stumble upon the same issue and my take on that was like in answer to SO: node ncp filter not working

hope it comes useful to someone

zawor avatar Jan 13 '20 08:01 zawor

Looking at the source code it seems like the filter function takes in a function too, I am using it like this to copy only .css files

    ncp(workDir, outputPath, {filter: (source) => {
        if (fs.lstatSync(source).isDirectory()) {
            return true;
        } else {
            return source.match(/.*css/) != null;
        }
    }}, function (err) {
        if (err) {
            return console.error(err);
        }
    });

MrMjauh avatar Mar 23 '20 12:03 MrMjauh