@vltpkg/which 0.0.0-0.1730239248325
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -0
- package/README.md +43 -0
- package/dist/esm/index.d.ts +42 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +99 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
The ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Isaac Z. Schlueter and Contributors
|
|
4
|
+
Copyright (c) vlt technology, Inc.
|
|
5
|
+
|
|
6
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
7
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
8
|
+
copyright notice and this permission notice appear in all copies.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
11
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
12
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
13
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
14
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
15
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
16
|
+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# `@vltpkg/which`
|
|
2
|
+
|
|
3
|
+
Like the unix `which` utility.
|
|
4
|
+
|
|
5
|
+
Finds the first instance of a specified executable in the PATH
|
|
6
|
+
environment variable. Does not cache the results, so `hash -r` is not
|
|
7
|
+
needed when the PATH changes.
|
|
8
|
+
|
|
9
|
+
Port of the [`which`](http://npm.im/which) to a TypeScript hybrid
|
|
10
|
+
module.
|
|
11
|
+
|
|
12
|
+
## USAGE
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
import { which, whichSync } from '@vltpkg/which'
|
|
16
|
+
|
|
17
|
+
// async usage
|
|
18
|
+
// rejects if not found
|
|
19
|
+
const resolved = await which('node')
|
|
20
|
+
|
|
21
|
+
// if nothrow option is used, returns null if not found
|
|
22
|
+
const resolvedOrNull = await which('node', { nothrow: true })
|
|
23
|
+
|
|
24
|
+
// sync usage
|
|
25
|
+
// throws if not found
|
|
26
|
+
const resolved = whichSync('node')
|
|
27
|
+
|
|
28
|
+
// if nothrow option is used, returns null if not found
|
|
29
|
+
const resolvedOrNull = whichSync('node', { nothrow: true })
|
|
30
|
+
|
|
31
|
+
// Pass options to override the PATH and PATHEXT environment vars.
|
|
32
|
+
await which('node', { path: someOtherPath, pathExt: somePathExt })
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## OPTIONS
|
|
36
|
+
|
|
37
|
+
You may pass an options object as the second argument.
|
|
38
|
+
|
|
39
|
+
- `path`: Use instead of the `PATH` environment variable.
|
|
40
|
+
- `pathExt`: Use instead of the `PATHEXT` environment variable.
|
|
41
|
+
- `all`: Return all matches, instead of just the first one. Note that
|
|
42
|
+
this means the function returns an array of strings instead of a
|
|
43
|
+
single string.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type WhichOptions = {
|
|
2
|
+
all?: boolean;
|
|
3
|
+
path?: string;
|
|
4
|
+
pathExt?: string;
|
|
5
|
+
nothrow?: boolean;
|
|
6
|
+
delimiter?: string;
|
|
7
|
+
};
|
|
8
|
+
export type WhichOptionsFindAll = WhichOptions & {
|
|
9
|
+
all: true;
|
|
10
|
+
};
|
|
11
|
+
export type WhichOptionsFindOne = WhichOptions & {
|
|
12
|
+
all?: false;
|
|
13
|
+
};
|
|
14
|
+
export type WhichOptionsNoThrow = WhichOptions & {
|
|
15
|
+
nothrow: true;
|
|
16
|
+
};
|
|
17
|
+
export type WhichOptionsThrow = WhichOptions & {
|
|
18
|
+
nothrow?: false;
|
|
19
|
+
};
|
|
20
|
+
export type WhichOptionsFindOneThrow = WhichOptionsFindOne & WhichOptionsThrow;
|
|
21
|
+
export type WhichOptionsFindOneNoThrow = WhichOptionsFindOne & WhichOptionsNoThrow;
|
|
22
|
+
export type WhichOptionsFindAllNoThrow = WhichOptionsFindAll & WhichOptionsNoThrow;
|
|
23
|
+
export type WhichOptionsFindAllThrow = WhichOptionsFindAll & WhichOptionsThrow;
|
|
24
|
+
export declare function which(cmd: string): Promise<string>;
|
|
25
|
+
export declare function which(cmd: string, opt: WhichOptionsFindAllNoThrow): Promise<string[] | null>;
|
|
26
|
+
export declare function which(cmd: string, opt: WhichOptionsFindOneNoThrow): Promise<string | null>;
|
|
27
|
+
export declare function which(cmd: string, opt: WhichOptionsFindAllThrow): Promise<string[]>;
|
|
28
|
+
export declare function which(cmd: string, opt: WhichOptionsFindOneThrow): Promise<string>;
|
|
29
|
+
export declare function which(cmd: string, opt: WhichOptionsFindOne): Promise<string | null>;
|
|
30
|
+
export declare function which(cmd: string, opt: WhichOptionsNoThrow): Promise<string[] | string | null>;
|
|
31
|
+
export declare function which(cmd: string, opt: WhichOptionsFindAll): Promise<string[] | null>;
|
|
32
|
+
export declare function which(cmd: string, opt: WhichOptions): Promise<string[] | string | null>;
|
|
33
|
+
export declare function whichSync(cmd: string): string;
|
|
34
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindAllNoThrow): string[] | null;
|
|
35
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindOneNoThrow): string | null;
|
|
36
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindAllThrow): string[];
|
|
37
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindOneThrow): string;
|
|
38
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindOne): string | null;
|
|
39
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsNoThrow): string[] | string | null;
|
|
40
|
+
export declare function whichSync(cmd: string, opt: WhichOptionsFindAll): string[] | null;
|
|
41
|
+
export declare function whichSync(cmd: string, opt?: WhichOptions): string[] | string | null;
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAgEA,MAAM,MAAM,YAAY,GAAG;IACzB,GAAG,CAAC,EAAE,OAAO,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG,YAAY,GAAG;IAAE,GAAG,EAAE,IAAI,CAAA;CAAE,CAAA;AAC9D,MAAM,MAAM,mBAAmB,GAAG,YAAY,GAAG;IAAE,GAAG,CAAC,EAAE,KAAK,CAAA;CAAE,CAAA;AAEhE,MAAM,MAAM,mBAAmB,GAAG,YAAY,GAAG;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,CAAA;AAClE,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG;IAAE,OAAO,CAAC,EAAE,KAAK,CAAA;CAAE,CAAA;AAElE,MAAM,MAAM,wBAAwB,GAAG,mBAAmB,GACxD,iBAAiB,CAAA;AACnB,MAAM,MAAM,0BAA0B,GAAG,mBAAmB,GAC1D,mBAAmB,CAAA;AAErB,MAAM,MAAM,0BAA0B,GAAG,mBAAmB,GAC1D,mBAAmB,CAAA;AACrB,MAAM,MAAM,wBAAwB,GAAG,mBAAmB,GACxD,iBAAiB,CAAA;AAEnB,wBAAsB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AACzD,w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,IAAI,CAAA;AAChB,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,wBAAwB,GAC5B,MAAM,EAAE,CAAA;AACX,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,wBAAwB,GAC5B,MAAM,CAAA;AACT,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,mBAAmB,GACvB,MAAM,GAAG,IAAI,CAAA;AAChB,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,mBAAmB,GACvB,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAAA;AAC3B,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,mBAAmB,GACvB,MAAM,EAAE,GAAG,IAAI,CAAA;AAClB,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,CAAC,EAAE,YAAY,GACjB,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAAA"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { isexe, sync as isexeSync } from 'isexe';
|
|
2
|
+
import { delimiter, join, sep } from 'path';
|
|
3
|
+
const isWindows = process.platform === 'win32';
|
|
4
|
+
// used to check for slashed in commands passed in. always checks for the posix
|
|
5
|
+
// separator on all platforms, and checks for the current separator when not on
|
|
6
|
+
// a posix platform. don't use the isWindows check for this since that is
|
|
7
|
+
// mocked in tests but we still need the code to actually work when called.
|
|
8
|
+
// that is also why it is ignored from coverage.
|
|
9
|
+
/* c8 ignore next */
|
|
10
|
+
const rSlash = sep === '/' ? /\// : /[\\/]/;
|
|
11
|
+
const rRel = new RegExp(`^\\.${rSlash.source}`);
|
|
12
|
+
// Simulate a NodeJS.ErrnoException
|
|
13
|
+
const getNotFoundError = (cmd) => Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' });
|
|
14
|
+
const getPathInfo = (cmd, { path: optPath = process.env.PATH, pathExt: optPathExt = process.env.PATHEXT, delimiter: optDelimiter = delimiter, }) => {
|
|
15
|
+
// If it has a slash, then we don't bother searching the pathenv.
|
|
16
|
+
// just check the file itself, and that's it.
|
|
17
|
+
const pathEnv = cmd.match(rSlash) ?
|
|
18
|
+
['']
|
|
19
|
+
: [
|
|
20
|
+
// windows always checks the cwd first
|
|
21
|
+
/* c8 ignore next - platform-specific */
|
|
22
|
+
...(isWindows ? [process.cwd()] : []),
|
|
23
|
+
...(optPath ?? /* c8 ignore next - very unusual */ '').split(optDelimiter),
|
|
24
|
+
];
|
|
25
|
+
if (isWindows) {
|
|
26
|
+
const pathExtExe = optPathExt ||
|
|
27
|
+
['.EXE', '.CMD', '.BAT', '.COM'].join(optDelimiter);
|
|
28
|
+
const pathExt = pathExtExe
|
|
29
|
+
.split(optDelimiter)
|
|
30
|
+
.flatMap(item => [item, item.toLowerCase()]);
|
|
31
|
+
if (cmd.includes('.') && pathExt[0] !== '') {
|
|
32
|
+
pathExt.unshift('');
|
|
33
|
+
}
|
|
34
|
+
return { pathEnv, pathExt, pathExtExe };
|
|
35
|
+
/* c8 ignore start - not reachable on windows */
|
|
36
|
+
}
|
|
37
|
+
return { pathEnv, pathExt: [''] };
|
|
38
|
+
};
|
|
39
|
+
/* c8 ignore stop */
|
|
40
|
+
const getPathPart = (raw, cmd) => {
|
|
41
|
+
const pathPart = /^".*"$/.test(raw) ? raw.slice(1, -1) : raw;
|
|
42
|
+
const prefix = !pathPart && rRel.test(cmd) ? cmd.slice(0, 2) : '';
|
|
43
|
+
return prefix + join(pathPart, cmd);
|
|
44
|
+
};
|
|
45
|
+
export async function which(cmd, opt = {}) {
|
|
46
|
+
const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
|
|
47
|
+
const found = [];
|
|
48
|
+
for (const envPart of pathEnv) {
|
|
49
|
+
const p = getPathPart(envPart, cmd);
|
|
50
|
+
for (const ext of pathExt) {
|
|
51
|
+
const withExt = p + ext;
|
|
52
|
+
const is = await isexe(withExt, {
|
|
53
|
+
pathExt: pathExtExe,
|
|
54
|
+
ignoreErrors: true,
|
|
55
|
+
});
|
|
56
|
+
if (is) {
|
|
57
|
+
if (!opt.all) {
|
|
58
|
+
return withExt;
|
|
59
|
+
}
|
|
60
|
+
found.push(withExt);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (opt.all && found.length) {
|
|
65
|
+
return found;
|
|
66
|
+
}
|
|
67
|
+
if (opt.nothrow) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
throw getNotFoundError(cmd);
|
|
71
|
+
}
|
|
72
|
+
export function whichSync(cmd, opt = {}) {
|
|
73
|
+
const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
|
|
74
|
+
const found = [];
|
|
75
|
+
for (const pathEnvPart of pathEnv) {
|
|
76
|
+
const p = getPathPart(pathEnvPart, cmd);
|
|
77
|
+
for (const ext of pathExt) {
|
|
78
|
+
const withExt = p + ext;
|
|
79
|
+
const is = isexeSync(withExt, {
|
|
80
|
+
pathExt: pathExtExe,
|
|
81
|
+
ignoreErrors: true,
|
|
82
|
+
});
|
|
83
|
+
if (is) {
|
|
84
|
+
if (!opt.all) {
|
|
85
|
+
return withExt;
|
|
86
|
+
}
|
|
87
|
+
found.push(withExt);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (opt.all && found.length) {
|
|
92
|
+
return found;
|
|
93
|
+
}
|
|
94
|
+
if (opt.nothrow) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
throw getNotFoundError(cmd);
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,OAAO,CAAA;AAChD,OAAO,EA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AC,GAAG,GAAG,CAAA;YACvB,MAAM,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE;gBAC5B,OAAO,EAAE,UAAU;gBACnB,YAAY,EAAE,IAAI;aACnB,CAAC,CAAA;YACF,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;oBACb,OAAO,OAAO,CAAA;gBAChB,CAAC;gBACD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IAED,IAAI,GAAG,CAAC,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,gBAAgB,CAAC,GAAG,CAAC,CAAA;AAC7B,CAAC","sourcesContent":["import { isexe, sync as isexeSync } from 'isexe'\nimport { delimiter, join, sep } from 'path'\n\nconst isWindows = process.platform === 'win32'\n\n// used to check for slashed in commands passed in. always checks for the posix\n// separator on all platforms, and checks for the current separator when not on\n// a posix platform. don't use the isWindows check for this since that is\n// mocked in tests but we still need the code to actually work when called.\n// that is also why it is ignored from coverage.\n/* c8 ignore next */\nconst rSlash = sep === '/' ? /\\// : /[\\\\/]/\nconst rRel = new RegExp(`^\\\\.${rSlash.source}`)\n\n// Simulate a NodeJS.ErrnoException\nconst getNotFoundError = (cmd: string) =>\n Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' })\n\nconst getPathInfo = (\n cmd: string,\n {\n path: optPath = process.env.PATH,\n pathExt: optPathExt = process.env.PATHEXT,\n delimiter: optDelimiter = delimiter,\n }: WhichOptions,\n) => {\n // If it has a slash, then we don't bother searching the pathenv.\n // just check the file itself, and that's it.\n const pathEnv =\n cmd.match(rSlash) ?\n ['']\n : [\n // windows always checks the cwd first\n /* c8 ignore next - platform-specific */\n ...(isWindows ? [process.cwd()] : []),\n ...(optPath ?? /* c8 ignore next - very unusual */ '').split(\n optDelimiter,\n ),\n ]\n\n if (isWindows) {\n const pathExtExe =\n optPathExt ||\n ['.EXE', '.CMD', '.BAT', '.COM'].join(optDelimiter)\n const pathExt = pathExtExe\n .split(optDelimiter)\n .flatMap(item => [item, item.toLowerCase()])\n if (cmd.includes('.') && pathExt[0] !== '') {\n pathExt.unshift('')\n }\n return { pathEnv, pathExt, pathExtExe }\n /* c8 ignore start - not reachable on windows */\n }\n\n return { pathEnv, pathExt: [''] }\n}\n/* c8 ignore stop */\n\nconst getPathPart = (raw: string, cmd: string) => {\n const pathPart = /^\".*\"$/.test(raw) ? raw.slice(1, -1) : raw\n const prefix = !pathPart && rRel.test(cmd) ? cmd.slice(0, 2) : ''\n return prefix + join(pathPart, cmd)\n}\n\nexport type WhichOptions = {\n all?: boolean\n path?: string\n pathExt?: string\n nothrow?: boolean\n delimiter?: string\n}\n\nexport type WhichOptionsFindAll = WhichOptions & { all: true }\nexport type WhichOptionsFindOne = WhichOptions & { all?: false }\n\nexport type WhichOptionsNoThrow = WhichOptions & { nothrow: true }\nexport type WhichOptionsThrow = WhichOptions & { nothrow?: false }\n\nexport type WhichOptionsFindOneThrow = WhichOptionsFindOne &\n WhichOptionsThrow\nexport type WhichOptionsFindOneNoThrow = WhichOptionsFindOne &\n WhichOptionsNoThrow\n\nexport type WhichOptionsFindAllNoThrow = WhichOptionsFindAll &\n WhichOptionsNoThrow\nexport type WhichOptionsFindAllThrow = WhichOptionsFindAll &\n WhichOptionsThrow\n\nexport async function which(cmd: string): Promise<string>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindAllNoThrow,\n): Promise<string[] | null>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindOneNoThrow,\n): Promise<string | null>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindAllThrow,\n): Promise<string[]>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindOneThrow,\n): Promise<string>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindOne,\n): Promise<string | null>\nexport async function which(\n cmd: string,\n opt: WhichOptionsNoThrow,\n): Promise<string[] | string | null>\nexport async function which(\n cmd: string,\n opt: WhichOptionsFindAll,\n): Promise<string[] | null>\nexport async function which(\n cmd: string,\n opt: WhichOptions,\n): Promise<string[] | string | null>\nexport async function which(cmd: string, opt: WhichOptions = {}) {\n const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt)\n const found = []\n\n for (const envPart of pathEnv) {\n const p = getPathPart(envPart, cmd)\n\n for (const ext of pathExt) {\n const withExt = p + ext\n const is = await isexe(withExt, {\n pathExt: pathExtExe,\n ignoreErrors: true,\n })\n if (is) {\n if (!opt.all) {\n return withExt\n }\n found.push(withExt)\n }\n }\n }\n\n if (opt.all && found.length) {\n return found\n }\n\n if (opt.nothrow) {\n return null\n }\n\n throw getNotFoundError(cmd)\n}\n\nexport function whichSync(cmd: string): string\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindAllNoThrow,\n): string[] | null\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindOneNoThrow,\n): string | null\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindAllThrow,\n): string[]\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindOneThrow,\n): string\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindOne,\n): string | null\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsNoThrow,\n): string[] | string | null\nexport function whichSync(\n cmd: string,\n opt: WhichOptionsFindAll,\n): string[] | null\nexport function whichSync(\n cmd: string,\n opt?: WhichOptions,\n): string[] | string | null\nexport function whichSync(cmd: string, opt: WhichOptions = {}) {\n const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt)\n const found = []\n\n for (const pathEnvPart of pathEnv) {\n const p = getPathPart(pathEnvPart, cmd)\n\n for (const ext of pathExt) {\n const withExt = p + ext\n const is = isexeSync(withExt, {\n pathExt: pathExtExe,\n ignoreErrors: true,\n })\n if (is) {\n if (!opt.all) {\n return withExt\n }\n found.push(withExt)\n }\n }\n }\n\n if (opt.all && found.length) {\n return found\n }\n\n if (opt.nothrow) {\n return null\n }\n\n throw getNotFoundError(cmd)\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vltpkg/which",
|
|
3
|
+
"description": "Find the first executable in the path (TS port of 'which')",
|
|
4
|
+
"version": "0.0.0-0.1730239248325",
|
|
5
|
+
"tshy": {
|
|
6
|
+
"selfLink": false,
|
|
7
|
+
"dialects": [
|
|
8
|
+
"esm"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": "./src/index.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"isexe": "^3.1.1"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@eslint/js": "^9.8.0",
|
|
20
|
+
"@types/eslint__js": "^8.42.3",
|
|
21
|
+
"@types/node": "^22.4.1",
|
|
22
|
+
"eslint": "^9.8.0",
|
|
23
|
+
"prettier": "^3.3.2",
|
|
24
|
+
"tap": "^21.0.1",
|
|
25
|
+
"tshy": "^3.0.2",
|
|
26
|
+
"typescript": "^5.5.4",
|
|
27
|
+
"typescript-eslint": "^8.0.1"
|
|
28
|
+
},
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": "20 || >=22"
|
|
32
|
+
},
|
|
33
|
+
"tap": {
|
|
34
|
+
"extends": "../../tap-config.yaml"
|
|
35
|
+
},
|
|
36
|
+
"prettier": "../../.prettierrc.js",
|
|
37
|
+
"module": "./dist/esm/index.js",
|
|
38
|
+
"type": "module",
|
|
39
|
+
"exports": {
|
|
40
|
+
"./package.json": "./package.json",
|
|
41
|
+
".": {
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./dist/esm/index.d.ts",
|
|
44
|
+
"default": "./dist/esm/index.js"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
|
|
53
|
+
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
|
|
54
|
+
"lint": "eslint . --fix",
|
|
55
|
+
"lint:check": "eslint .",
|
|
56
|
+
"presnap": "tshy",
|
|
57
|
+
"snap": "tap",
|
|
58
|
+
"pretest": "tshy",
|
|
59
|
+
"test": "tap"
|
|
60
|
+
}
|
|
61
|
+
}
|