From 8a3fcef45add7855a7a5f7f363eafb7683e621cb Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Wed, 24 Apr 2019 03:36:58 +0000 Subject: [PATCH] Add TypeScript definition (#12) --- index.d.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ index.test-d.ts | 20 +++++++++++++++ package.json | 14 +++++++---- readme.md | 4 +-- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..0c77f03 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,67 @@ +import * as typeFest from 'type-fest'; +import normalize = require('normalize-package-data'); + +declare namespace readPkg { + interface Options { + /** + [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data. + + @default true + */ + normalize?: boolean; + + /** + Current working directory. + + @default process.cwd() + */ + cwd?: string; + } + + interface NormalizeOptions extends Options { + normalize: true; + } + + type NormalizedPackageJson = PackageJson & normalize.Package; + type PackageJson = typeFest.PackageJson; +} + +declare const readPkg: { + /** + @returns The parsed JSON. + + @example + ``` + import readPkg = require('read-pkg'); + + (async () => { + console.log(await readPkg()); + //=> {name: 'read-pkg', …} + + console.log(await readPkg({cwd: 'some-other-directory'}); + //=> {name: 'unicorn', …} + })(); + ``` + */ + (options?: readPkg.NormalizeOptions): Promise; + (options: readPkg.Options): Promise; + + /** + @returns The parsed JSON. + + @example + ``` + import readPkg = require('read-pkg'); + + console.log(readPkg.sync()); + //=> {name: 'read-pkg', …} + + console.log(readPkg.sync({cwd: 'some-other-directory'}); + //=> {name: 'unicorn', …} + ``` + */ + sync(options?: readPkg.NormalizeOptions): readPkg.NormalizedPackageJson; + sync(options: readPkg.Options): readPkg.PackageJson; +}; + +export = readPkg; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..5c708a2 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,20 @@ +import {expectType, expectError} from 'tsd'; +import readPkg = require('.'); + +const options: readPkg.Options = {}; +expectError({}); +expectType({}); + +expectType>(readPkg()); +expectType>(readPkg({normalize: true})); +expectType>(readPkg({normalize: false})); +expectError>( + readPkg({normalize: false}) +); +expectType>(readPkg({cwd: './'})); + +expectType(readPkg.sync()); +expectType(readPkg.sync({normalize: true})); +expectType(readPkg.sync({normalize: false})); +expectError(readPkg.sync({normalize: false})); +expectType(readPkg.sync({cwd: './'})); diff --git a/package.json b/package.json index 11401da..a6378b6 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "json", @@ -31,11 +32,14 @@ "normalize" ], "dependencies": { - "normalize-package-data": "^2.3.2", - "parse-json": "^4.0.0" + "@types/normalize-package-data": "^2.4.0", + "normalize-package-data": "^2.5.0", + "parse-json": "^4.0.0", + "type-fest": "^0.4.1" }, "devDependencies": { - "ava": "^1.3.1", + "ava": "^1.4.1", + "tsd": "^0.7.2", "xo": "^0.24.0" }, "xo": { diff --git a/readme.md b/readme.md index 1eea218..0b45541 100644 --- a/readme.md +++ b/readme.md @@ -36,7 +36,7 @@ const readPkg = require('read-pkg'); ### readPkg([options]) -Returns a `Promise` for the parsed JSON. +Returns a `Promise` with the parsed JSON. ### readPkg.sync([options]) @@ -44,7 +44,7 @@ Returns the parsed JSON. #### options -Type: `Object` +Type: `object` ##### cwd