Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter is not working #130

Open
Exocomp opened this issue Mar 20, 2018 · 4 comments
Open

Filter is not working #130

Exocomp opened this issue Mar 20, 2018 · 4 comments

Comments

@Exocomp
Copy link

Exocomp commented Mar 20, 2018

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!');
    }
);
@pgraham
Copy link

pgraham commented Apr 13, 2018

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.

@Melmoth-the-Wanderer
Copy link

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?

@zawor
Copy link

zawor commented Jan 13, 2020

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

@MrMjauh
Copy link

MrMjauh commented Mar 23, 2020

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);
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants