From d2332fda94799f5c0b80767a96d49975cb48752c Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 15:23:06 -0400 Subject: [PATCH 01/15] Add TypeScript definition --- index.d.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 6 ++++++ package.json | 4 +++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8896e93 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,54 @@ +type Replacer = (key: string, value: any) => void; + +interface Options { + indent?: string | number | null; + detectIndent?: boolean; + sortKeys?: boolean | ((a: string, b: string) => number); + replacer?: Replacer | Array | null; + mode?: number; +} + +/** + * Stringify and write JSON to a file atomically + * + * Creates directories for you as needed. + * + * @param {string} filepath Filepath + * @param {any} data Data + * @param {object} [options] Optional parameters + * @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. + * @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. + * @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. + * @param {function|Array|null} [options.replacer] Passed into JSON.stringify. + * @param {number} [options.mode=0o666] Mode used when writing the file. + * @returns {void} + * @example + * const writeJsonFile = require('write-json-file'); + * + * writeJsonFile.sync('foo.json', {foo: true}); + * console.log('done'); + */ +export function sync(filepath: string, data: any, options?: Options): void; + +/** + * Stringify and write JSON to a file atomically + * + * Creates directories for you as needed. + * + * @param {string} filepath Filepath + * @param {any} data Data + * @param {object} [options] Optional parameters + * @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. + * @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. + * @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. + * @param {function|Array|null} [options.replacer] Passed into JSON.stringify. + * @param {number} [options.mode=0o666] Mode used when writing the file. + * @returns {Promise} + * @example + * const writeJsonFile = require('write-json-file'); + * + * writeJsonFile('foo.json', {foo: true}).then(() => { + * console.log('done'); + * }); + */ +export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise; diff --git a/index.js b/index.js index cbd7e70..5e97a50 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,12 @@ module.exports = (fp, data, opts) => { .then(() => init(main, fp, data, opts)); }; +// Support for Typescript default export +module.exports.default = (fp, data, opts) => { + return makeDir(path.dirname(fp), {fs}) + .then(() => init(main, fp, data, opts)); +}; + module.exports.sync = (fp, data, opts) => { makeDir.sync(path.dirname(fp), {fs}); init(mainSync, fp, data, opts); diff --git a/package.json b/package.json index 169c7a8..a718a7f 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,10 @@ "test": "xo && ava" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], + "types": "index.d.ts", "keywords": [ "write", "json", From b391dd43475413f1b23a13a901f3073067ec26b2 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 15:49:21 -0400 Subject: [PATCH 02/15] Fix tests --- .travis.yml | 2 +- index.js | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d69d74..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' - - '4' diff --git a/index.js b/index.js index 5e97a50..3ff7016 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,7 @@ const main = (fp, data, opts) => { }; const mainSync = (fp, data, opts) => { - let indent = opts.indent; + let {indent} = opts; if (opts.detectIndent) { try { diff --git a/package.json b/package.json index a718a7f..2461d7b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=4" + "node": ">=6" }, "scripts": { "test": "xo && ava" From ffa88230c91328769f40888b997404813223ae33 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 16:28:09 -0400 Subject: [PATCH 03/15] Update based on comments --- .gitignore | 1 + index.d.ts | 44 ++++++++++++++++++++++---------------------- index.js | 10 +++------- package.json | 3 +-- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..97008e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 8896e93..25b5894 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,25 @@ type Replacer = (key: string, value: any) => void; interface Options { + /** + * Indentation as a string or number of spaces. Pass in null for no formatting. + */ indent?: string | number | null; + /** + * Detect indentation automatically if the file exists. + */ detectIndent?: boolean; + /** + * Sort the keys recursively. Optionally pass in a compare function. + */ sortKeys?: boolean | ((a: string, b: string) => number); - replacer?: Replacer | Array | null; + /** + * Passed into JSON.stringify. + */ + replacer?: Replacer | Array; + /** + * Mode used when writing the file. + */ mode?: number; } @@ -13,17 +28,9 @@ interface Options { * * Creates directories for you as needed. * - * @param {string} filepath Filepath - * @param {any} data Data - * @param {object} [options] Optional parameters - * @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. - * @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. - * @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. - * @param {function|Array|null} [options.replacer] Passed into JSON.stringify. - * @param {number} [options.mode=0o666] Mode used when writing the file. * @returns {void} * @example - * const writeJsonFile = require('write-json-file'); + * import * as writeJsonFile from 'write-json-file'; * * writeJsonFile.sync('foo.json', {foo: true}); * console.log('done'); @@ -35,20 +42,13 @@ export function sync(filepath: string, data: any, options?: Options): void; * * Creates directories for you as needed. * - * @param {string} filepath Filepath - * @param {any} data Data - * @param {object} [options] Optional parameters - * @param {string|number|null} [options.indent='\t'] Indentation as a string or number of spaces. Pass in null for no formatting. - * @param {boolean} [options.detectIndent=false] Detect indentation automatically if the file exists. - * @param {boolean|function} [options.sortKeys=false] Sort the keys recursively. Optionally pass in a compare function. - * @param {function|Array|null} [options.replacer] Passed into JSON.stringify. - * @param {number} [options.mode=0o666] Mode used when writing the file. * @returns {Promise} * @example - * const writeJsonFile = require('write-json-file'); + * import writeJsonFile from 'write-json-file'; * - * writeJsonFile('foo.json', {foo: true}).then(() => { - * console.log('done'); - * }); + * (async () => { + * await writeJsonFile('foo.json', {foo: true}); + * console.log('done'); + * })(); */ export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise; diff --git a/index.js b/index.js index 3ff7016..d06d394 100644 --- a/index.js +++ b/index.js @@ -62,17 +62,13 @@ const mainSync = (fp, data, opts) => { return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode}); }; -module.exports = (fp, data, opts) => { - return makeDir(path.dirname(fp), {fs}) - .then(() => init(main, fp, data, opts)); -}; - -// Support for Typescript default export -module.exports.default = (fp, data, opts) => { +const writeJsonFile = (fp, data, opts) => { return makeDir(path.dirname(fp), {fs}) .then(() => init(main, fp, data, opts)); }; +module.exports = writeJsonFile; +module.exports.default = writeJsonFile; module.exports.sync = (fp, data, opts) => { makeDir.sync(path.dirname(fp), {fs}); init(mainSync, fp, data, opts); diff --git a/package.json b/package.json index 2461d7b..84d773b 100644 --- a/package.json +++ b/package.json @@ -13,13 +13,12 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo --ignore index.d.ts && ava" }, "files": [ "index.js", "index.d.ts" ], - "types": "index.d.ts", "keywords": [ "write", "json", From 3e683e80b87c54fce31ed33880e6cf5db3c7da57 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 16:34:25 -0400 Subject: [PATCH 04/15] Add dot & ignore xo --- index.d.ts | 4 ++-- package.json | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 25b5894..db7a118 100644 --- a/index.d.ts +++ b/index.d.ts @@ -24,7 +24,7 @@ interface Options { } /** - * Stringify and write JSON to a file atomically + * Stringify and write JSON to a file atomically. * * Creates directories for you as needed. * @@ -38,7 +38,7 @@ interface Options { export function sync(filepath: string, data: any, options?: Options): void; /** - * Stringify and write JSON to a file atomically + * Stringify and write JSON to a file atomically. * * Creates directories for you as needed. * diff --git a/package.json b/package.json index 84d773b..c465e51 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo --ignore index.d.ts && ava" + "test": "xo && ava" }, "files": [ "index.js", @@ -45,5 +45,10 @@ "ava": "*", "tempfile": "^2.0.0", "xo": "*" - } + }, + "xo": { + "ignores": [ + "index.d.ts" + ] + } } From 5b9a733b7df9fac0bf1670b8b897d0c518a05aa6 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 16:39:43 -0400 Subject: [PATCH 05/15] remove {returns} & fix indentation --- index.d.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index db7a118..76c3aa5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -28,7 +28,6 @@ interface Options { * * Creates directories for you as needed. * - * @returns {void} * @example * import * as writeJsonFile from 'write-json-file'; * @@ -42,13 +41,12 @@ export function sync(filepath: string, data: any, options?: Options): void; * * Creates directories for you as needed. * - * @returns {Promise} * @example * import writeJsonFile from 'write-json-file'; * * (async () => { - * await writeJsonFile('foo.json', {foo: true}); - * console.log('done'); + * await writeJsonFile('foo.json', {foo: true}); + * console.log('done'); * })(); */ export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise; From 2d1dd15e26161dd4d42750fb2bf5db0995f05522 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 16:53:37 -0400 Subject: [PATCH 06/15] Update example --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 76c3aa5..bc29a85 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,7 +29,7 @@ interface Options { * Creates directories for you as needed. * * @example - * import * as writeJsonFile from 'write-json-file'; + * import writeJsonFile from 'write-json-file'; * * writeJsonFile.sync('foo.json', {foo: true}); * console.log('done'); From 1c9b6e5d3f8fadb1d743e35a20dae6fb82460b98 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 17:06:33 -0400 Subject: [PATCH 07/15] Eslint disable --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index d06d394..7864bfa 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,7 @@ const mainSync = (fp, data, opts) => { if (opts.detectIndent) { try { const file = fs.readFileSync(fp, 'utf8'); + // eslint-disable-next-line prefer-destructuring indent = detectIndent(file).indent; } catch (err) { if (err.code !== 'ENOENT') { From 5a45486876a1e9ec51db5e50339fd09b172c8635 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Mon, 20 Aug 2018 17:10:06 -0400 Subject: [PATCH 08/15] Fix indent --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index bc29a85..801b7a8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -46,7 +46,7 @@ export function sync(filepath: string, data: any, options?: Options): void; * * (async () => { * await writeJsonFile('foo.json', {foo: true}); - * console.log('done'); + * console.log('done'); * })(); */ export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise; From 54aebc1377af405225d54f90d0030228b91eaf3c Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Tue, 21 Aug 2018 00:50:34 -0400 Subject: [PATCH 09/15] Convert tabs to spaces Future commit will convert package.json to tabs --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c465e51..0411c38 100644 --- a/package.json +++ b/package.json @@ -47,8 +47,8 @@ "xo": "*" }, "xo": { - "ignores": [ - "index.d.ts" - ] - } + "ignores": [ + "index.d.ts" + ] + } } From 6a15428cd6ccc8d7e4a8c47faf851811307ac923 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Tue, 21 Aug 2018 00:53:26 -0400 Subject: [PATCH 10/15] Add defaults --- index.d.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 801b7a8..f31830a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,14 +3,20 @@ type Replacer = (key: string, value: any) => void; interface Options { /** * Indentation as a string or number of spaces. Pass in null for no formatting. + * + * Default: '\t' */ indent?: string | number | null; /** * Detect indentation automatically if the file exists. + * + * Default: false */ detectIndent?: boolean; /** * Sort the keys recursively. Optionally pass in a compare function. + * + * Default: false */ sortKeys?: boolean | ((a: string, b: string) => number); /** @@ -19,6 +25,8 @@ interface Options { replacer?: Replacer | Array; /** * Mode used when writing the file. + * + * Default: 0o666 */ mode?: number; } @@ -29,7 +37,7 @@ interface Options { * Creates directories for you as needed. * * @example - * import writeJsonFile from 'write-json-file'; + * import * as writeJsonFile from 'write-json-file'; * * writeJsonFile.sync('foo.json', {foo: true}); * console.log('done'); From d2c300bc4e8fc1cb8d82e728004354799971fd7c Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Tue, 21 Aug 2018 09:33:46 -0400 Subject: [PATCH 11/15] Add new line to .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 97008e5..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -yarn.lock \ No newline at end of file +yarn.lock From 8d85e0c850436c41fc4ef2e24e2d08b6ea9b2f00 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Tue, 21 Aug 2018 09:34:21 -0400 Subject: [PATCH 12/15] Add @default Ref: http://usejsdoc.org/tags-default.html --- index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index f31830a..2abd6ba 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,19 +4,19 @@ interface Options { /** * Indentation as a string or number of spaces. Pass in null for no formatting. * - * Default: '\t' + * @default '\t' */ indent?: string | number | null; /** * Detect indentation automatically if the file exists. * - * Default: false + * @default false */ detectIndent?: boolean; /** * Sort the keys recursively. Optionally pass in a compare function. * - * Default: false + * @default false */ sortKeys?: boolean | ((a: string, b: string) => number); /** @@ -26,7 +26,7 @@ interface Options { /** * Mode used when writing the file. * - * Default: 0o666 + * @default 0o666 */ mode?: number; } From 4df67a1ac8347c10d6e5e1c99f262c163e9b5e50 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Tue, 21 Aug 2018 10:01:59 -0400 Subject: [PATCH 13/15] Make `data` param "strict" --- index.d.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2abd6ba..095acaa 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,7 @@ -type Replacer = (key: string, value: any) => void; +export type Replacer = (key: string, value: any) => void; +export type SortKeys = (a: string, b: string) => number; -interface Options { +export interface Options { /** * Indentation as a string or number of spaces. Pass in null for no formatting. * @@ -18,7 +19,7 @@ interface Options { * * @default false */ - sortKeys?: boolean | ((a: string, b: string) => number); + sortKeys?: SortKeys | boolean; /** * Passed into JSON.stringify. */ @@ -42,7 +43,7 @@ interface Options { * writeJsonFile.sync('foo.json', {foo: true}); * console.log('done'); */ -export function sync(filepath: string, data: any, options?: Options): void; +export function sync(filepath: string, data: object | number | string, options?: Options): void; /** * Stringify and write JSON to a file atomically. @@ -57,4 +58,4 @@ export function sync(filepath: string, data: any, options?: Options): void; * console.log('done'); * })(); */ -export default function writeJsonFile(filepath: string, data: any, options?: Options): Promise; +export default function writeJsonFile(filepath: string, data: object | number | string, options?: Options): Promise; From 8062f518a1d2cd41a152f943c2c0fc1c08596426 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Thu, 23 Aug 2018 13:08:37 -0400 Subject: [PATCH 14/15] Add JSONStringifyable --- index.d.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 095acaa..01e4ce7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,6 @@ export type Replacer = (key: string, value: any) => void; export type SortKeys = (a: string, b: string) => number; +export type JSONStringifyable = object | number | string; export interface Options { /** @@ -19,7 +20,7 @@ export interface Options { * * @default false */ - sortKeys?: SortKeys | boolean; + sortKeys?: boolean | SortKeys; /** * Passed into JSON.stringify. */ @@ -43,7 +44,7 @@ export interface Options { * writeJsonFile.sync('foo.json', {foo: true}); * console.log('done'); */ -export function sync(filepath: string, data: object | number | string, options?: Options): void; +export function sync(filepath: string, data: JSONStringifyable, options?: Options): void; /** * Stringify and write JSON to a file atomically. @@ -58,4 +59,4 @@ export function sync(filepath: string, data: object | number | string, options?: * console.log('done'); * })(); */ -export default function writeJsonFile(filepath: string, data: object | number | string, options?: Options): Promise; +export default function writeJsonFile(filepath: string, data: JSONStringifyable, options?: Options): Promise; From 1f282fb767c5c0c958e9f551335b4884b8fad518 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 27 Aug 2018 14:15:36 +0700 Subject: [PATCH 15/15] Update index.d.ts --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 01e4ce7..f0baef3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -22,7 +22,7 @@ export interface Options { */ sortKeys?: boolean | SortKeys; /** - * Passed into JSON.stringify. + * Passed into `JSON.stringify`. */ replacer?: Replacer | Array; /**