|
| 1 | +/** |
| 2 | + * The ISC License |
| 3 | + * |
| 4 | + * Copyright (c) Isaac Z. Schlueter and Contributors |
| 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. |
| 17 | + * |
| 18 | + * -- |
| 19 | + * |
| 20 | + * This is based on https://github.com/npm/node-which/blob/v7.0.0/lib/index.js with |
| 21 | + * the following modifications for QTap: |
| 22 | + * - Fork node-which to inline isexe 3.1.1, because isexe 3.1.3 and later are too large. |
| 23 | + * The increase would fail our structure test for the "Lean" policy (ARCHITECTURE.md). |
| 24 | + * The essential logic for node-which has been constant for years, easy to understand, |
| 25 | + * easy to test, and adds more cost than benefit when maintained as a dependency. |
| 26 | + * - Simplify: |
| 27 | + * - Remove async version, use sync always. |
| 28 | + * - Remove "nothrow" option, enable always. |
| 29 | + * - Remove unused "all", "path", "pathExt", "delimiter" options. |
| 30 | + * - Remove isexe's unused "uid", "gid" options. Use plain `fs.accessSync(,X_OK)` and `stat.isFile()`. |
| 31 | + * - Restrict feature set to just searching for commands in the pathenv: |
| 32 | + * - Remove unneeded support for absolute paths (i.e. command with a slash like "/bin/foo"). |
| 33 | + * This responsibility is handled by /src/util.js#spawn. |
| 34 | + * - Remove unneeded support for relative paths (i.e. "./foo"). |
| 35 | + * - Remove isexe "ignoreErrors" option, enable always. |
| 36 | + * - Remove isexe feature for locating Windows-executable PATHEXT suffixes. |
| 37 | + * Programs (e.g. Firefox) don't have suffixes that are user-dependent or otherwise not |
| 38 | + * knowable in advance. To locate "FOO.COM", just specify that directly to keep code simple, |
| 39 | + * greppable, and unsurprising. We don't need to emulate or predict everything Windows shell |
| 40 | + * can find (i.e. locate "FOO.COM" given "FOO"). We only need to search the PATH choices, |
| 41 | + * and use the absolute path after that. Besides, browsers on Windows aren't installed as |
| 42 | + * shell commands, they're in PROGRAMFILES (see WINDOWS_DIRS in /src/browsers.js). To run a |
| 43 | + * custom shell program in a QTap browser plugin, specify the absolute path to your bundled |
| 44 | + * program or (if installed globally) include your program's suffix. |
| 45 | + */ |
| 46 | +'use strict'; |
| 47 | + |
| 48 | +import path from 'node:path'; |
| 49 | +import fs from 'node:fs'; |
| 50 | + |
| 51 | +// On Windows, this treats all links or files as executable. |
| 52 | +function isexeSync (fullpath) { |
| 53 | + try { |
| 54 | + fs.accessSync(fullpath, fs.constants.X_OK); |
| 55 | + // Skip executable directories. https://github.com/npm/node-which/pull/46 |
| 56 | + const stat = fs.statSync(fullpath); |
| 57 | + return stat.isSymbolicLink() || stat.isFile(); |
| 58 | + } catch (er) { |
| 59 | + return false; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function whichSync (cmd) { |
| 64 | + const pathEnv = (process.env.PATH || '').split(path.delimiter); |
| 65 | + for (const pathEnvPart of pathEnv) { |
| 66 | + const cmdPath = path.join(pathEnvPart, cmd); |
| 67 | + if (isexeSync(cmdPath)) { |
| 68 | + return cmdPath; |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | +} |
| 73 | + |
| 74 | +export default whichSync; |
0 commit comments