Skip to content

Commit

Permalink
Return copied file paths (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and sindresorhus committed Jun 11, 2019
1 parent a280082 commit cfacfc8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ declare namespace cpy {
on(
event: 'progress',
handler: (progress: ProgressData) => void
): Promise<void>;
): Promise<string[]>;
}
}

Expand All @@ -82,7 +82,7 @@ declare const cpy: {
files: string | ReadonlyArray<string>,
destination: string,
options?: cpy.Options
): Promise<void> & cpy.ProgressEmitter;
): Promise<string[]> & cpy.ProgressEmitter;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function cpy(
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const cpy = (src, dest, options = {}) => {
});
}
})
.then(() => to)
.catch(error => {
throw new CpyError(`Cannot copy from \`${from}\` to \`${to}\`: ${error.message}`, error);
});
Expand Down
16 changes: 8 additions & 8 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import {expectType} from 'tsd';
import cpy = require('.');
import {ProgressEmitter, ProgressData} from '.';

expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy(['source/*.png', '!source/goat.png'], 'destination')
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {rename: 'foobar'})
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {rename: basename => `prefix-${basename}`})
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {cwd: '/'})
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {parents: true})
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {expandDirectories: true})
);
expectType<Promise<void> & ProgressEmitter>(
expectType<Promise<string[]> & ProgressEmitter>(
cpy('foo.js', 'destination', {overwrite: false})
);

expectType<Promise<void>>(
expectType<Promise<string[]>>(
cpy('foo.js', 'destination').on('progress', progress => {
expectType<ProgressData>(progress);

Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,17 @@ test('returns the event emitter on early rejection', t => {
t.is(typeof rejectedPromise.on, 'function');
rejectedPromise.catch(() => {});
});

test('returns destination path', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/bar'), 'dolor sit amet');

const to = await cpy(['foo', 'bar'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')});

t.deepEqual(to, [
path.join(t.context.tmp, 'foo'),
path.join(t.context.tmp, 'bar')
]);
});

0 comments on commit cfacfc8

Please sign in to comment.