From 96746ac97c7922971fe33ef14f421d6bd83d6de0 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sun, 3 Mar 2019 17:06:30 +0100 Subject: [PATCH 1/2] Add TypeScript definition --- index.d.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 5 +++- index.test-d.ts | 22 ++++++++++++++++++ package.json | 12 ++++++---- 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..cafde62 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,62 @@ +import {IOptions as GlobOptions} from 'glob'; + +interface Options extends Readonly { + /** + * Allow deleting the current working directory and outside. + * + * @default false + */ + readonly force?: boolean; + + /** + * See what would be deleted. + * + * @default false + * + * @example + * + * const del = require('del'); + * + * (async () => { + * const deletedPaths = await del(['tmp/*.js'], {dryRun: true}); + * + * console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n')); + * })(); + */ + readonly dryRun?: boolean; + + /** + * Concurrency limit. Minimum: `1`. + * + * @default Infinity + */ + readonly concurrency?: number; +} + +/** + * Delete files and folders using glob patterns. + * + * @param patterns - See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage). + * - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) + * - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) + * @param options - See the [`glob` options](https://github.com/isaacs/node-glob#options). + * @returns A promise for an array of deleted paths. + */ +export default function del( + patterns: string | ReadonlyArray, + options?: Options +): Promise; + +/** + * Synchronously delete files and folders using glob patterns. + * + * @param patterns - See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage). + * - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) + * - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) + * @param options - See the [`glob` options](https://github.com/isaacs/node-glob#options). + * @returns An array of deleted paths. + */ +export function sync( + patterns: string | ReadonlyArray, + options?: Options +): string[]; diff --git a/index.js b/index.js index 714ebba..248b92b 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ function safeCheck(file) { } } -module.exports = (patterns, options) => { +const del = (patterns, options) => { options = Object.assign({}, options); const {force, dryRun} = options; @@ -43,6 +43,9 @@ module.exports = (patterns, options) => { return globby(patterns, options).then(files => pMap(files, mapper, options)); }; +module.exports = del; +module.exports.default = del; + module.exports.sync = (patterns, options) => { options = Object.assign({}, options); diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..d2ea883 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,22 @@ +import {expectType} from 'tsd-check'; +import del, {sync as delSync} from '.'; + +let paths = ['tmp/*.js', '!tmp/unicorn.js']; + +// Del +expectType>(del('tmp/*.js')); +expectType>(del(paths)); + +expectType>(del(paths, {force: true})); +expectType>(del(paths, {dryRun: true})); +expectType>(del(paths, {concurrency: 20})); +expectType>(del(paths, {cwd: ''})); + +// Del (sync) +expectType(delSync('tmp/*.js')); +expectType(delSync(paths)); + +expectType(delSync(paths, {force: true})); +expectType(delSync(paths, {dryRun: true})); +expectType(delSync(paths, {concurrency: 20})); +expectType(delSync(paths, {cwd: ''})); diff --git a/package.json b/package.json index 3b1c5a1..351f7ad 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd-check" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "delete", @@ -53,9 +54,10 @@ "rimraf": "^2.6.2" }, "devDependencies": { - "ava": "^0.25.0", - "make-dir": "^1.3.0", + "ava": "^1.2.1", + "make-dir": "^2.0.0", "tempy": "^0.2.1", - "xo": "^0.23.0" + "tsd-check": "^0.3.0", + "xo": "^0.24.0" } } From 4f8fe95f1a77b38592233de30c257d2090e102d3 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 4 Mar 2019 02:10:07 +0700 Subject: [PATCH 2/2] Update index.d.ts --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index cafde62..78eda93 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,7 +15,7 @@ interface Options extends Readonly { * * @example * - * const del = require('del'); + * import del from 'del'; * * (async () => { * const deletedPaths = await del(['tmp/*.js'], {dryRun: true});