ncp
ncp copied to clipboard
Filter is not working
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!');
}
);
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.
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?
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
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);
}
});