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 2 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
26 changes: 25 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ declare namespace del {
*/
readonly concurrency?: number;
}

interface ProgressData {
/**
Deleted file count.
*/
deletedFiles: number;

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

/**
Completed percentage. A value between `0` and `1`.
*/
percent: number;
}

interface ProgressEmitter {
on: (
event: 'progress',
handler: (progress: ProgressData) => void
) => Promise<string[]>;
}
}

declare const del: {
Expand Down Expand Up @@ -78,7 +102,7 @@ declare const del: {
(
patterns: string | readonly string[],
options?: del.Options
): Promise<string[]>;
): Promise<string[]> & del.ProgressEmitter;
};

export = del;
65 changes: 49 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const isPathCwd = require('is-path-cwd');
const isPathInside = require('is-path-inside');
const rimraf = require('rimraf');
const pMap = require('p-map');
const {EventEmitter} = require('events');

const rimrafP = promisify(rimraf);

Expand Down Expand Up @@ -52,7 +53,7 @@ function normalizePatterns(patterns) {
return patterns;
}

module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
module.exports = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
options = {
expandDirectories: false,
onlyFiles: false,
Expand All @@ -61,30 +62,62 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option
...options
};

patterns = normalizePatterns(patterns);
const progressEmitter = new EventEmitter();

const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));
const promise = (async () => {
patterns = normalizePatterns(patterns);

const mapper = async file => {
file = path.resolve(cwd, file);
const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));

if (!force) {
safeCheck(file, cwd);
}
if (files.length === 0) {
progressEmitter.emit('progress', {
totalFiles: 0,
deletedFiles: 0,
percent: 1
});

if (!dryRun) {
await rimrafP(file, rimrafOptions);
return [];
}

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

const removedFiles = await pMap(files, mapper, options);
if (!force) {
safeCheck(file, cwd);
}

removedFiles.sort((a, b) => a.localeCompare(b));
progressEmitter.emit('progress', {
totalFiles: files.length,
deletedFiles: fileIndex,
percent: fileIndex / files.length
});

return removedFiles;
if (!dryRun) {
await rimrafP(file, rimrafOptions);
}

return file;
};

const removedFiles = await pMap(files, mapper, options);
removedFiles.sort((a, b) => a.localeCompare(b));

progressEmitter.emit('progress', {
totalFiles: files.length,
deletedFiles: files.length,
percent: 1
});

return removedFiles;
})();

promise.on = (...arguments_) => {
progressEmitter.on(...arguments_);
return promise;
};

return promise;
};

module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
Expand Down
13 changes: 7 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expectType} from 'tsd';
import {ProgressEmitter} from './index.d';
import del = require('.');

const paths = [
Expand All @@ -7,13 +8,13 @@ const paths = [
];

// Del
expectType<Promise<string[]>>(del('temp/*.js'));
expectType<Promise<string[]>>(del(paths));
expectType<Promise<string[]> & ProgressEmitter>(del('temp/*.js'));
expectType<Promise<string[]> & ProgressEmitter>(del(paths));

expectType<Promise<string[]>>(del(paths, {force: true}));
expectType<Promise<string[]>>(del(paths, {dryRun: true}));
expectType<Promise<string[]>>(del(paths, {concurrency: 20}));
expectType<Promise<string[]>>(del(paths, {cwd: ''}));
expectType<Promise<string[]> & ProgressEmitter>(del(paths, {force: true}));
expectType<Promise<string[]> & ProgressEmitter>(del(paths, {dryRun: true}));
expectType<Promise<string[]> & ProgressEmitter>(del(paths, {concurrency: 20}));
expectType<Promise<string[]> & ProgressEmitter>(del(paths, {cwd: ''}));

// Del (sync)
expectType<string[]>(del.sync('tmp/*.js'));
Expand Down
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ Minimum: `1`

Concurrency limit.

## Progress reporting

### del.on('progress', handler)

#### handler(progress)

Type: `Function`

##### progress

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

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

Note that the `.on()` method is available only right after the initial `del` call, so make sure you add a `handler` before awaiting the promise:

```js
import del from 'del';

await del(patterns).on('progress', progress => {
// …
});
```

## 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
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,41 @@ 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').on('progress', event => {
report = event;
});

t.is(report.totalFiles, 0);
t.is(report.deletedFiles, 0);
t.is(report.percent, 1);
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
});

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

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

t.is(report.totalFiles, 1);
t.is(report.deletedFiles, 1);
t.is(report.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}).on('progress', event => {
report = event;
});

t.is(report.totalFiles, 4);
t.is(report.deletedFiles, 4);
t.is(report.percent, 1);
});