diff --git a/index.d.ts b/index.d.ts index 962ecdd..981a2c8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,66 +1,85 @@ -export type Replacer = (this: unknown, key: string, value: unknown) => unknown; -export type SortKeys = (a: string, b: string) => number; -export type JSONStringifyable = string | number | boolean | null | object; +declare namespace writeJsonFile { + type Replacer = (this: unknown, key: string, value: any) => unknown; + type SortKeys = (a: string, b: string) => number; + type JSONStringifyable = string | number | boolean | null | object; -export interface Options { - /** - * Indentation as a string or number of spaces. Pass in null for no formatting. - * - * @default '\t' - */ - readonly indent?: string | number | null; + interface Options { + /** + Indentation as a string or number of spaces. Pass in null for no formatting. - /** - * Detect indentation automatically if the file exists. - * - * @default false - */ - readonly detectIndent?: boolean; + @default '\t' + */ + readonly indent?: string | number | null; - /** - * Sort the keys recursively. Optionally pass in a compare function. - * - * @default false - */ - readonly sortKeys?: boolean | SortKeys; + /** + Detect indentation automatically if the file exists. + + @default false + */ + readonly detectIndent?: boolean; + + /** + Sort the keys recursively. Optionally pass in a compare function. + + @default false + */ + readonly sortKeys?: boolean | SortKeys; + + /** + Passed into `JSON.stringify`. + */ + readonly replacer?: Replacer | Array; + + /** + Mode used when writing the file. + + @default 0o666 + */ + readonly mode?: number; + } +} +declare const writeJsonFile: { /** - * Passed into `JSON.stringify`. - */ - readonly replacer?: Replacer | Array; + Stringify and write JSON to a file atomically. + + Creates directories for you as needed. + + @example + ``` + import writeJsonFile = require('write-json-file'); + + (async () => { + await writeJsonFile('foo.json', {foo: true}); + })(); + ``` + */ + ( + filepath: string, + data: writeJsonFile.JSONStringifyable, + options?: writeJsonFile.Options + ): Promise; /** - * Mode used when writing the file. - * - * @default 0o666 - */ - readonly mode?: number; -} + Stringify and write JSON to a file atomically. + + Creates directories for you as needed. + + @example + ``` + import writeJsonFile = require('write-json-file'); + + writeJsonFile.sync('foo.json', {foo: true}); + ``` + */ + sync( + filepath: string, + data: writeJsonFile.JSONStringifyable, + options?: writeJsonFile.Options + ): void; + + // TODO: Remove this for the next major release + default: typeof writeJsonFile; +}; -/** - * 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}); - */ -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}); - * })(); - */ -export default function writeJsonFile(filepath: string, data: JSONStringifyable, options?: Options): Promise; +export = writeJsonFile; diff --git a/index.js b/index.js index fb35f6f..12e637d 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,7 @@ const writeJsonFile = (filePath, data, options) => { }; module.exports = writeJsonFile; +// TODO: Remove this for the next major release module.exports.default = writeJsonFile; module.exports.sync = (filePath, data, options) => { makeDir.sync(path.dirname(filePath), {fs}); diff --git a/index.test-d.ts b/index.test-d.ts index a7a22c0..71835cc 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,6 @@ -import {expectType} from 'tsd-check'; -import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.'; +import {expectType} from 'tsd'; +import writeJsonFile = require('.'); +import {sync, Replacer, SortKeys, JSONStringifyable} from '.'; expectType('🦄'); expectType(1); @@ -23,18 +24,18 @@ expectType(() => () => 'foo'); expectType((key: string) => key.toUpperCase()); expectType((key: string, value: string) => (key + value).toUpperCase()); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'})); -expectType(await writeJsonFile('unicorn.json', '🦄')); -expectType(await writeJsonFile('date.json', new Date())); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length})); -expectType(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'})); +expectType>(writeJsonFile('unicorn.json', '🦄')); +expectType>(writeJsonFile('date.json', new Date())); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length})); +expectType>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); expectType(sync('unicorn.json', {unicorn: '🦄'})); expectType(sync('unicorn.json', '🦄')); diff --git a/package.json b/package.json index 04c2bbe..8193020 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -35,16 +35,16 @@ ], "dependencies": { "detect-indent": "^5.0.0", - "graceful-fs": "^4.1.2", - "make-dir": "^1.0.0", - "pify": "^4.0.0", + "graceful-fs": "^4.1.15", + "make-dir": "^2.1.0", + "pify": "^4.0.1", "sort-keys": "^2.0.0", - "write-file-atomic": "^2.0.0" + "write-file-atomic": "^2.4.2" }, "devDependencies": { - "ava": "*", + "ava": "^1.4.1", "tempfile": "^2.0.0", - "tsd-check": "^0.2.1", - "xo": "*" + "tsd": "^0.7.2", + "xo": "^0.24.0" } }