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

Add path to onProgress event #155

Merged
merged 6 commits into from Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
});