Skip to content

Commit

Permalink
Add TypeScript definition (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 3, 2019
1 parent c1294d7 commit 3e54190
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
39 changes: 39 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference types="node"/>
import * as fs from 'fs';

interface Options {
/**
* Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
*
* @default 0o777 & (~process.umask())
*/
readonly mode?: number;

/**
* Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
*
* Using a custom `fs` implementation will block the use of the native `recursive` option if `fs.mkdir` or `fs.mkdirSync` is not the native function.
*
* @default require('fs')
*/
readonly fs?: typeof fs;
}

/**
* Make a directory and its parents if needed - Think `mkdir -p`.
*
* @param path - Directory to create.
* @returns A `Promise` for the path to the created directory.
*/
export default function makeDir(
path: string,
options?: Options
): Promise<string>;

/**
* Synchronously make a directory and its parents if needed - Think `mkdir -p`.
*
* @param path - Directory to create.
* @returns The path to the created directory.
*/
export function sync(path: string, options?: Options): string;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const permissionError = pth => {
return error;
};

module.exports = (input, options) => Promise.resolve().then(() => {
const makeDir = (input, options) => Promise.resolve().then(() => {
checkPath(input);
options = Object.assign({}, defaults, options);

Expand Down Expand Up @@ -84,6 +84,9 @@ module.exports = (input, options) => Promise.resolve().then(() => {
return make(path.resolve(input));
});

module.exports = makeDir;
module.exports.default = makeDir;

module.exports.sync = (input, options) => {
checkPath(input);
options = Object.assign({}, defaults, 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 makeDir, {sync as makeDirSync} from '.';
import * as fs from 'fs';
import * as gfs from 'graceful-fs';

// MakeDir
expectType<Promise<string>>(makeDir('path/to/somewhere'));

expectType<Promise<string>>(
makeDir('path/to/somewhere', {mode: parseInt('777', 8)})
);
expectType<Promise<string>>(makeDir('path/to/somewhere', {fs}));
expectType<Promise<string>>(makeDir('path/to/somewhere', {fs: gfs}));

// MakeDir (sync)
expectType<string>(makeDirSync('path/to/somewhere'));

expectType<string>(
makeDirSync('path/to/somewhere', {mode: parseInt('777', 8)})
);
expectType<string>(makeDirSync('path/to/somewhere', {fs}));
expectType<string>(makeDirSync('path/to/somewhere', {fs: gfs}));
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && nyc ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"mkdir",
Expand Down Expand Up @@ -44,12 +45,15 @@
"semver": "^5.6.0"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.3",
"@types/node": "^11.10.4",
"ava": "^1.2.0",
"codecov": "^3.0.0",
"graceful-fs": "^4.1.11",
"nyc": "^13.1.0",
"path-type": "^3.0.0",
"tempy": "^0.2.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}

0 comments on commit 3e54190

Please sign in to comment.