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 2 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
5 changes: 5 additions & 0 deletions index.d.ts
Expand Up @@ -15,6 +15,11 @@ export interface ProgressData {
Completed percentage. A value between `0` and `1`.
*/
readonly percent: number;

/**
Path of deleted file
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From onProgress docs:

Called after each file or directory is deleted.

So it could be a directory too.


It would also be useful to make it clear it's an "absolute path".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a8b391e

*/
readonly path?: string;
}

export interface Options extends GlobbyOptions {
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` will not be present if no files were deleted
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing in the TS doc comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it should describe what it is, like in the TS docs. They should be in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a8b391e


## 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;
let 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());
});