Skip to content

Commit 8742649

Browse files
jopemachinesindresorhus
andauthoredMay 16, 2022
Add onProgress option (#141)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 7c756b3 commit 8742649

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed
 

‎index.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import {GlobbyOptions} from 'globby';
22

33
declare namespace del {
4+
interface ProgressData {
5+
/**
6+
Deleted files and directories count.
7+
*/
8+
deletedCount: number;
9+
10+
/**
11+
Total files and directories count.
12+
*/
13+
totalCount: number;
14+
15+
/**
16+
Completed percentage. A value between `0` and `1`.
17+
*/
18+
percent: number;
19+
}
20+
421
interface Options extends GlobbyOptions {
522
/**
623
Allow deleting the current working directory and outside.
@@ -33,6 +50,21 @@ declare namespace del {
3350
@default Infinity
3451
*/
3552
readonly concurrency?: number;
53+
54+
/**
55+
Called after each file or directory is deleted.
56+
57+
@example
58+
```
59+
import del from 'del';
60+
61+
await del(patterns, {
62+
onProgress: progress => {
63+
// …
64+
}});
65+
```
66+
*/
67+
readonly onProgress?: (progress: ProgressData) => void;
3668
}
3769
}
3870

‎index.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function normalizePatterns(patterns) {
5252
return patterns;
5353
}
5454

55-
module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
55+
module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
5656
options = {
5757
expandDirectories: false,
5858
onlyFiles: false,
@@ -66,7 +66,15 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option
6666
const files = (await globby(patterns, options))
6767
.sort((a, b) => b.localeCompare(a));
6868

69-
const mapper = async file => {
69+
if (files.length === 0) {
70+
onProgress({
71+
totalCount: 0,
72+
deletedCount: 0,
73+
percent: 1
74+
});
75+
}
76+
77+
const mapper = async (file, fileIndex) => {
7078
file = path.resolve(cwd, file);
7179

7280
if (!force) {
@@ -77,11 +85,23 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option
7785
await rimrafP(file, rimrafOptions);
7886
}
7987

88+
onProgress({
89+
totalCount: files.length,
90+
deletedCount: fileIndex,
91+
percent: fileIndex / files.length
92+
});
93+
8094
return file;
8195
};
8296

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

99+
onProgress({
100+
totalCount: files.length,
101+
deletedCount: files.length,
102+
percent: 1
103+
});
104+
85105
removedFiles.sort((a, b) => a.localeCompare(b));
86106

87107
return removedFiles;

‎readme.md

+27
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ Minimum: `1`
108108

109109
Concurrency limit.
110110

111+
##### onProgress
112+
113+
Type: `(progress: ProgressData) => void`
114+
115+
Called after each file or directory is deleted.
116+
117+
```js
118+
import del from 'del';
119+
120+
await del(patterns, {
121+
onProgress: progress => {
122+
//
123+
}});
124+
```
125+
126+
###### ProgressData
127+
128+
```js
129+
{
130+
totalFiles: number,
131+
deletedFiles: number,
132+
percent: number
133+
}
134+
```
135+
136+
- `percent` is a value between `0` and `1`
137+
111138
## CLI
112139

113140
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.

‎test.js

+48
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,51 @@ test('windows can pass relative paths with "\\" - sync', t => {
349349

350350
t.deepEqual(removeFiles, [nestedFile]);
351351
});
352+
353+
test('onProgress option - progress of non-existent file', async t => {
354+
let report;
355+
356+
await del('non-existent-directory', {onProgress: event => {
357+
report = event;
358+
}});
359+
360+
t.deepEqual(report, {
361+
totalCount: 0,
362+
deletedCount: 0,
363+
percent: 1
364+
});
365+
});
366+
367+
test('onProgress option - progress of single file', async t => {
368+
let report;
369+
370+
await del(t.context.tmp, {cwd: __dirname, force: true, onProgress: event => {
371+
report = event;
372+
}});
373+
374+
t.deepEqual(report, {
375+
totalCount: 1,
376+
deletedCount: 1,
377+
percent: 1
378+
});
379+
});
380+
381+
test('onProgress option - progress of multiple files', async t => {
382+
let report;
383+
384+
const sourcePath = process.platform === 'win32' ? path.resolve(`${t.context.tmp}/*`).replace(/\\/g, '/') : `${t.context.tmp}/*`;
385+
386+
await del(sourcePath, {
387+
cwd: __dirname,
388+
force: true,
389+
onProgress: event => {
390+
report = event;
391+
}
392+
});
393+
394+
t.deepEqual(report, {
395+
totalCount: 4,
396+
deletedCount: 4,
397+
percent: 1
398+
});
399+
});

0 commit comments

Comments
 (0)
Please sign in to comment.