Skip to content

Commit 106d7d8

Browse files
authoredJul 19, 2022
Move to ESM (#143)
1 parent 5de56fd commit 106d7d8

File tree

8 files changed

+227
-239
lines changed

8 files changed

+227
-239
lines changed
 

‎.github/workflows/main.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
14-
- 12
15-
- 10
1615
os:
1716
- ubuntu-latest
1817
- macos-latest

‎benchmark.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
2-
const path = require('path');
3-
const Benchmark = require('benchmark');
4-
const makeDir = require('make-dir');
5-
const tempy = require('tempy');
6-
const del = require('.');
1+
import path from 'node:path';
2+
import process from 'node:process';
3+
import Benchmark from 'benchmark';
4+
import makeDir from 'make-dir';
5+
import tempy from 'tempy';
6+
import {deleteAsync, deleteSync} from './index.js';
77

88
const suite = new Benchmark.Suite('concurrency');
99

1010
const temporaryDir = tempy.directory();
1111

12-
const fixtures = Array.from({length: 2000}, (_, index) => {
13-
return path.resolve(temporaryDir, (index + 1).toString());
14-
});
12+
const fixtures = Array.from({length: 2000}, (_, index) => path.resolve(temporaryDir, (index + 1).toString()));
1513

1614
function createFixtures() {
1715
for (const fixture of fixtures) {
@@ -33,7 +31,7 @@ const concurrencies = [
3331
400,
3432
500,
3533
1000,
36-
Infinity
34+
Number.POSITIVE_INFINITY,
3735
];
3836

3937
for (const concurrency of concurrencies) {
@@ -48,26 +46,26 @@ for (const concurrency of concurrencies) {
4846
// https://github.com/bestiejs/benchmark.js/issues/136
4947
createFixtures();
5048

51-
const removedFiles = await del(['**/*'], {
49+
const removedFiles = await deleteAsync(['**/*'], {
5250
cwd: temporaryDir,
53-
concurrency
51+
concurrency,
5452
});
5553

5654
if (removedFiles.length !== fixtures.length) {
5755
const error = new Error(
58-
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`
56+
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`,
5957
);
6058

6159
console.error(error);
6260

63-
del.sync(temporaryDir, {cwd: temporaryDir, force: true});
61+
deleteSync(temporaryDir, {cwd: temporaryDir, force: true});
6462

6563
// eslint-disable-next-line unicorn/no-process-exit
6664
process.exit(1);
6765
}
6866

6967
deferred.resolve();
70-
}
68+
},
7169
});
7270
}
7371

@@ -78,6 +76,6 @@ suite
7876
.on('complete', function () {
7977
console.log(`Fastest is ${this.filter('fastest').map('name')}`);
8078

81-
del.sync(temporaryDir, {cwd: temporaryDir, force: true});
79+
deleteSync(temporaryDir, {cwd: temporaryDir, force: true});
8280
})
8381
.run({async: true});

‎index.d.ts

+87-97
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,106 @@
11
import {GlobbyOptions} from 'globby';
22

3-
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-
21-
interface Options extends GlobbyOptions {
22-
/**
23-
Allow deleting the current working directory and outside.
24-
25-
@default false
26-
*/
27-
readonly force?: boolean;
28-
29-
/**
30-
See what would be deleted.
31-
32-
@default false
33-
34-
@example
35-
```
36-
import del = require('del');
37-
38-
(async () => {
39-
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
40-
41-
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
42-
})();
43-
```
44-
*/
45-
readonly dryRun?: boolean;
46-
47-
/**
48-
Concurrency limit. Minimum: `1`.
49-
50-
@default Infinity
51-
*/
52-
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;
68-
}
3+
export interface ProgressData {
4+
/**
5+
Deleted files and directories count.
6+
*/
7+
deletedCount: number;
8+
9+
/**
10+
Total files and directories count.
11+
*/
12+
totalCount: number;
13+
14+
/**
15+
Completed percentage. A value between `0` and `1`.
16+
*/
17+
percent: number;
6918
}
7019

71-
declare const del: {
20+
export interface Options extends GlobbyOptions {
7221
/**
73-
Synchronously delete files and directories using glob patterns.
22+
Allow deleting the current working directory and outside.
7423
75-
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()`.
24+
@default false
25+
*/
26+
readonly force?: boolean;
27+
28+
/**
29+
See what would be deleted.
7630
77-
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
78-
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
79-
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
80-
@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.
81-
@returns The deleted paths.
31+
@default false
32+
33+
@example
34+
```
35+
import {deleteAsync} from 'del';
36+
37+
const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
38+
39+
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
40+
```
8241
*/
83-
sync: (
84-
patterns: string | readonly string[],
85-
options?: del.Options
86-
) => string[];
42+
readonly dryRun?: boolean;
8743

8844
/**
89-
Delete files and directories using glob patterns.
45+
Concurrency limit. Minimum: `1`.
9046
91-
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()`.
47+
@default Infinity
48+
*/
49+
readonly concurrency?: number;
9250

93-
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
94-
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
95-
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
96-
@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.
97-
@returns The deleted paths.
51+
/**
52+
Called after each file or directory is deleted.
9853
9954
@example
10055
```
101-
import del = require('del');
56+
import {deleteAsync} from 'del';
10257
103-
(async () => {
104-
const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']);
105-
106-
console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
107-
})();
58+
await deleteAsync(patterns, {
59+
onProgress: progress => {
60+
// …
61+
}});
10862
```
10963
*/
110-
(
111-
patterns: string | readonly string[],
112-
options?: del.Options
113-
): Promise<string[]>;
114-
};
64+
readonly onProgress?: (progress: ProgressData) => void;
65+
}
11566

116-
export = del;
67+
/**
68+
Delete files and directories using glob patterns.
69+
70+
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()`.
71+
72+
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
73+
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
74+
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
75+
@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.
76+
@returns The deleted paths.
77+
78+
@example
79+
```
80+
import {deleteAsync} from 'del';
81+
82+
const deletedPaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
83+
84+
console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
85+
```
86+
*/
87+
export function deleteAsync(
88+
patterns: string | readonly string[],
89+
options?: Options
90+
): Promise<string[]>;
91+
92+
/**
93+
Synchronously delete files and directories using glob patterns.
94+
95+
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()`.
96+
97+
@param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
98+
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
99+
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
100+
@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.
101+
@returns The deleted paths.
102+
*/
103+
export function deleteSync(
104+
patterns: string | readonly string[],
105+
options?: Options
106+
): string[];

‎index.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'use strict';
2-
const {promisify} = require('util');
3-
const path = require('path');
4-
const globby = require('globby');
5-
const isGlob = require('is-glob');
6-
const slash = require('slash');
7-
const gracefulFs = require('graceful-fs');
8-
const isPathCwd = require('is-path-cwd');
9-
const isPathInside = require('is-path-inside');
10-
const rimraf = require('rimraf');
11-
const pMap = require('p-map');
1+
import {promisify} from 'node:util';
2+
import path from 'node:path';
3+
import process from 'node:process';
4+
import globby from 'globby';
5+
import isGlob from 'is-glob';
6+
import slash from 'slash';
7+
import gracefulFs from 'graceful-fs';
8+
import isPathCwd from 'is-path-cwd';
9+
import isPathInside from 'is-path-inside';
10+
import rimraf from 'rimraf';
11+
import pMap from 'p-map';
1212

1313
const rimrafP = promisify(rimraf);
1414

@@ -25,7 +25,7 @@ const rimrafOptions = {
2525
rmdir: gracefulFs.rmdir,
2626
rmdirSync: gracefulFs.rmdirSync,
2727
readdir: gracefulFs.readdir,
28-
readdirSync: gracefulFs.readdirSync
28+
readdirSync: gracefulFs.readdirSync,
2929
};
3030

3131
function safeCheck(file, cwd) {
@@ -52,25 +52,25 @@ function normalizePatterns(patterns) {
5252
return patterns;
5353
}
5454

55-
module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
55+
export async function deleteAsync(patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) {
5656
options = {
5757
expandDirectories: false,
5858
onlyFiles: false,
5959
followSymbolicLinks: false,
6060
cwd,
61-
...options
61+
...options,
6262
};
6363

6464
patterns = normalizePatterns(patterns);
6565

66-
const files = (await globby(patterns, options))
67-
.sort((a, b) => b.localeCompare(a));
66+
const paths = await globby(patterns, options);
67+
const files = paths.sort((a, b) => b.localeCompare(a));
6868

6969
if (files.length === 0) {
7070
onProgress({
7171
totalCount: 0,
7272
deletedCount: 0,
73-
percent: 1
73+
percent: 1,
7474
});
7575
}
7676

@@ -92,7 +92,7 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgres
9292
onProgress({
9393
totalCount: files.length,
9494
deletedCount,
95-
percent: deletedCount / files.length
95+
percent: deletedCount / files.length,
9696
});
9797

9898
return file;
@@ -103,15 +103,15 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgres
103103
removedFiles.sort((a, b) => a.localeCompare(b));
104104

105105
return removedFiles;
106-
};
106+
}
107107

108-
module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
108+
export function deleteSync(patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) {
109109
options = {
110110
expandDirectories: false,
111111
onlyFiles: false,
112112
followSymbolicLinks: false,
113113
cwd,
114-
...options
114+
...options,
115115
};
116116

117117
patterns = normalizePatterns(patterns);
@@ -136,4 +136,4 @@ module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options
136136
removedFiles.sort((a, b) => a.localeCompare(b));
137137

138138
return removedFiles;
139-
};
139+
}

‎index.test-d.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import {expectType} from 'tsd';
2-
import del = require('.');
2+
import {deleteAsync, deleteSync} from './index.js';
33

44
const paths = [
55
'temp/*.js',
6-
'!temp/unicorn.js'
6+
'!temp/unicorn.js',
77
];
88

99
// Del
10-
expectType<Promise<string[]>>(del('temp/*.js'));
11-
expectType<Promise<string[]>>(del(paths));
10+
expectType<Promise<string[]>>(deleteAsync('temp/*.js'));
11+
expectType<Promise<string[]>>(deleteAsync(paths));
1212

13-
expectType<Promise<string[]>>(del(paths, {force: true}));
14-
expectType<Promise<string[]>>(del(paths, {dryRun: true}));
15-
expectType<Promise<string[]>>(del(paths, {concurrency: 20}));
16-
expectType<Promise<string[]>>(del(paths, {cwd: ''}));
13+
expectType<Promise<string[]>>(deleteAsync(paths, {force: true}));
14+
expectType<Promise<string[]>>(deleteAsync(paths, {dryRun: true}));
15+
expectType<Promise<string[]>>(deleteAsync(paths, {concurrency: 20}));
16+
expectType<Promise<string[]>>(deleteAsync(paths, {cwd: ''}));
1717

1818
// Del (sync)
19-
expectType<string[]>(del.sync('tmp/*.js'));
20-
expectType<string[]>(del.sync(paths));
19+
expectType<string[]>(deleteSync('tmp/*.js'));
20+
expectType<string[]>(deleteSync(paths));
2121

22-
expectType<string[]>(del.sync(paths, {force: true}));
23-
expectType<string[]>(del.sync(paths, {dryRun: true}));
24-
expectType<string[]>(del.sync(paths, {concurrency: 20}));
25-
expectType<string[]>(del.sync(paths, {cwd: ''}));
22+
expectType<string[]>(deleteSync(paths, {force: true}));
23+
expectType<string[]>(deleteSync(paths, {dryRun: true}));
24+
expectType<string[]>(deleteSync(paths, {concurrency: 20}));
25+
expectType<string[]>(deleteSync(paths, {cwd: ''}));

‎package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": "^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
17-
"test": "xo && ava && tsd",
19+
"test": "xo && ava --serial --no-worker-threads && tsd",
1820
"bench": "node benchmark.js"
1921
},
2022
"files": [
@@ -56,11 +58,11 @@
5658
"slash": "^3.0.0"
5759
},
5860
"devDependencies": {
59-
"ava": "^2.4.0",
61+
"ava": "^4.3.1",
6062
"benchmark": "^2.1.4",
6163
"make-dir": "^3.1.0",
6264
"tempy": "^0.7.0",
63-
"tsd": "^0.13.1",
64-
"xo": "^0.33.1"
65+
"tsd": "^0.22.0",
66+
"xo": "^0.50.0"
6567
}
6668
}

‎readme.md

+16-20
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ $ npm install del
1313
## Usage
1414

1515
```js
16-
const del = require('del');
16+
import {deleteAsync} from 'del';
1717

18-
(async () => {
19-
const deletedFilePaths = await del(['temp/*.js', '!temp/unicorn.js']);
20-
const deletedDirectoryPaths = await del(['temp', 'public']);
18+
const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
19+
const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);
2120

22-
console.log('Deleted files:\n', deletedFilePaths.join('\n'));
23-
console.log('\n\n');
24-
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
25-
})();
21+
console.log('Deleted files:\n', deletedFilePaths.join('\n'));
22+
console.log('\n\n');
23+
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
2624
```
2725

2826
## Beware
@@ -32,19 +30,19 @@ The glob pattern `**` matches all children and *the parent*.
3230
So this won't work:
3331

3432
```js
35-
del.sync(['public/assets/**', '!public/assets/goat.png']);
33+
deleteSync(['public/assets/**', '!public/assets/goat.png']);
3634
```
3735

3836
You have to explicitly ignore the parent directories too:
3937

4038
```js
41-
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
39+
deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
4240
```
4341

4442
To delete all subdirectories inside `public/`, you can do:
4543

4644
```js
47-
del.sync(['public/*/']);
45+
deleteSync(['public/*/']);
4846
```
4947

5048
Suggestions on how to improve this welcome!
@@ -53,11 +51,11 @@ Suggestions on how to improve this welcome!
5351

5452
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()`.
5553

56-
### del(patterns, options?)
54+
### deleteAsync(patterns, options?)
5755

5856
Returns `Promise<string[]>` with the deleted paths.
5957

60-
### del.sync(patterns, options?)
58+
### deleteSync(patterns, options?)
6159

6260
Returns `string[]` with the deleted paths.
6361

@@ -91,13 +89,11 @@ Default: `false`
9189
See what would be deleted.
9290

9391
```js
94-
const del = require('del');
92+
import {deleteAsync} from 'del';
9593

96-
(async () => {
97-
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
94+
const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
9895

99-
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
100-
})();
96+
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
10197
```
10298

10399
##### concurrency
@@ -115,9 +111,9 @@ Type: `(progress: ProgressData) => void`
115111
Called after each file or directory is deleted.
116112

117113
```js
118-
import del from 'del';
114+
import {deleteAsync} from 'del';
119115

120-
await del(patterns, {
116+
await deleteAsync(patterns, {
121117
onProgress: progress => {
122118
//
123119
}});

‎test.js

+66-63
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.