Skip to content

Commit c4014c0

Browse files
Throw if the given source file does not exist (#68)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent d93e189 commit c4014c0

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ declare namespace cpy {
8181
/**
8282
Copy files.
8383
84-
@param source - Files to copy.
84+
@param source - Files to copy. If any of the files do not exist, an error will be thrown (does not apply to globs).
8585
@param destination - Destination directory.
8686
@param options - In addition to the options defined here, options are passed to [globby](https://github.com/sindresorhus/globby#options).
8787

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os');
55
const pAll = require('p-all');
66
const arrify = require('arrify');
77
const globby = require('globby');
8+
const isGlob = require('is-glob');
89
const cpFile = require('cp-file');
910
const junk = require('junk');
1011
const CpyError = require('./cpy-error');
@@ -65,13 +66,10 @@ module.exports = (source, destination, {
6566
throw new CpyError(`Cannot glob \`${source}\`: ${error.message}`, error);
6667
}
6768

68-
if (files.length === 0) {
69-
progressEmitter.emit('progress', {
70-
totalFiles: 0,
71-
percent: 1,
72-
completedFiles: 0,
73-
completedSize: 0
74-
});
69+
const sourcePaths = source.filter(value => !isGlob(value));
70+
71+
if (files.length === 0 || (sourcePaths.length > 0 && !sourcePaths.every(value => files.includes(value)))) {
72+
throw new CpyError(`Cannot copy \`${source}\`: the file doesn't exist`);
7573
}
7674

7775
const fileProgressHandler = event => {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
"arrify": "^2.0.1",
4646
"cp-file": "^7.0.0",
4747
"globby": "^9.2.0",
48+
"is-glob": "^4.0.1",
49+
"junk": "^3.1.0",
4850
"nested-error-stacks": "^2.1.0",
49-
"p-all": "^2.1.0",
50-
"junk": "^3.1.0"
51+
"p-all": "^2.1.0"
5152
},
5253
"devDependencies": {
5354
"ava": "^2.1.0",

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Type: `string | string[]`
4343

4444
Files to copy.
4545

46+
If any of the files do not exist, an error will be thrown (does not apply to globs).
47+
4648
#### destination
4749

4850
Type: `string`

test.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,34 @@ test('glob errors are CpyErrors', async t => {
140140
t.true(error instanceof CpyError);
141141
});
142142

143-
test('reports copy progress of no files', async t => {
143+
test('throws on non-existing file', async t => {
144144
fs.mkdirSync(t.context.tmp);
145-
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
146145

147-
let report;
148-
await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
149-
.on('progress', event => {
150-
report = event;
151-
});
146+
await t.throwsAsync(cpy(['no-file'], t.context.tmp), {
147+
instanceOf: CpyError
148+
});
149+
});
152150

153-
t.not(report, undefined);
154-
t.is(report.totalFiles, 0);
155-
t.is(report.completedFiles, 0);
156-
t.is(report.completedSize, 0);
157-
t.is(report.percent, 1);
151+
test('throws on multiple non-existing files', async t => {
152+
fs.mkdirSync(t.context.tmp);
153+
154+
await t.throwsAsync(cpy(['no-file1', 'no-file2'], t.context.tmp), {
155+
instanceOf: CpyError
156+
});
157+
});
158+
159+
test('does not throw when not matching any file on glob pattern', async t => {
160+
fs.mkdirSync(t.context.tmp);
161+
162+
await t.notThrowsAsync(cpy(['*.js'], t.context.tmp));
163+
});
164+
165+
test('throws on mixed path and glob if path does not exist', async t => {
166+
fs.mkdirSync(t.context.tmp);
167+
168+
await t.throwsAsync(cpy(['*', 'no-file'], t.context.tmp), {
169+
instanceOf: CpyError
170+
});
158171
});
159172

160173
test('junk files are ignored', async t => {
@@ -205,7 +218,7 @@ test('nested junk files are ignored', async t => {
205218

206219
let report;
207220

208-
await cpy(['cwd/Thumbs.db', 'cwd/test'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true})
221+
await cpy(['cwd/*'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true})
209222
.on('progress', event => {
210223
report = event;
211224
});

0 commit comments

Comments
 (0)