diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock 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.d.ts b/index.d.ts new file mode 100644 index 0000000..f0baef3 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,62 @@ +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 { + /** + * 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 | SortKeys; + /** + * Passed into `JSON.stringify`. + */ + replacer?: Replacer | Array; + /** + * Mode used when writing the file. + * + * @default 0o666 + */ + mode?: number; +} + +/** + * Stringify and write JSON to a file atomically. + * + * Creates directories for you as needed. + * + * @example + * import * as writeJsonFile from 'write-json-file'; + * + * writeJsonFile.sync('foo.json', {foo: true}); + * console.log('done'); + */ +export function sync(filepath: string, data: JSONStringifyable, options?: Options): void; + +/** + * Stringify and write JSON to a file atomically. + * + * Creates directories for you as needed. + * + * @example + * import writeJsonFile from 'write-json-file'; + * + * (async () => { + * await writeJsonFile('foo.json', {foo: true}); + * console.log('done'); + * })(); + */ +export default function writeJsonFile(filepath: string, data: JSONStringifyable, options?: Options): Promise; diff --git a/index.js b/index.js index cbd7e70..7864bfa 100644 --- a/index.js +++ b/index.js @@ -44,11 +44,12 @@ const main = (fp, data, opts) => { }; const mainSync = (fp, data, opts) => { - let indent = opts.indent; + let {indent} = 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') { @@ -62,11 +63,13 @@ const mainSync = (fp, data, opts) => { return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode}); }; -module.exports = (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 169c7a8..0411c38 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=4" + "node": ">=6" }, "scripts": { "test": "xo && ava" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "write", @@ -44,5 +45,10 @@ "ava": "*", "tempfile": "^2.0.0", "xo": "*" + }, + "xo": { + "ignores": [ + "index.d.ts" + ] } }