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 #74

Open
CSester opened this issue Dec 19, 2014 · 3 comments
Open

Filter #74

CSester opened this issue Dec 19, 2014 · 3 comments

Comments

@CSester
Copy link

CSester commented Dec 19, 2014

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

bill209 added a commit to bill209/ncp that referenced this issue May 23, 2015
the cause: when a regex filter is provided as an option, the first
check is made against the path name, and if the path does not match the
regex option, then the callback function is called with no error, and
the code is exited without processing any of the files within the
folder.
this change simply ignores the first iteration through the startCopy
function, which is the path name.
@mummybot
Copy link

+1 I'm confused as well!

@mummybot
Copy link

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
Copy link

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!

pgangwani added a commit to pgangwani/ncp that referenced this issue Feb 15, 2017
this change addresses issue AvianFlu#74 - Filter, submitted by CSester.
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

2 participants