From d24b63a665ab61473cf4c978ec836e34329767c5 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 15 Apr 2019 03:44:57 +0000 Subject: [PATCH] Add TypeScript definition (#4) --- index.d.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ index.test-d.ts | 7 +++++++ package.json | 8 +++++--- 3 files changed, 60 insertions(+), 3 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..fb3bdee --- /dev/null +++ b/index.d.ts @@ -0,0 +1,48 @@ +declare namespace pkgUp { + interface Options { + /** + Directory to start from. + + @default process.cwd() + */ + readonly cwd?: string; + } +} + +declare const pkgUp: { + /** + Find the closest `package.json` file. + + @returns The filepath, or `null` if it couldn't be found. + + @example + ``` + // / + // └── Users + // └── sindresorhus + // └── foo + // ├── package.json + // └── bar + // ├── baz + // └── example.js + + // example.js + import pkgUp = require('pkg-up'); + + (async () => { + console.log(await pkgUp()); + //=> '/Users/sindresorhus/foo/package.json' + })(); + ``` + */ + (options?: pkgUp.Options): Promise; + + /** + Synchronously find the closest `package.json` file. + + @returns The filepath, or `null` if it couldn't be found. + */ + sync(options?: pkgUp.Options): string | null; +}; + +export = pkgUp; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..53ea1ba --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import pkgUp = require('.'); + +expectType>(pkgUp()); +expectType>(pkgUp({cwd: '.'})); +expectType(pkgUp.sync()); +expectType(pkgUp.sync({cwd: '.'})); diff --git a/package.json b/package.json index ac92557..94926ae 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": [ "pkg", @@ -44,7 +45,8 @@ "find-up": "^3.0.0" }, "devDependencies": { - "ava": "^1.3.1", + "ava": "^1.4.1", + "tsd": "^0.7.2", "xo": "^0.24.0" } }