diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3e1529c --- /dev/null +++ b/index.d.ts @@ -0,0 +1,39 @@ +/// +import * as fs from 'fs'; + +export 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; + +/** + * 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; diff --git a/index.js b/index.js index 7f2e0cf..4d95c91 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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); diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..9555f6d --- /dev/null +++ b/index.test-d.ts @@ -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>(makeDir('path/to/somewhere')); + +expectType>( + makeDir('path/to/somewhere', {mode: parseInt('777', 8)}) +); +expectType>(makeDir('path/to/somewhere', {fs})); +expectType>(makeDir('path/to/somewhere', {fs: gfs})); + +// MakeDir (sync) +expectType(makeDirSync('path/to/somewhere')); + +expectType( + makeDirSync('path/to/somewhere', {mode: parseInt('777', 8)}) +); +expectType(makeDirSync('path/to/somewhere', {fs})); +expectType(makeDirSync('path/to/somewhere', {fs: gfs})); diff --git a/package.json b/package.json index e799421..71f735c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } }