Skip to content

Commit 8a602ea

Browse files
DenisCarrieresindresorhus
authored andcommitted
Add TypeScript definition and require Node.js 6 (#17)
1 parent e4c9b99 commit 8a602ea

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
yarn.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3+
- '10'
34
- '8'
45
- '6'
5-
- '4'

index.d.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export type Replacer = (key: string, value: any) => void;
2+
export type SortKeys = (a: string, b: string) => number;
3+
export type JSONStringifyable = object | number | string;
4+
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+
indent?: string | number | null;
12+
/**
13+
* Detect indentation automatically if the file exists.
14+
*
15+
* @default false
16+
*/
17+
detectIndent?: boolean;
18+
/**
19+
* Sort the keys recursively. Optionally pass in a compare function.
20+
*
21+
* @default false
22+
*/
23+
sortKeys?: boolean | SortKeys;
24+
/**
25+
* Passed into `JSON.stringify`.
26+
*/
27+
replacer?: Replacer | Array<number | string>;
28+
/**
29+
* Mode used when writing the file.
30+
*
31+
* @default 0o666
32+
*/
33+
mode?: number;
34+
}
35+
36+
/**
37+
* Stringify and write JSON to a file atomically.
38+
*
39+
* Creates directories for you as needed.
40+
*
41+
* @example
42+
* import * as writeJsonFile from 'write-json-file';
43+
*
44+
* writeJsonFile.sync('foo.json', {foo: true});
45+
* console.log('done');
46+
*/
47+
export function sync(filepath: string, data: JSONStringifyable, options?: Options): void;
48+
49+
/**
50+
* Stringify and write JSON to a file atomically.
51+
*
52+
* Creates directories for you as needed.
53+
*
54+
* @example
55+
* import writeJsonFile from 'write-json-file';
56+
*
57+
* (async () => {
58+
* await writeJsonFile('foo.json', {foo: true});
59+
* console.log('done');
60+
* })();
61+
*/
62+
export default function writeJsonFile(filepath: string, data: JSONStringifyable, options?: Options): Promise<void>;

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ const main = (fp, data, opts) => {
4444
};
4545

4646
const mainSync = (fp, data, opts) => {
47-
let indent = opts.indent;
47+
let {indent} = opts;
4848

4949
if (opts.detectIndent) {
5050
try {
5151
const file = fs.readFileSync(fp, 'utf8');
52+
// eslint-disable-next-line prefer-destructuring
5253
indent = detectIndent(file).indent;
5354
} catch (err) {
5455
if (err.code !== 'ENOENT') {
@@ -62,11 +63,13 @@ const mainSync = (fp, data, opts) => {
6263
return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode});
6364
};
6465

65-
module.exports = (fp, data, opts) => {
66+
const writeJsonFile = (fp, data, opts) => {
6667
return makeDir(path.dirname(fp), {fs})
6768
.then(() => init(main, fp, data, opts));
6869
};
6970

71+
module.exports = writeJsonFile;
72+
module.exports.default = writeJsonFile;
7073
module.exports.sync = (fp, data, opts) => {
7174
makeDir.sync(path.dirname(fp), {fs});
7275
init(mainSync, fp, data, opts);

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=4"
13+
"node": ">=6"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"write",
@@ -44,5 +45,10 @@
4445
"ava": "*",
4546
"tempfile": "^2.0.0",
4647
"xo": "*"
48+
},
49+
"xo": {
50+
"ignores": [
51+
"index.d.ts"
52+
]
4753
}
4854
}

0 commit comments

Comments
 (0)