Skip to content

Commit 7ba9663

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#23)
1 parent 70ebfd1 commit 7ba9663

File tree

4 files changed

+101
-80
lines changed

4 files changed

+101
-80
lines changed

index.d.ts

+77-58
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,85 @@
1-
export type Replacer = (this: unknown, key: string, value: unknown) => unknown;
2-
export type SortKeys = (a: string, b: string) => number;
3-
export type JSONStringifyable = string | number | boolean | null | object;
1+
declare namespace writeJsonFile {
2+
type Replacer = (this: unknown, key: string, value: any) => unknown;
3+
type SortKeys = (a: string, b: string) => number;
4+
type JSONStringifyable = string | number | boolean | null | object;
45

5-
export interface Options {
6-
/**
7-
* Indentation as a string or number of spaces. Pass in null for no formatting.
8-
*
9-
* @default '\t'
10-
*/
11-
readonly indent?: string | number | null;
6+
interface Options {
7+
/**
8+
Indentation as a string or number of spaces. Pass in null for no formatting.
129
13-
/**
14-
* Detect indentation automatically if the file exists.
15-
*
16-
* @default false
17-
*/
18-
readonly detectIndent?: boolean;
10+
@default '\t'
11+
*/
12+
readonly indent?: string | number | null;
1913

20-
/**
21-
* Sort the keys recursively. Optionally pass in a compare function.
22-
*
23-
* @default false
24-
*/
25-
readonly sortKeys?: boolean | SortKeys;
14+
/**
15+
Detect indentation automatically if the file exists.
16+
17+
@default false
18+
*/
19+
readonly detectIndent?: boolean;
20+
21+
/**
22+
Sort the keys recursively. Optionally pass in a compare function.
23+
24+
@default false
25+
*/
26+
readonly sortKeys?: boolean | SortKeys;
27+
28+
/**
29+
Passed into `JSON.stringify`.
30+
*/
31+
readonly replacer?: Replacer | Array<number | string>;
32+
33+
/**
34+
Mode used when writing the file.
35+
36+
@default 0o666
37+
*/
38+
readonly mode?: number;
39+
}
40+
}
2641

42+
declare const writeJsonFile: {
2743
/**
28-
* Passed into `JSON.stringify`.
29-
*/
30-
readonly replacer?: Replacer | Array<number | string>;
44+
Stringify and write JSON to a file atomically.
45+
46+
Creates directories for you as needed.
47+
48+
@example
49+
```
50+
import writeJsonFile = require('write-json-file');
51+
52+
(async () => {
53+
await writeJsonFile('foo.json', {foo: true});
54+
})();
55+
```
56+
*/
57+
(
58+
filepath: string,
59+
data: writeJsonFile.JSONStringifyable,
60+
options?: writeJsonFile.Options
61+
): Promise<void>;
3162

3263
/**
33-
* Mode used when writing the file.
34-
*
35-
* @default 0o666
36-
*/
37-
readonly mode?: number;
38-
}
64+
Stringify and write JSON to a file atomically.
65+
66+
Creates directories for you as needed.
67+
68+
@example
69+
```
70+
import writeJsonFile = require('write-json-file');
71+
72+
writeJsonFile.sync('foo.json', {foo: true});
73+
```
74+
*/
75+
sync(
76+
filepath: string,
77+
data: writeJsonFile.JSONStringifyable,
78+
options?: writeJsonFile.Options
79+
): void;
80+
81+
// TODO: Remove this for the next major release
82+
default: typeof writeJsonFile;
83+
};
3984

40-
/**
41-
* Stringify and write JSON to a file atomically.
42-
*
43-
* Creates directories for you as needed.
44-
*
45-
* @example
46-
*
47-
* import * as writeJsonFile from 'write-json-file';
48-
*
49-
* writeJsonFile.sync('foo.json', {foo: true});
50-
*/
51-
export function sync(filepath: string, data: JSONStringifyable, options?: Options): void;
52-
53-
/**
54-
* Stringify and write JSON to a file atomically.
55-
*
56-
* Creates directories for you as needed.
57-
*
58-
* @example
59-
*
60-
* import writeJsonFile from 'write-json-file';
61-
*
62-
* (async () => {
63-
* await writeJsonFile('foo.json', {foo: true});
64-
* })();
65-
*/
66-
export default function writeJsonFile(filepath: string, data: JSONStringifyable, options?: Options): Promise<void>;
85+
export = writeJsonFile;

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const writeJsonFile = (filePath, data, options) => {
6868
};
6969

7070
module.exports = writeJsonFile;
71+
// TODO: Remove this for the next major release
7172
module.exports.default = writeJsonFile;
7273
module.exports.sync = (filePath, data, options) => {
7374
makeDir.sync(path.dirname(filePath), {fs});

index.test-d.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {expectType} from 'tsd-check';
2-
import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.';
1+
import {expectType} from 'tsd';
2+
import writeJsonFile = require('.');
3+
import {sync, Replacer, SortKeys, JSONStringifyable} from '.';
34

45
expectType<JSONStringifyable>('🦄');
56
expectType<JSONStringifyable>(1);
@@ -23,18 +24,18 @@ expectType<Replacer>(() => () => 'foo');
2324
expectType<Replacer>((key: string) => key.toUpperCase());
2425
expectType<Replacer>((key: string, value: string) => (key + value).toUpperCase());
2526

26-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}));
27-
expectType<void>(await writeJsonFile('unicorn.json', '🦄'));
28-
expectType<void>(await writeJsonFile('date.json', new Date()));
29-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
30-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
31-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
32-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
33-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
34-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
35-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
36-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
37-
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));
27+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}));
28+
expectType<Promise<void>>(writeJsonFile('unicorn.json', '🦄'));
29+
expectType<Promise<void>>(writeJsonFile('date.json', new Date()));
30+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
31+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
32+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
33+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
34+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
35+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
36+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
37+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
38+
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));
3839

3940
expectType<void>(sync('unicorn.json', {unicorn: '🦄'}));
4041
expectType<void>(sync('unicorn.json', '🦄'));

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -35,16 +35,16 @@
3535
],
3636
"dependencies": {
3737
"detect-indent": "^5.0.0",
38-
"graceful-fs": "^4.1.2",
39-
"make-dir": "^1.0.0",
40-
"pify": "^4.0.0",
38+
"graceful-fs": "^4.1.15",
39+
"make-dir": "^2.1.0",
40+
"pify": "^4.0.1",
4141
"sort-keys": "^2.0.0",
42-
"write-file-atomic": "^2.0.0"
42+
"write-file-atomic": "^2.4.2"
4343
},
4444
"devDependencies": {
45-
"ava": "*",
45+
"ava": "^1.4.1",
4646
"tempfile": "^2.0.0",
47-
"tsd-check": "^0.2.1",
48-
"xo": "*"
47+
"tsd": "^0.7.2",
48+
"xo": "^0.24.0"
4949
}
5050
}

0 commit comments

Comments
 (0)