Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 1, 2019
1 parent 1c36213 commit 379001f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 41 deletions.
94 changes: 62 additions & 32 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
/// <reference types="node"/>
import * as fs from 'fs';

export interface Options {
declare namespace makeDir {
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;
}
}

declare const makeDir: {
/**
* Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
*
* @default 0o777 & (~process.umask())
*/
readonly mode?: number;
Make a directory and its parents if needed - Think `mkdir -p`.
@param path - Directory to create.
@returns The path to the created directory.
@example
```
import makeDir = require('make-dir');
(async () => {
const path = await makeDir('unicorn/rainbow/cake');
console.log(path);
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
// Multiple directories:
const paths = await Promise.all([
makeDir('unicorn/rainbow'),
makeDir('foo/bar')
]);
console.log(paths);
// [
// '/Users/sindresorhus/fun/unicorn/rainbow',
// '/Users/sindresorhus/fun/foo/bar'
// ]
})();
```
*/
(path: string, options?: makeDir.Options): Promise<string>;

/**
* 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;
}
Synchronously make a directory and its parents if needed - Think `mkdir -p`.
@param path - Directory to create.
@returns The path to the created directory.
*/
sync(path: string, options?: makeDir.Options): string;

// TODO: Remove this for the next major release
default: typeof makeDir;
};

/**
* 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;
export = makeDir;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const makeDir = (input, options) => Promise.resolve().then(() => {
});

module.exports = makeDir;
// TODO: Remove this for the next major release
module.exports.default = makeDir;

module.exports.sync = (input, options) => {
Expand Down
5 changes: 3 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import makeDir, {sync as makeDirSync} from '.';
import {expectType} from 'tsd';
import makeDir = require('.');
import {sync as makeDirSync} from '.';
import * as fs from 'fs';
import * as gfs from 'graceful-fs';

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava && tsd-check"
"test": "xo && nyc ava && tsd"
},
"files": [
"index.js",
Expand Down Expand Up @@ -46,14 +46,14 @@
},
"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",
"@types/node": "^11.12.2",
"ava": "^1.4.0",
"codecov": "^3.2.0",
"graceful-fs": "^4.1.15",
"nyc": "^13.3.0",
"path-type": "^3.0.0",
"tempy": "^0.2.1",
"tsd-check": "^0.3.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}

0 comments on commit 379001f

Please sign in to comment.