Skip to content

Commit

Permalink
Add path to onProgress event (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchuffnagle committed Aug 30, 2023
1 parent e9b0d71 commit f5d31e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions test.js
Expand Up @@ -378,25 +378,27 @@ 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}/*`;

await deleteAsync(sourcePath, {
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());
});

0 comments on commit f5d31e6

Please sign in to comment.