Skip to content

Commit

Permalink
Add TypeScript definition (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 3, 2019
1 parent e73cc8a commit 34c771e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
62 changes: 62 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {IOptions as GlobOptions} from 'glob';

interface Options extends Readonly<GlobOptions> {
/**
* 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<string>,
options?: Options
): Promise<string[]>;

/**
* 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<string>,
options?: Options
): string[];
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function safeCheck(file) {
}
}

module.exports = (patterns, options) => {
const del = (patterns, options) => {
options = Object.assign({}, options);

const {force, dryRun} = options;
Expand All @@ -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);

Expand Down
22 changes: 22 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expectType} from 'tsd-check';
import del, {sync as delSync} from '.';

let paths = ['tmp/*.js', '!tmp/unicorn.js'];

// Del
expectType<Promise<string[]>>(del('tmp/*.js'));
expectType<Promise<string[]>>(del(paths));

expectType<Promise<string[]>>(del(paths, {force: true}));
expectType<Promise<string[]>>(del(paths, {dryRun: true}));
expectType<Promise<string[]>>(del(paths, {concurrency: 20}));
expectType<Promise<string[]>>(del(paths, {cwd: ''}));

// Del (sync)
expectType<string[]>(delSync('tmp/*.js'));
expectType<string[]>(delSync(paths));

expectType<string[]>(delSync(paths, {force: true}));
expectType<string[]>(delSync(paths, {dryRun: true}));
expectType<string[]>(delSync(paths, {concurrency: 20}));
expectType<string[]>(delSync(paths, {cwd: ''}));
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 34c771e

Please sign in to comment.