From 8008fe673965f77d98ae25f4c14f14b559837b94 Mon Sep 17 00:00:00 2001 From: Daniel Andrei Date: Mon, 5 Aug 2019 02:45:17 +0300 Subject: [PATCH] Add `ignoreJunk` option (#67) --- index.d.ts | 7 ++++++ index.js | 10 +++++++++ package.json | 3 ++- readme.md | 6 ++++++ test.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index f4e8c88..4abaa41 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/index.js b/index.js index 15ce53c..638bc51 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -36,6 +41,7 @@ module.exports = (source, destination, { ...options } = {}) => { const progressEmitter = new EventEmitter(); + options = {...defaultOptions, ...options}; const promise = (async () => { source = arrify(source); @@ -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); } diff --git a/package.json b/package.json index 482fe6a..53a8afe 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index 9d15f63..bf2a8c1 100644 --- a/readme.md +++ b/readme.md @@ -101,6 +101,12 @@ Default: `(os.cpus().length || 1) * 2` Number of files being copied concurrently. +##### ignoreJunk + +Type: `boolean`
+Default: `true` + +Ignores [junk](https://github.com/sindresorhus/junk) files. ## Progress reporting diff --git a/test.js b/test.js index dd67d15..ea0d495 100644 --- a/test.js +++ b/test.js @@ -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'));