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

Support progressive reporting #141

Merged
merged 9 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import {GlobbyOptions} from 'globby';

declare namespace del {
interface ProgressData {
/**
Deleted file count.
*/
deletedFiles: number;
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

/**
Overall file count.
*/
totalFiles: number;

/**
Completed percentage. A value between `0` and `1`.
*/
percent: number;
}
interface Options extends GlobbyOptions {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
/**
Allow deleting the current working directory and outside.
Expand Down Expand Up @@ -33,6 +49,22 @@ declare namespace del {
@default Infinity
*/
readonly concurrency?: number;

/**
Callback function for progress reporting.

Called before each file is deleted.
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

@example
```js
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
import del from 'del';

await del(patterns, {onProgress: (progress) => {
// …
}});
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
```
*/
readonly onProgress?: (progress: ProgressData) => void;
}
}

Expand Down
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function normalizePatterns(patterns) {
return patterns;
}

module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
options = {
expandDirectories: false,
onlyFiles: false,
Expand All @@ -66,13 +66,27 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option
const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));

const mapper = async file => {
if (files.length === 0) {
onProgress({
totalFiles: 0,
deletedFiles: 0,
percent: 1
});
}

const mapper = async (file, fileIndex) => {
file = path.resolve(cwd, file);

if (!force) {
safeCheck(file, cwd);
}

onProgress({
totalFiles: files.length,
deletedFiles: fileIndex,
percent: fileIndex / files.length
});

if (!dryRun) {
await rimrafP(file, rimrafOptions);
}
Expand All @@ -82,6 +96,12 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option

const removedFiles = await pMap(files, mapper, options);

onProgress({
totalFiles: files.length,
deletedFiles: files.length,
percent: 1
});

removedFiles.sort((a, b) => a.localeCompare(b));

return removedFiles;
Expand Down
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ Minimum: `1`

Concurrency limit.

##### onProgress

Type: `(progress: ProgressData) => void`

Callback function for progress reporting.
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

Called before each file is deleted.
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

```js
import del from 'del';

await del(patterns, {onProgress: (progress) => {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
// …
}});
```

###### ProgressData

```js
{
totalFiles: number,
deletedFiles: number,
percent: number
}
```

- `percent` is a value between `0` and `1`

## CLI

See [del-cli](https://github.com/sindresorhus/del-cli) for a CLI for this module and [trash-cli](https://github.com/sindresorhus/trash-cli) for a safe version that is suitable for running by hand.
Expand Down
44 changes: 44 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,47 @@ test('windows can pass relative paths with "\\" - sync', t => {

t.deepEqual(removeFiles, [nestedFile]);
});

test('report delete progress of non-existent file', async t => {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
let report;

await del('non-existent-directory', {onProgress: event => {
report = event;
}});

t.deepEqual(report, {
totalFiles: 0,
deletedFiles: 0,
percent: 1
});
});

test('report delete progress of single file', async t => {
let report;

await del(t.context.tmp, {cwd: __dirname, force: true, onProgress: event => {
report = event;
}});

t.deepEqual(report, {
totalFiles: 1,
deletedFiles: 1,
percent: 1
});
});

test('report delete progress of multiple files', async t => {
let report;

const sourcePath = process.platform === 'win32' ? path.resolve(`${t.context.tmp}/*`).replace(/\\/g, '/') : `${t.context.tmp}/*`;

await del(sourcePath, {cwd: __dirname, force: true, onProgress: event => {
report = event;
}});
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

t.deepEqual(report, {
totalFiles: 4,
deletedFiles: 4,
percent: 1
});
});