Skip to content

Commit

Permalink
add filter option, fixes sindresorhus#63
Browse files Browse the repository at this point in the history
  • Loading branch information
stroncium committed Jun 18, 2019
1 parent b1ca91c commit c915aab
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ declare namespace cpy {
```
*/
readonly rename?: string | ((basename: string) => string);

/**
Function to filter copied files. Return true to include, false to exclude. Can also return a Promise that resolves to true or false.
@example
```
import cpy = require('cpy');
(async () => {
await cpy('foo', 'destination', {
filter: name => !name.includes('NOCOPY')
});
})();
```
*/
readonly filter?: (basename: string) => (boolean | Promise<boolean>);
}

interface ProgressData {
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const arrify = require('arrify');
const globby = require('globby');
const cpFile = require('cp-file');
const pFilter = require('p-filter');
const CpyError = require('./cpy-error');

const preprocessSourcePath = (source, options) => options.cwd ? path.resolve(options.cwd, source) : source;
Expand Down Expand Up @@ -50,6 +51,11 @@ module.exports = (source, destination, options = {}) => {
throw new CpyError(`Cannot glob \`${source}\`: ${error.message}`, error);
}

if (options.filter !== undefined) {
const filteredFiles = await pFilter(files, options.filter, {concurrency: 1024});
files = filteredFiles;
}

if (files.length === 0) {
progressEmitter.emit('progress', {
totalFiles: 0,
Expand Down
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ expectType<Promise<string[]> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {overwrite: false})
);
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {filter: () => true})
);
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {filter: async () => true})
);

expectType<Promise<string[]>>(
cpy('foo.js', 'destination').on('progress', progress => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"arrify": "^2.0.1",
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"nested-error-stacks": "^2.1.0"
"nested-error-stacks": "^2.1.0",
"p-filter": "^2.1.0"
},
"devDependencies": {
"ava": "^2.1.0",
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,21 @@ const cpy = require('cpy');
})();
```

#### filter

Type: `string | Function`

Function to filter copied files. Return true to include, false to exclude. Can also return a Promise that resolves to true or false.

```js
const cpy = require('cpy');

(async () => {
await cpy('foo.js', 'destination', {
filter: name => !name.includes('NOCOPY')
});
})();
```
## Progress reporting

### cpy.on('progress', handler)
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ test('copy array of files', async t => {
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});

test('copy array of files with filter', async t => {
await cpy(['license', 'package.json'], t.context.tmp, {filter: name => name !== 'license'});

t.false(fs.existsSync(path.join(t.context.tmp, 'license')));
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});

test('copy array of files with async filter', async t => {
await cpy(['license', 'package.json'], t.context.tmp, {filter: async name => name !== 'license'});

t.false(fs.existsSync(path.join(t.context.tmp, 'license')));
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});

test('cwd', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
Expand Down

0 comments on commit c915aab

Please sign in to comment.