Skip to content

Commit

Permalink
Move to ESM (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops committed Jul 19, 2022
1 parent 5de56fd commit 106d7d8
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 239 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,9 +10,8 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
Expand Down
30 changes: 14 additions & 16 deletions benchmark.js
@@ -1,17 +1,15 @@
'use strict';
const path = require('path');
const Benchmark = require('benchmark');
const makeDir = require('make-dir');
const tempy = require('tempy');
const del = require('.');
import path from 'node:path';
import process from 'node:process';
import Benchmark from 'benchmark';
import makeDir from 'make-dir';
import tempy from 'tempy';
import {deleteAsync, deleteSync} from './index.js';

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

const temporaryDir = tempy.directory();

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

function createFixtures() {
for (const fixture of fixtures) {
Expand All @@ -33,7 +31,7 @@ const concurrencies = [
400,
500,
1000,
Infinity
Number.POSITIVE_INFINITY,
];

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

const removedFiles = await del(['**/*'], {
const removedFiles = await deleteAsync(['**/*'], {
cwd: temporaryDir,
concurrency
concurrency,
});

if (removedFiles.length !== fixtures.length) {
const error = new Error(
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`,
);

console.error(error);

del.sync(temporaryDir, {cwd: temporaryDir, force: true});
deleteSync(temporaryDir, {cwd: temporaryDir, force: true});

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

deferred.resolve();
}
},
});
}

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

del.sync(temporaryDir, {cwd: temporaryDir, force: true});
deleteSync(temporaryDir, {cwd: temporaryDir, force: true});
})
.run({async: true});
184 changes: 87 additions & 97 deletions index.d.ts
@@ -1,116 +1,106 @@
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.
@default false
*/
readonly force?: boolean;

/**
See what would be deleted.
@default false
@example
```
import del = require('del');
(async () => {
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
})();
```
*/
readonly dryRun?: boolean;

/**
Concurrency limit. Minimum: `1`.
@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;
}
export 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;
}

declare const del: {
export interface Options extends GlobbyOptions {
/**
Synchronously delete files and directories using glob patterns.
Allow deleting the current working directory and outside.
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()`.
@default false
*/
readonly force?: boolean;

/**
See what would be deleted.
@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/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.
@default false
@example
```
import {deleteAsync} from 'del';
const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
```
*/
sync: (
patterns: string | readonly string[],
options?: del.Options
) => string[];
readonly dryRun?: boolean;

/**
Delete files and directories using glob patterns.
Concurrency limit. Minimum: `1`.
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()`.
@default Infinity
*/
readonly concurrency?: number;

@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/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.
/**
Called after each file or directory is deleted.
@example
```
import del = require('del');
import {deleteAsync} from 'del';
(async () => {
const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']);
console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
})();
await deleteAsync(patterns, {
onProgress: progress => {
// …
}});
```
*/
(
patterns: string | readonly string[],
options?: del.Options
): Promise<string[]>;
};
readonly onProgress?: (progress: ProgressData) => void;
}

export = del;
/**
Delete files and directories using glob patterns.
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/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.
@example
```
import {deleteAsync} from 'del';
const deletedPaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
```
*/
export function deleteAsync(
patterns: string | readonly string[],
options?: Options
): Promise<string[]>;

/**
Synchronously delete files and directories using glob patterns.
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/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.
*/
export function deleteSync(
patterns: string | readonly string[],
options?: Options
): string[];
44 changes: 22 additions & 22 deletions index.js
@@ -1,14 +1,14 @@
'use strict';
const {promisify} = require('util');
const path = require('path');
const globby = require('globby');
const isGlob = require('is-glob');
const slash = require('slash');
const gracefulFs = require('graceful-fs');
const isPathCwd = require('is-path-cwd');
const isPathInside = require('is-path-inside');
const rimraf = require('rimraf');
const pMap = require('p-map');
import {promisify} from 'node:util';
import path from 'node:path';
import process from 'node:process';
import globby from 'globby';
import isGlob from 'is-glob';
import slash from 'slash';
import gracefulFs from 'graceful-fs';
import isPathCwd from 'is-path-cwd';
import isPathInside from 'is-path-inside';
import rimraf from 'rimraf';
import pMap from 'p-map';

const rimrafP = promisify(rimraf);

Expand All @@ -25,7 +25,7 @@ const rimrafOptions = {
rmdir: gracefulFs.rmdir,
rmdirSync: gracefulFs.rmdirSync,
readdir: gracefulFs.readdir,
readdirSync: gracefulFs.readdirSync
readdirSync: gracefulFs.readdirSync,
};

function safeCheck(file, cwd) {
Expand All @@ -52,25 +52,25 @@ function normalizePatterns(patterns) {
return patterns;
}

module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
export async function deleteAsync(patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options
...options,
};

patterns = normalizePatterns(patterns);

const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));
const paths = await globby(patterns, options);
const files = paths.sort((a, b) => b.localeCompare(a));

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

Expand All @@ -92,7 +92,7 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgres
onProgress({
totalCount: files.length,
deletedCount,
percent: deletedCount / files.length
percent: deletedCount / files.length,
});

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

return removedFiles;
};
}

module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
export function deleteSync(patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options
...options,
};

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

return removedFiles;
};
}

0 comments on commit 106d7d8

Please sign in to comment.