Skip to content

Commit

Permalink
Add ignoreJunk option
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Andrei committed Aug 4, 2019
1 parent 5637c16 commit d96b460
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 67 deletions.
16 changes: 10 additions & 6 deletions index.d.ts
@@ -1,5 +1,5 @@
import {GlobbyOptions} from 'globby';
import {Options as CpFileOptions} from 'cp-file';
import { GlobbyOptions } from 'globby';
import { Options as CpFileOptions } from 'cp-file';

declare namespace cpy {
interface Options extends Readonly<GlobbyOptions>, CpFileOptions {
Expand Down Expand Up @@ -39,6 +39,13 @@ declare namespace cpy {
@default (os.cpus().length || 1) * 2
*/
readonly concurrency?: number;

/**
Ignore junk files.
@default true
*/
readonly ignoreJunk?: boolean;
}

interface ProgressData {
Expand All @@ -64,10 +71,7 @@ declare namespace cpy {
}

interface ProgressEmitter {
on(
event: 'progress',
handler: (progress: ProgressData) => void
): Promise<string[]>;
on(event: 'progress', handler: (progress: ProgressData) => void): Promise<string[]>;
}
}

Expand Down
10 changes: 10 additions & 0 deletions index.js
Expand Up @@ -6,8 +6,13 @@ const pAll = require('p-all');
const arrify = require('arrify');
const globby = require('globby');
const cpFile = require('cp-file');
const junk = require('junk');
const CpyError = require('./cpy-error');

const defaultOptions = {
ignoreJunk: true
};

const preprocessSourcePath = (source, options) => options.cwd ? path.resolve(options.cwd, source) : source;

const preprocessDestinationPath = (source, destination, options) => {
Expand Down Expand Up @@ -36,6 +41,7 @@ module.exports = (source, destination, {
...options
} = {}) => {
const progressEmitter = new EventEmitter();
options = {...defaultOptions, ...options};

const promise = (async () => {
source = arrify(source);
Expand All @@ -51,6 +57,10 @@ module.exports = (source, destination, {
let files;
try {
files = await globby(source, options);

if (options.ignoreJunk) {
files = files.filter(file => junk.not(path.basename(file)));
}
} catch (error) {
throw new CpyError(`Cannot glob \`${source}\`: ${error.message}`, error);
}
Expand Down
113 changes: 57 additions & 56 deletions package.json
@@ -1,58 +1,59 @@
{
"name": "cpy",
"version": "7.3.0",
"description": "Copy files",
"license": "MIT",
"repository": "sindresorhus/cpy",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"cpy-error.js",
"index.js",
"index.d.ts"
],
"keywords": [
"copy",
"cp",
"cpy",
"file",
"files",
"clone",
"fs",
"stream",
"glob",
"file-system",
"ncp",
"fast",
"quick",
"data",
"content",
"contents",
"cpx",
"directory",
"directories"
],
"dependencies": {
"arrify": "^2.0.1",
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"nested-error-stacks": "^2.1.0",
"p-all": "^2.1.0"
},
"devDependencies": {
"ava": "^2.1.0",
"rimraf": "^2.6.3",
"tempfile": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
"name": "cpy",
"version": "7.3.0",
"description": "Copy files",
"license": "MIT",
"repository": "sindresorhus/cpy",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"cpy-error.js",
"index.js",
"index.d.ts"
],
"keywords": [
"copy",
"cp",
"cpy",
"file",
"files",
"clone",
"fs",
"stream",
"glob",
"file-system",
"ncp",
"fast",
"quick",
"data",
"content",
"contents",
"cpx",
"directory",
"directories"
],
"dependencies": {
"arrify": "^2.0.1",
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"nested-error-stacks": "^2.1.0",
"p-all": "^2.1.0",
"junk": "^3.1.0"
},
"devDependencies": {
"ava": "^2.1.0",
"rimraf": "^2.6.3",
"tempfile": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
11 changes: 6 additions & 5 deletions readme.md
Expand Up @@ -2,7 +2,6 @@

> Copy files

## Why

- Fast by using streams.
Expand All @@ -11,14 +10,12 @@
- User-friendly error messages.
- Progress reporting.


## Install

```
$ npm install cpy
```


## Usage

```js
Expand All @@ -30,7 +27,6 @@ const cpy = require('cpy');
})();
```


## API

### cpy(source, destination, options?)
Expand Down Expand Up @@ -101,6 +97,12 @@ Default: `(os.cpus().length || 1) * 2`

Number of files being copied concurrently.

##### ignoreJunk

Type: `boolean`<br>
Default: `true`

Ignores [junk](https://github.com/sindresorhus/junk) files.

## Progress reporting

Expand Down Expand Up @@ -133,7 +135,6 @@ Note that the `.on()` method is available only right after the initial `cpy` cal
})();
```


## Related

- [cpy-cli](https://github.com/sindresorhus/cpy-cli) - CLI for this module
Expand Down
60 changes: 60 additions & 0 deletions test.js
Expand Up @@ -157,6 +157,66 @@ test('reports copy progress of no files', async t => {
t.is(report.percent, 1);
});

test('junk files are ignored', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');

let report;

await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd'), ignoreJunk: true})
.on('progress', event => {
report = event;
});

t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
});

test('junk files are copied', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');

let report;

await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd'), ignoreJunk: false})
.on('progress', event => {
report = event;
});

t.not(report, undefined);
t.is(report.totalFiles, 2);
t.is(report.completedFiles, 2);
t.is(report.completedSize, 22);
t.is(report.percent, 1);
});

test('nested junk files are ignored', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ispum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/test'), 'lorem ispum');

let report;

await cpy(['cwd/Thumbs.db', 'cwd/test'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true})
.on('progress', event => {
report = event;
});

t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
});

test('reports copy progress of single file', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
Expand Down

0 comments on commit d96b460

Please sign in to comment.