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

Throw if the given source file does not exist #68

Merged
merged 13 commits into from
Sep 22, 2019
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const os = require('os');
const pAll = require('p-all');
const arrify = require('arrify');
const globby = require('globby');
const hasGlob = require('has-glob');
const cpFile = require('cp-file');
const junk = require('junk');
const CpyError = require('./cpy-error');
Expand Down Expand Up @@ -65,13 +66,8 @@ 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
});
if (files.length === 0 && !hasGlob(source)) {
throw new CpyError(`Cannot copy \`${source}\`: the file doesn't exist`);
}

const fileProgressHandler = event => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"arrify": "^2.0.1",
"cp-file": "^7.0.0",
"globby": "^9.2.0",
"has-glob": "^1.0.0",
"nested-error-stacks": "^2.1.0",
"p-all": "^2.1.0",
"junk": "^3.1.0"
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
35 changes: 23 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,32 @@ 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), {
whitecrownclown marked this conversation as resolved.
Show resolved Hide resolved
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));
whitecrownclown marked this conversation as resolved.
Show resolved Hide resolved
});

test('does not throw on mixed path and glob', async t => {
fs.mkdirSync(t.context.tmp);

await t.notThrowsAsync(cpy(['*.js', 'readme.md'], t.context.tmp));
});

test('junk files are ignored', async t => {
Expand Down