Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #81

Merged
merged 2 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*
* const del = require('del');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import syntax. This is supposed to be a TS usage example.

Suggested change
* const del = require('del');
* 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 '.';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I the only one that finds this ugly? Should the sync just be a property of the default export instead of a named export? Or should we name the named export delSync so it doesn't have to be renamed to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of default imports. They're always kinda ugly, everyone names the thing differently and the tooling has hard times determining what you actually want to import. It's much easier with named exports.


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"
}
}