Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreJunk option #67

Merged
merged 2 commits into from Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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