Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@vltpkg/which

Package Overview
Dependencies
Maintainers
5
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vltpkg/which - npm Package Compare versions

Comparing version
0.0.0-6
to
0.0.0-7
+1
-1
dist/esm/index.js
import { isexe, sync as isexeSync } from 'isexe';
import { delimiter, join, sep } from 'path';
import { delimiter, join, sep } from 'node:path';
const isWindows = process.platform === 'win32';

@@ -4,0 +4,0 @@ // used to check for slashed in commands passed in. always checks for the posix

@@ -1,1 +0,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,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AAE3C,MAAM,SAAS,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAA;AAE9C,+EAA+E;AAC/E,+EAA+E;AAC/E,yEAAyE;AACzE,2EAA2E;AAC3E,gDAAgD;AAChD,oBAAoB;AACpB,MAAM,MAAM,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAA;AAC3C,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC,CAAA;AAE/C,mCAAmC;AACnC,MAAM,gBAAgB,GAAG,CAAC,GAAW,EAAE,EAAE,CACvC,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,cAAc,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAA;AAEnE,MAAM,WAAW,GAAG,CAClB,GAAW,EACX,EACE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,EAChC,OAAO,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AAA;IACd,CAAC;IAED,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,gBAAgB,CAAC,GAAG,CAAC,CAAA;AAC7B,CAAC;AAmCD,MAAM,UAAU,SAAS,CAAC,GAAW,EAAE,MAAoB,EAAE;IAC3D,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAC9D,MAAM,KAAK,GAAG,EAAE,CAAA;IAEhB,KAAK,MAAM,WAAW,IAAI,OAAO,EAAE,CAAC;QAClC,MAAM,CAAC,GAAG,WAAW,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;QAEvC,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;YAC1B,MAAM,OAAO,GAAG,CAAC,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"]}
{"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,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AAEhD,MAAM,SAAS,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAA;AAE9C,+EAA+E;AAC/E,+EAA+E;AAC/E,yEAAyE;AACzE,2EAA2E;AAC3E,gDAAgD;AAChD,oBAAoB;AACpB,MAAM,MAAM,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAA;AAC3C,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC,CAAA;AAE/C,mCAAmC;AAC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O,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 'node: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"]}
{
"name": "@vltpkg/which",
"description": "Find the first executable in the path (TS port of 'which')",
"version": "0.0.0-6",
"version": "0.0.0-7",
"repository": {

@@ -6,0 +6,0 @@ "type": "git",