From f5d31e6136bc675b33375edddc0bdda93046615a Mon Sep 17 00:00:00 2001 From: M <6790070+marchuffnagle@users.noreply.github.com> Date: Wed, 30 Aug 2023 12:42:08 -0400 Subject: [PATCH] Add `path` to `onProgress` event (#155) --- index.d.ts | 7 +++++++ index.js | 1 + readme.md | 4 +++- test.js | 16 +++++++++------- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index e32977f..639fc5d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,6 +15,13 @@ export type ProgressData = { Completed percentage. A value between `0` and `1`. */ readonly percent: number; + + /** + The absolute path of the deleted file or directory. + + It will not be present if nothing was deleted. + */ + readonly path?: string; }; export type Options = { diff --git a/index.js b/index.js index 5e2a43a..dd5b8d0 100644 --- a/index.js +++ b/index.js @@ -93,6 +93,7 @@ export async function deleteAsync(patterns, {force, dryRun, cwd = process.cwd(), totalCount: files.length, deletedCount, percent: deletedCount / files.length, + path: file, }); return file; diff --git a/readme.md b/readme.md index d2e8082..315ff84 100644 --- a/readme.md +++ b/readme.md @@ -153,11 +153,13 @@ await deleteAsync(patterns, { { totalCount: number, deletedCount: number, - percent: number + percent: number, + path?: string } ``` - `percent` is a value between `0` and `1` +- `path` is the absolute path of the deleted file or directory. It will not be present if nothing was deleted. ## CLI diff --git a/test.js b/test.js index 5b972ad..a2a5016 100644 --- a/test.js +++ b/test.js @@ -378,11 +378,12 @@ test('onProgress option - progress of single file', async t => { totalCount: 1, deletedCount: 1, percent: 1, + path: t.context.tmp, }); }); test('onProgress option - progress of multiple files', async t => { - let report; + const reports = []; const sourcePath = process.platform === 'win32' ? path.resolve(`${t.context.tmp}/*`).replace(/\\/g, '/') : `${t.context.tmp}/*`; @@ -390,13 +391,14 @@ test('onProgress option - progress of multiple files', async t => { cwd: __dirname, force: true, onProgress(event) { - report = event; + reports.push(event); }, }); - t.deepEqual(report, { - totalCount: 4, - deletedCount: 4, - percent: 1, - }); + t.is(reports.length, 4); + t.deepEqual(reports.map(r => r.totalCount), [4, 4, 4, 4]); + t.deepEqual(reports.map(r => r.deletedCount).sort(), [1, 2, 3, 4]); + + const expectedPaths = ['1', '2', '3', '4'].map(x => path.join(t.context.tmp, `${x}.tmp`)); + t.deepEqual(reports.map(r => r.path).sort(), expectedPaths.sort()); });