From 936710efe8b778ffe8619a714a5536abcadc5207 Mon Sep 17 00:00:00 2001 From: Jeff Wen Date: Wed, 10 May 2023 21:07:46 +0800 Subject: [PATCH] Add file `sourcePath` and `destinationPath` to the progress event (#112) Co-authored-by: Sindre Sorhus --- index.d.ts | 10 ++++++++++ index.js | 2 ++ index.test-d.ts | 2 ++ readme.md | 6 +++++- test.js | 12 ++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 8e2e753..40198c8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -125,6 +125,16 @@ export type ProgressData = { Completed percentage. A value between `0` and `1`. */ percent: number; + + /** + The absolute source path of the current file being copied. + */ + sourcePath: string; + + /** + The absolute destination path of the current file being copied. + */ + destinationPath: string; }; export type ProgressEmitter = { diff --git a/index.js b/index.js index cbcb179..ec04f72 100644 --- a/index.js +++ b/index.js @@ -248,6 +248,8 @@ export default function cpy( percent: completedFiles / entries.length, completedFiles, completedSize, + sourcePath: event.sourcePath, + destinationPath: event.destinationPath, }); } }; diff --git a/index.test-d.ts b/index.test-d.ts index 6b39e7d..cd50991 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -50,5 +50,7 @@ expectType>( expectType(progress.totalFiles); expectType(progress.completedSize); expectType(progress.percent); + expectType(progress.sourcePath); + expectType(progress.destinationPath); }), ); diff --git a/readme.md b/readme.md index 18285ee..c1af084 100644 --- a/readme.md +++ b/readme.md @@ -201,12 +201,16 @@ Type: `Function` completedFiles: number, totalFiles: number, completedSize: number, - percent: number + percent: number, + sourcePath: string, + destinationPath: string, } ``` - `completedSize` is in bytes - `percent` is a value between `0` and `1` +- `sourcePath` is the absolute source path of the current file being copied. +- `destinationPath` is The absolute destination path of the current file being copied. Note that the `.on()` method is available only right after the initial `cpy` call, so make sure you add a `handler` before awaiting the promise: diff --git a/test.js b/test.js index 10fa4f9..7ef13a8 100644 --- a/test.js +++ b/test.js @@ -334,6 +334,8 @@ test('junk files are ignored', async t => { t.is(report.completedFiles, 1); t.is(report.completedSize, 11); t.is(report.percent, 1); + t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo')); + t.is(read(report.destinationPath), read(t.context.tmp + '/cwd/foo')); }); test('junk files are copied', async t => { @@ -356,6 +358,8 @@ test('junk files are copied', async t => { t.is(report.completedFiles, 2); t.is(report.completedSize, 22); t.is(report.percent, 1); + t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo')); + t.is(read(report.destinationPath), read(t.context.tmp + '/cwd/foo')); }); test('nested junk files are ignored', async t => { @@ -378,6 +382,8 @@ test('nested junk files are ignored', async t => { t.is(report.completedFiles, 1); t.is(report.completedSize, 11); t.is(report.percent, 1); + t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/test')); + t.is(read(report.destinationPath), read(t.context.tmp + '/test')); }); test('reports copy progress of single file', async t => { @@ -398,6 +404,8 @@ test('reports copy progress of single file', async t => { t.is(report.completedFiles, 1); t.is(report.completedSize, 11); t.is(report.percent, 1); + t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo')); + t.is(read(report.destinationPath), read(t.context.tmp + '/foo')); }); test('reports copy progress of multiple files', async t => { @@ -419,6 +427,8 @@ test('reports copy progress of multiple files', async t => { t.is(report.completedFiles, 2); t.is(report.completedSize, 25); t.is(report.percent, 1); + t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/bar')); + t.is(read(report.destinationPath), read(t.context.tmp + '/bar')); }); test('reports correct completedSize', async t => { @@ -443,6 +453,8 @@ test('reports correct completedSize', async t => { t.is(report.totalFiles, 1); t.is(report.completedFiles, 1); t.is(report.completedSize, ONE_MEGABYTE); + t.is(read(report.sourcePath), read(t.context.tmp, 'cwd/fatfile')); + t.is(read(report.destinationPath), read(t.context.tmp, 'fatfile')); t.true(chunkCount > 1); t.is(report.percent, 1); });