UNPKG

2.03 kBTypeScriptView Raw
1/** Finds all instances of a specified executable in the PATH environment variable */
2
3type AppendNullIfNothrow<TOptions, TRet> = TOptions extends { nothrow: infer TVal }
4 // nothrow is specified
5 ? TVal extends false
6 // TVal is false
7 ? TRet
8 // TVal is boolean or true
9 : TRet | null
10 // nothrow not specified
11 : TRet;
12
13type TransformToArrayIfAll<TOptions, TRet> = TOptions extends { all: infer TVal }
14 // all is specified
15 ? TVal extends true
16 // TVal is true
17 ? readonly TRet[]
18 : TVal extends false
19 // TVal is false
20 ? TRet
21 // TVal is boolean
22 : readonly TRet[] | TRet
23 // all not specified
24 : TRet;
25
26type ReturnType<TOptions> = AppendNullIfNothrow<TOptions, TransformToArrayIfAll<TOptions, string>>;
27
28type Exact<T, U extends T> = {
29 [Key in keyof U]: Key extends keyof T ? U[Key]
30 : never;
31};
32
33declare function which<TOptions extends which.Options>(
34 cmd: string,
35 options?: Exact<which.Options, TOptions>,
36): Promise<ReturnType<Exact<which.Options, TOptions>>>;
37
38declare namespace which {
39 /** Finds all instances of a specified executable in the PATH environment variable */
40 function sync<TOptions extends Options>(
41 cmd: string,
42 options?: Exact<Options, TOptions>,
43 ): ReturnType<Exact<Options, TOptions>>;
44
45 /** Options for which() API */
46 interface Options {
47 /** If true, return all matches, instead of just the first one. Note that this means the function returns an array of strings instead of a single string. */
48 all?: boolean | undefined;
49 /** Use instead of the PATH environment variable. */
50 path?: string | undefined;
51 /** Use instead of the PATHEXT environment variable. */
52 pathExt?: string | undefined;
53 /** Use instead of the platform's native path separator. */
54 delimiter?: string | undefined;
55 /** If true, returns null when not found */
56 nothrow?: boolean | undefined;
57 }
58}
59
60export = which;