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 Jun 24, 2019
1 parent b1ca91c commit cdf57cb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const arrify = require('arrify');
const globby = require('globby');
const cpFile = require('cp-file');
const junk = require('junk');
const CpyError = require('./cpy-error');

const preprocessSourcePath = (source, options) => options.cwd ? path.resolve(options.cwd, source) : source;
Expand Down Expand Up @@ -46,6 +47,10 @@ module.exports = (source, destination, options = {}) => {
let files;
try {
files = await globby(source, options);

if (options.ignoreJunk) {
files = files.filter(file => junk.not(file.substring(file.lastIndexOf('/') + 1, file.length)));
}
} catch (error) {
throw new CpyError(`Cannot glob \`${source}\`: ${error.message}`, error);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"arrify": "^2.0.1",
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"junk": "^3.1.0",
"nested-error-stacks": "^2.1.0"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Expand Up @@ -94,6 +94,12 @@ const cpy = require('cpy');
})();
```

##### ignoreJunk

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

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

## Progress reporting

Expand Down
60 changes: 60 additions & 0 deletions test.js
Expand Up @@ -152,6 +152,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')})
.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 cdf57cb

Please sign in to comment.