From c4014c0e79abbba7e8ce9c205dbeeab98221d347 Mon Sep 17 00:00:00 2001 From: Daniel Andrei Date: Sun, 22 Sep 2019 10:17:55 +0300 Subject: [PATCH] Throw if the given source file does not exist (#68) Co-authored-by: Sindre Sorhus --- index.d.ts | 2 +- index.js | 12 +++++------- package.json | 5 +++-- readme.md | 2 ++ test.js | 39 ++++++++++++++++++++++++++------------- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4abaa41..a1ecdc6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -81,7 +81,7 @@ declare namespace cpy { /** Copy files. -@param source - Files to copy. +@param source - Files to copy. If any of the files do not exist, an error will be thrown (does not apply to globs). @param destination - Destination directory. @param options - In addition to the options defined here, options are passed to [globby](https://github.com/sindresorhus/globby#options). diff --git a/index.js b/index.js index efb1273..202440c 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const os = require('os'); const pAll = require('p-all'); const arrify = require('arrify'); const globby = require('globby'); +const isGlob = require('is-glob'); const cpFile = require('cp-file'); const junk = require('junk'); const CpyError = require('./cpy-error'); @@ -65,13 +66,10 @@ module.exports = (source, destination, { throw new CpyError(`Cannot glob \`${source}\`: ${error.message}`, error); } - if (files.length === 0) { - progressEmitter.emit('progress', { - totalFiles: 0, - percent: 1, - completedFiles: 0, - completedSize: 0 - }); + const sourcePaths = source.filter(value => !isGlob(value)); + + if (files.length === 0 || (sourcePaths.length > 0 && !sourcePaths.every(value => files.includes(value)))) { + throw new CpyError(`Cannot copy \`${source}\`: the file doesn't exist`); } const fileProgressHandler = event => { diff --git a/package.json b/package.json index 53a8afe..b8e33d3 100644 --- a/package.json +++ b/package.json @@ -45,9 +45,10 @@ "arrify": "^2.0.1", "cp-file": "^7.0.0", "globby": "^9.2.0", + "is-glob": "^4.0.1", + "junk": "^3.1.0", "nested-error-stacks": "^2.1.0", - "p-all": "^2.1.0", - "junk": "^3.1.0" + "p-all": "^2.1.0" }, "devDependencies": { "ava": "^2.1.0", diff --git a/readme.md b/readme.md index bf2a8c1..4d513d1 100644 --- a/readme.md +++ b/readme.md @@ -43,6 +43,8 @@ Type: `string | string[]` Files to copy. +If any of the files do not exist, an error will be thrown (does not apply to globs). + #### destination Type: `string` diff --git a/test.js b/test.js index ea0d495..f61ed75 100644 --- a/test.js +++ b/test.js @@ -140,21 +140,34 @@ test('glob errors are CpyErrors', async t => { t.true(error instanceof CpyError); }); -test('reports copy progress of no files', async t => { +test('throws on non-existing file', async t => { fs.mkdirSync(t.context.tmp); - fs.mkdirSync(path.join(t.context.tmp, 'cwd')); - let report; - await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')}) - .on('progress', event => { - report = event; - }); + await t.throwsAsync(cpy(['no-file'], t.context.tmp), { + instanceOf: CpyError + }); +}); - t.not(report, undefined); - t.is(report.totalFiles, 0); - t.is(report.completedFiles, 0); - t.is(report.completedSize, 0); - t.is(report.percent, 1); +test('throws on multiple non-existing files', async t => { + fs.mkdirSync(t.context.tmp); + + await t.throwsAsync(cpy(['no-file1', 'no-file2'], t.context.tmp), { + instanceOf: CpyError + }); +}); + +test('does not throw when not matching any file on glob pattern', async t => { + fs.mkdirSync(t.context.tmp); + + await t.notThrowsAsync(cpy(['*.js'], t.context.tmp)); +}); + +test('throws on mixed path and glob if path does not exist', async t => { + fs.mkdirSync(t.context.tmp); + + await t.throwsAsync(cpy(['*', 'no-file'], t.context.tmp), { + instanceOf: CpyError + }); }); test('junk files are ignored', async t => { @@ -205,7 +218,7 @@ test('nested junk files are ignored', async t => { let report; - await cpy(['cwd/Thumbs.db', 'cwd/test'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true}) + await cpy(['cwd/*'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true}) .on('progress', event => { report = event; });