diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..78eda93 --- /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 + * + * import del from '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" } }