Skip to content

Commit

Permalink
Add TypeScript definition and require Node.js 6 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere authored and sindresorhus committed Aug 27, 2018
1 parent e4c9b99 commit 8a602ea
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
62 changes: 62 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<number | string>;
/**
* 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<void>;
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -44,5 +45,10 @@
"ava": "*",
"tempfile": "^2.0.0",
"xo": "*"
},
"xo": {
"ignores": [
"index.d.ts"
]
}
}

0 comments on commit 8a602ea

Please sign in to comment.