Use `which-command` instead of `which` (#1253)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent c31c94c · Browse files · 3 files·+10 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+10 added−36 removed
- tests:+2 added−2 removed
- other:+8 added−34 removed
Provenance
- Commit
9a7a162- Tree
13f4870- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
c31c94c
3 files changed+10 added−36 removed
modified:lib/arguments/command-file.js+7 added-33 removed
123455678961 unmodified lines71727374757677787576778081828384858687888990919293949596979899100101102103104105106107787980818283848575 unmodified linesimport {openSync, readSync, closeSync} from 'node:fs';import {Buffer} from 'node:buffer';import path from 'node:path';import process from 'node:process';import which from 'which';import {whichCommandSync} from 'which-command';import pathKey from 'path-key';
/*On Windows, `node:child_process` cannot natively run many kinds of files (`.cmd`, `.bat`, shebang scripts, ...): without a shell it resolves neither `PATHEXT` nor shebangs, and it does not escape arguments. We resolve the command to its full file path and escape its arguments ourselves, so those work without an explicit shell, just like on Unix, where the OS handles all of this for us.61 unmodified lines parsed.file = interpreter; return resolvePath(parsed);};
// Search `PATH` for the command, first honoring `PATHEXT`, then ignoring it as a fallbackconst resolvePath = parsed => resolveInCwd(parsed, false) || resolveInCwd(parsed, true);
const resolveInCwd = (parsed, ignorePathExtension) => {// Search `PATH` for the command, resolving its Windows executable extension via `PATHEXT`const resolvePath = parsed => { const environment = parsed.options.env || process.env;
/* `which` runs `stat` relative to the current directory but has no `cwd` option, so we temporarily switch into the target directory and restore it afterwards.
`process.chdir()` is disabled in worker threads and might be absent in non-Node runtimes. */ const canChangeDirectory = process.chdir !== undefined && !process.chdir.disabled; const originalDirectory = process.cwd(); if (canChangeDirectory) { try { process.chdir(parsed.options.cwd); } catch {} }
try { const resolved = which.sync(parsed.file, { path: environment[pathKey({env: environment})], pathExt: ignorePathExtension ? path.delimiter : undefined, }); // `cwd` is already an absolute path, so this returns an absolute path too return path.resolve(parsed.options.cwd, resolved); } catch { return undefined; } finally { if (canChangeDirectory) { process.chdir(originalDirectory); } } return whichCommandSync(parsed.file, { cwd: parsed.options.cwd, path: environment[pathKey({env: environment})], });};
const SHEBANG_BYTE_LENGTH = 150;
75 unmodified linesmodified:test/arguments/shell.js+2 added-2 removed
1 import process from 'node:process';2 import {pathToFileURL} from 'node:url';3 import test from 'ava';4-import which from 'which';4+import {whichCommand} from 'which-command';5 import {execa} from '../../index.js';6 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7 import {identity} from '../helpers/stdio.js';8 8 unmodified lines17 });18 19 const testShellPath = async (t, mapPath) => {20 const shellPath = isWindows ? 'cmd.exe' : 'bash';21- const shell = mapPath(await which(shellPath));21+ const shell = mapPath(await whichCommand(shellPath));22 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});23 t.is(stdout, 'foo');24 };25 2 unmodified linesmodified:package.json+1 added-1 removed
60 unmodified lines61 "path-key": "^4.0.0",62 "pretty-ms": "^9.3.0",63 "signal-exit": "^4.1.0",64 "strip-final-newline": "^4.0.0",65- "which": "^7.0.0",65+ "which-command": "^0.1.0",66 "yoctocolors": "^2.1.2"67 },68 "devDependencies": {69 "@types/node": "^26.1.0",61 unmodified lines