Skip to content

Commit

Permalink
Add ignoreJunk option (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitecrownclown authored and sindresorhus committed Aug 4, 2019
1 parent 5637c16 commit 8008fe6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions index.d.ts
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 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
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -46,7 +46,8 @@
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"nested-error-stacks": "^2.1.0",
"p-all": "^2.1.0"
"p-all": "^2.1.0",
"junk": "^3.1.0"
},
"devDependencies": {
"ava": "^2.1.0",
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Expand Up @@ -101,6 +101,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
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 8008fe6

Please sign in to comment.