Filter
CSester opened this issue · comments
Hi, I tried to copy all json files in a folder without success.
copyJson = (source, dest, callback) ->
jsonFilter = (name) ->
return name[-5..] is '.json'
options = {filter: jsonFilter}
ncp source, dest, options, (error)->
if error
console.log "Error while copying *.json from #{source} to #{dest}"
callback()
options.filter - a RegExp instance, against which each file name is tested to determine whether to copy it or not, or a function taking single parameter: copied file name, returning true or false, determining whether to copy file or not.
function startCopy(source) {
started++;
if (filter) {
if (filter instanceof RegExp) {
if (!filter.test(source)) {
return cb(true);
}
}
else if (typeof filter === 'function') {
if (!filter(source)) {
return cb(true);
}
}
}
return getStats(source);
}
As the source folder name doesn't fit the *.json mask, content is not explored. Did I misunderstand the purpose of the filter, or should only filenames be checked, and not folder names ?
Regards
+1 I'm confused as well!
fs-extra actually uses this library for copying, but checks whether the directory exists first and creates it if not. However I don't think this will address the ultimate filter issue.
With the power of regex, you can solve this issue:
/^[A-Za-z\-\/]*(\.php|$)/
This will match:
- /Volumes/Storage/Work/TPBC/site/wp-content/themes/tpbc/src/components/header/components/search
- /Volumes/Storage/Work/TPBC/site/wp-content/themes/tpbc/src/components/header/components/search/search.php
But won't match:
- /Volumes/Storage/Work/TPBC/site/wp-content/themes/tpbc/src/components/header/components/search/search.js
Simply add additional file extensions to match to the regex or statement e.g. (\.php|\.html|$)
Running from package.json
"build:php": "ncp src build --filter=^[A-Za-z\\-\/]*\\(\\.php\\|$\\)",
It does copy all of the folders even if they are empty but hey, it works!