Skip to content

Refactor TypeScript definition to CommonJS compatible export #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 77 additions & 58 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<number | string>;

/**
Mode used when writing the file.

@default 0o666
*/
readonly mode?: number;
}
}

declare const writeJsonFile: {
/**
* Passed into `JSON.stringify`.
*/
readonly replacer?: Replacer | Array<number | string>;
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<void>;

/**
* 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<void>;
export = writeJsonFile;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
29 changes: 15 additions & 14 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<JSONStringifyable>('🦄');
expectType<JSONStringifyable>(1);
Expand All @@ -23,18 +24,18 @@ expectType<Replacer>(() => () => 'foo');
expectType<Replacer>((key: string) => key.toUpperCase());
expectType<Replacer>((key: string, value: string) => (key + value).toUpperCase());

expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}));
expectType<void>(await writeJsonFile('unicorn.json', '🦄'));
expectType<void>(await writeJsonFile('date.json', new Date()));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', '🦄'));
expectType<Promise<void>>(writeJsonFile('date.json', new Date()));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: () => 'unicorn'}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: () => -1}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: (a: string, b: string) => a.length - b.length}));
expectType<Promise<void>>(writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true}));

expectType<void>(sync('unicorn.json', {unicorn: '🦄'}));
expectType<void>(sync('unicorn.json', '🦄'));
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -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"
}
}