From f379e816be4445c1256d7bef08cfca5033029985 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 14 Jun 2019 06:03:35 +0200 Subject: [PATCH] Add types for the "npm-which" module (#36044) --- types/npm-which/index.d.ts | 176 +++++++++++++++++++++++++++++ types/npm-which/npm-which-tests.ts | 25 ++++ types/npm-which/tsconfig.json | 23 ++++ types/npm-which/tslint.json | 1 + 4 files changed, 225 insertions(+) create mode 100644 types/npm-which/index.d.ts create mode 100644 types/npm-which/npm-which-tests.ts create mode 100644 types/npm-which/tsconfig.json create mode 100644 types/npm-which/tslint.json diff --git a/types/npm-which/index.d.ts b/types/npm-which/index.d.ts new file mode 100644 index 0000000000..87a3e1b485 --- /dev/null +++ b/types/npm-which/index.d.ts @@ -0,0 +1,176 @@ +// Type definitions for npm-which 3.0 +// Project: https://github.com/timoxley/npm-which +// Definitions by: Manuel Thalmann +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import "node"; + +/** + * Provides options for the `npmwhich`-module. + */ +interface NpmWhichOptions { + /** + * The environment to use for resolving the binary. + */ + env?: NodeJS.ProcessEnv; + + /** + * The directory to find the binary for. + */ + cwd?: string; +} + +/** + * Provides options for the `npmwhich`-module. + */ +interface StaticWhichOptions { + /** + * The environment to use for resolving the binary. + */ + env?: NodeJS.ProcessEnv; + + /** + * The directory to find the binary for. + */ + cwd: string; +} + +/** + * Represents a callback for handling the result of `NpmWhich`. + */ +interface NpmWhichCallback { + /** + * Handles the result of `NpmWhich`. + * + * @param error + * The error-message. + * + * @param result + * The result. + */ + (error: string, result: string): void; +} + +/** + * Represents a basic interface for `npm-which`. + */ +interface WhichBase { + /** + * Creates a searcher for the specified command. + * + * @param cmd + * The command to look for. + * + * @param options + * The default options. + * + * @return + * A searcher for the specified command. + */ + (cmd: string, options?: TOptions): InnerWhich; + + /** + * Searches for the specified command. + * + * @param cmd + * The command to look for. + * + * @param callback + * A callback for handling the result. + */ + (cmd: string, callback: NpmWhichCallback): void; + + /** + * Searches for the specified command. + * + * @param cmd + * The command to look for. + * + * @param options + * The options for searching the command. + * + * @param callback + * A callback for handling the result. + */ + (cmd: string, options: TOptions, callback: NpmWhichCallback): void; +} + +/** + * Represents the static instance of `npm-which`. + */ +interface StaticWhich extends WhichBase { + /** + * Initializes an `NpmWhich`-instance for the specified working-directory. + * + * @param cwd + * The working-directory to browse. + */ + (cwd?: string): NpmWhich; + + /** + * Searches for the specified command. + * + * @param cmd + * The command to look for. + * + * @param options + * The options for searching the command. + */ + sync(cmd: string, options: StaticWhichOptions): string; +} + +/** + * Provides the functionality to search for a command. + */ +interface NpmWhich extends WhichBase { + /** + * Searches for the specified command. + * + * @param cmd + * The command to look for. + * + * @param options + * The options for searching the command. + */ + sync(cmd: string, options?: StaticWhichOptions): string; +} + +interface InnerWhich { + /** + * Creates a searcher for the specified command. + * + * @param options + * The options for searching the command. + */ + (options?: NpmWhichOptions): InnerWhich; + + /** + * Searches for the command. + * + * @param callback + * A callback for handling the result. + */ + (callback: NpmWhichCallback): void; + + /** + * Searches for the command. + * + * @param options + * The options for searching the command. + * + * @param callback + * A callback for handling the result. + */ + (options: NpmWhichOptions, callback: NpmWhichCallback): void; + + /** + * Searches for the command. + * + * @param options + * The options for searching the command. + */ + sync(options?: NpmWhichOptions): string; +} + +declare let npmWhich: StaticWhich; +export = npmWhich; diff --git a/types/npm-which/npm-which-tests.ts b/types/npm-which/npm-which-tests.ts new file mode 100644 index 0000000000..b4032bce44 --- /dev/null +++ b/types/npm-which/npm-which-tests.ts @@ -0,0 +1,25 @@ +import npmWhich = require("npm-which"); + +const which = npmWhich(""); +// $ExpectError +npmWhich("", { }); +npmWhich("", { cwd: "" }); +npmWhich( + "", + { cwd: "" }, + (error, result) => { + // $ExpectType string + error; + // $ExpectType string + result; + }); +// $ExpectError +npmWhich.sync("", { }); +npmWhich.sync("", { cwd: "" }); +which("", () => null); +which("", { cwd: "" }, () => null); +which.sync(""); +const innerWhich = which(""); +innerWhich({}); +innerWhich({}, () => null); +innerWhich.sync({}); diff --git a/types/npm-which/tsconfig.json b/types/npm-which/tsconfig.json new file mode 100644 index 0000000000..01cd92d30e --- /dev/null +++ b/types/npm-which/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "npm-which-tests.ts" + ] +} diff --git a/types/npm-which/tslint.json b/types/npm-which/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/npm-which/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }