Skip to content

Commit 379001f

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#14)
1 parent 1c36213 commit 379001f

File tree

4 files changed

+73
-41
lines changed

4 files changed

+73
-41
lines changed

index.d.ts

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,69 @@
11
/// <reference types="node"/>
22
import * as fs from 'fs';
33

4-
export interface Options {
4+
declare namespace makeDir {
5+
interface Options {
6+
/**
7+
Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
8+
9+
@default 0o777 & (~process.umask())
10+
*/
11+
readonly mode?: number;
12+
13+
/**
14+
Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
15+
16+
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.
17+
18+
@default require('fs')
19+
*/
20+
readonly fs?: typeof fs;
21+
}
22+
}
23+
24+
declare const makeDir: {
525
/**
6-
* Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
7-
*
8-
* @default 0o777 & (~process.umask())
9-
*/
10-
readonly mode?: number;
26+
Make a directory and its parents if needed - Think `mkdir -p`.
27+
28+
@param path - Directory to create.
29+
@returns The path to the created directory.
30+
31+
@example
32+
```
33+
import makeDir = require('make-dir');
34+
35+
(async () => {
36+
const path = await makeDir('unicorn/rainbow/cake');
37+
38+
console.log(path);
39+
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
40+
41+
// Multiple directories:
42+
const paths = await Promise.all([
43+
makeDir('unicorn/rainbow'),
44+
makeDir('foo/bar')
45+
]);
46+
47+
console.log(paths);
48+
// [
49+
// '/Users/sindresorhus/fun/unicorn/rainbow',
50+
// '/Users/sindresorhus/fun/foo/bar'
51+
// ]
52+
})();
53+
```
54+
*/
55+
(path: string, options?: makeDir.Options): Promise<string>;
1156

1257
/**
13-
* Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
14-
*
15-
* 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.
16-
*
17-
* @default require('fs')
18-
*/
19-
readonly fs?: typeof fs;
20-
}
58+
Synchronously make a directory and its parents if needed - Think `mkdir -p`.
59+
60+
@param path - Directory to create.
61+
@returns The path to the created directory.
62+
*/
63+
sync(path: string, options?: makeDir.Options): string;
64+
65+
// TODO: Remove this for the next major release
66+
default: typeof makeDir;
67+
};
2168

22-
/**
23-
* Make a directory and its parents if needed - Think `mkdir -p`.
24-
*
25-
* @param path - Directory to create.
26-
* @returns A `Promise` for the path to the created directory.
27-
*/
28-
export default function makeDir(
29-
path: string,
30-
options?: Options
31-
): Promise<string>;
32-
33-
/**
34-
* Synchronously make a directory and its parents if needed - Think `mkdir -p`.
35-
*
36-
* @param path - Directory to create.
37-
* @returns The path to the created directory.
38-
*/
39-
export function sync(path: string, options?: Options): string;
69+
export = makeDir;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const makeDir = (input, options) => Promise.resolve().then(() => {
8585
});
8686

8787
module.exports = makeDir;
88+
// TODO: Remove this for the next major release
8889
module.exports.default = makeDir;
8990

9091
module.exports.sync = (input, options) => {

index.test-d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {expectType} from 'tsd-check';
2-
import makeDir, {sync as makeDirSync} from '.';
1+
import {expectType} from 'tsd';
2+
import makeDir = require('.');
3+
import {sync as makeDirSync} from '.';
34
import * as fs from 'fs';
45
import * as gfs from 'graceful-fs';
56

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && nyc ava && tsd-check"
16+
"test": "xo && nyc ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -46,14 +46,14 @@
4646
},
4747
"devDependencies": {
4848
"@types/graceful-fs": "^4.1.3",
49-
"@types/node": "^11.10.4",
50-
"ava": "^1.2.0",
51-
"codecov": "^3.0.0",
52-
"graceful-fs": "^4.1.11",
53-
"nyc": "^13.1.0",
49+
"@types/node": "^11.12.2",
50+
"ava": "^1.4.0",
51+
"codecov": "^3.2.0",
52+
"graceful-fs": "^4.1.15",
53+
"nyc": "^13.3.0",
5454
"path-type": "^3.0.0",
5555
"tempy": "^0.2.1",
56-
"tsd-check": "^0.3.0",
56+
"tsd": "^0.7.1",
5757
"xo": "^0.24.0"
5858
}
5959
}

0 commit comments

Comments
 (0)