Skip to content
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

Add TypeScript definition and require Node.js 6 #17

Merged
merged 15 commits into from
Aug 27, 2018
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to define default arguments in the types themselves instead of in the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I don't think it's possible as an interface.

[ts] A parameter initializer is only allowed in a function or constructor implementation.

You can do it if the types are created from a .ts file

/**
* 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';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be import writeJsonFile from 'write-json-file';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 ... to access .sync() you need to add import * as

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if you use Babel this example is correct, I'll change it to:

import writeJsonFile from 'write-json-file';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most Typescript users kinda got used to adding import * as, no need to include it.

Most docs are mixed between Babel & Typescript, we can keep as the Babel way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus FYI: I've changed it back to import * as syntax for the .sync() example.

Reason: Since this is a Typescript definition, it makes more sense to reference the example in the "Typescript" way.

*
* 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));
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating the code here, assign the method to a variable and then assign that variable to both module.exports and module.exports.default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 makes sense


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"
]
}
}