question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation for copy() options.filter function

See original GitHub issue

I’m not sure how to use the options.filter function with copy().

The docs say:

Function to filter copied files. Return true to include, false to exclude.

I need to copy a folder and sub files/folders, however I need to exclude some from the copy (I’m copying a node project folder and need to exclude the node_modules folder within). The above makes it sound like this function can used but I’m not sure how.

Can this function be used for what I need…?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:21

github_iconTop GitHub Comments

13reactions
pwang2commented, Feb 4, 2017

@stephen-last You may have the same puzzle as I had. When copying directory and use the filter to include/exclude files in subdirectory, it actually filters from the root first. If the root folder does not match the filter function, it did not go to the subdirectory at all. To make it work as expected, I use this workaround. here if (fs.lstatSync(n).isDirectory()) {return true} is needed to bypass the root folder to have chance to match sub directories and files.

src
├── bar
│   └── baz
│       └── file1.txt
└── foo
    ├── baz
    │   └── file1.txt
    └── mie.js
var fs = require('fs-extra');
fs.copy('./src', './dist', {
        clobber: true,
        filter: n => {
             if (fs.lstatSync(n).isDirectory()) {
                return true;
            }
            var result = /\/baz\//.test(n);
            console.log(result ? 'copied' : 'skipped', n);
            return result;
        }
    },
    () => console.log('done')
);

This gives output like this.

➜ test node index.js skipped /private/tmp/test/src/foo/mie.js copied /private/tmp/test/src/foo/baz/file1.txt copied /private/tmp/test/src/bar/baz/file1.txt done

9reactions
RyanZimcommented, Feb 4, 2017

Quick example:

fs.copy(src, dest, {
  filter: function (path) {
    return path.indexOf('node_modules') > -1
  }
}, callback)

We should add an example to the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array.prototype.filter() - JavaScript - MDN Web Docs
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given...
Read more >
Google Sheets FILTER function: what it is and how to use it
Learn how to use the powerful Google Sheets filter function to filter (display) your datasets based on specific criteria.
Read more >
filter(_:) | Apple Developer Documentation
Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.
Read more >
FILTER function - Google Docs Editors Help
Returns a filtered version of the source range, returning only rows or columns that meet the specified conditions. Examples Make a copy.
Read more >
Filter, Search, and LookUp functions in Power Apps
The Filter function finds records in a table that satisfy a formula. ... (Optional) In the Layout list, select different options.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found