AvianFlu / ncp

Asynchronous recursive file copying with Node.js.
MIT License
680 stars 103 forks source link

Filter #74

Open CSester opened 9 years ago

CSester commented 9 years ago

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

mummybot commented 8 years ago

+1 I'm confused as well!

mummybot commented 8 years ago

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.

mummybot commented 8 years ago

With the power of regex, you can solve this issue:

/^[A-Za-z\-\/]*(\.php|$)/

This will match:

But won't match:

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!