Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/del
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.0.0
Choose a base ref
...
head repository: sindresorhus/del
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.1.0
Choose a head ref
  • 5 commits
  • 7 files changed
  • 4 contributors

Commits on Sep 27, 2020

  1. Fix readme typo (#129)

    SimenB authored Sep 27, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    AtofStryker Bill Glesias
    Copy the full SHA
    72007d7 View commit details

Commits on Jan 2, 2021

  1. Verified

    This commit was signed with the committer’s verified signature.
    AtofStryker Bill Glesias
    Copy the full SHA
    d0030ba View commit details

Commits on Jan 24, 2021

  1. Copy the full SHA
    7c756b3 View commit details

Commits on May 16, 2022

  1. Add onProgress option (#141)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    jopemachine and sindresorhus authored May 16, 2022
    Copy the full SHA
    8742649 View commit details
  2. 6.1.0

    sindresorhus committed May 16, 2022
    Copy the full SHA
    c12fddd View commit details
Showing with 161 additions and 17 deletions.
  1. +26 −0 .github/workflows/main.yml
  2. +0 −9 .travis.yml
  3. +34 −2 index.d.ts
  4. +22 −2 index.js
  5. +1 −1 package.json
  6. +30 −3 readme.md
  7. +48 −0 test.js
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

36 changes: 34 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import {GlobbyOptions} from 'globby';

declare namespace del {
interface ProgressData {
/**
Deleted files and directories count.
*/
deletedCount: number;

/**
Total files and directories count.
*/
totalCount: number;

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

interface Options extends GlobbyOptions {
/**
Allow deleting the current working directory and outside.
@@ -33,6 +50,21 @@ declare namespace del {
@default Infinity
*/
readonly concurrency?: number;

/**
Called after each file or directory is deleted.
@example
```
import del from 'del';
await del(patterns, {
onProgress: progress => {
// …
}});
```
*/
readonly onProgress?: (progress: ProgressData) => void;
}
}

@@ -43,7 +75,7 @@ declare const del: {
Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`.
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
@param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
@returns The deleted paths.
@@ -59,7 +91,7 @@ declare const del: {
Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`.
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
@param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
@returns The deleted paths.
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
@@ -66,7 +66,15 @@ 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({
totalCount: 0,
deletedCount: 0,
percent: 1
});
}

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

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

onProgress({
totalCount: files.length,
deletedCount: fileIndex,
percent: fileIndex / files.length
});

return file;
};

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

onProgress({
totalCount: files.length,
deletedCount: files.length,
percent: 1
});

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

return removedFiles;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "del",
"version": "6.0.0",
"version": "6.1.0",
"description": "Delete files and directories",
"license": "MIT",
"repository": "sindresorhus/del",
33 changes: 30 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# del [![Build Status](https://travis-ci.com/sindresorhus/del.svg?branch=master)](https://travis-ci.com/github/sindresorhus/del) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
# del

> Delete files and directories using [globs](https://github.com/sindresorhus/globby#globbing-patterns)
@@ -21,7 +21,7 @@ const del = require('del');

console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n));
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
})();
```

@@ -67,7 +67,7 @@ Type: `string | string[]`

See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).

- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)

#### options
@@ -108,6 +108,33 @@ Minimum: `1`

Concurrency limit.

##### onProgress

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

Called after each file or directory is deleted.

```js
import del from 'del';

await del(patterns, {
onProgress: progress => {
//
}});
```

###### 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.
48 changes: 48 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -349,3 +349,51 @@ test('windows can pass relative paths with "\\" - sync', t => {

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

test('onProgress option - progress of non-existent file', async t => {
let report;

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

t.deepEqual(report, {
totalCount: 0,
deletedCount: 0,
percent: 1
});
});

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

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

t.deepEqual(report, {
totalCount: 1,
deletedCount: 1,
percent: 1
});
});

test('onProgress option - 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;
}
});

t.deepEqual(report, {
totalCount: 4,
deletedCount: 4,
percent: 1
});
});