|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { homedir } from "node:os"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { Uri, workspace } from "vscode"; |
| 5 | +import { validateSafeBinaryPath } from "./PathValidator"; |
| 6 | + |
| 7 | +function replaceTargetFromMainToBin(resolvedPath: string, binaryName: string): string { |
| 8 | + // we want to target the binary instead of the main index file |
| 9 | + // Improvement: search inside package.json "bin" and `main` field for more reliability |
| 10 | + return resolvedPath.replace( |
| 11 | + `${binaryName}${path.sep}dist${path.sep}index.js`, |
| 12 | + `${binaryName}${path.sep}bin${path.sep}${binaryName}`, |
| 13 | + ); |
| 14 | +} |
| 15 | +/** |
| 16 | + * Search for the binary in all workspaces' node_modules/.bin directories. |
| 17 | + * If multiple workspaces contain the binary, the first one found is returned. |
| 18 | + */ |
| 19 | +export async function searchProjectNodeModulesBin(binaryName: string): Promise<string | undefined> { |
| 20 | + // try to resolve via require.resolve |
| 21 | + try { |
| 22 | + const resolvedPath = replaceTargetFromMainToBin( |
| 23 | + require.resolve(binaryName, { |
| 24 | + paths: workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ?? [], |
| 25 | + }), |
| 26 | + binaryName, |
| 27 | + ); |
| 28 | + return resolvedPath; |
| 29 | + } catch {} |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Search for the binary in global node_modules. |
| 34 | + * Returns undefined if not found. |
| 35 | + */ |
| 36 | +export async function searchGlobalNodeModulesBin(binaryName: string): Promise<string | undefined> { |
| 37 | + // try to resolve via require.resolve |
| 38 | + try { |
| 39 | + const resolvedPath = replaceTargetFromMainToBin( |
| 40 | + require.resolve(binaryName, { paths: globalNodeModulesPaths() }), |
| 41 | + binaryName, |
| 42 | + ); |
| 43 | + return resolvedPath; |
| 44 | + } catch {} |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Search for the binary based on user settings. |
| 49 | + * If the path is relative, it is resolved against the first workspace folder. |
| 50 | + * Returns undefined if no valid binary is found or the path is unsafe. |
| 51 | + */ |
| 52 | +export async function searchSettingsBin(settingsBinary: string): Promise<string | undefined> { |
| 53 | + if (!workspace.isTrusted) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // validates the given path is safe to use |
| 58 | + if (!validateSafeBinaryPath(settingsBinary)) { |
| 59 | + return undefined; |
| 60 | + } |
| 61 | + |
| 62 | + if (!path.isAbsolute(settingsBinary)) { |
| 63 | + const cwd = workspace.workspaceFolders?.[0]?.uri.fsPath; |
| 64 | + if (!cwd) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + // if the path is not absolute, resolve it to the first workspace folder |
| 68 | + settingsBinary = path.normalize(path.join(cwd, settingsBinary)); |
| 69 | + } |
| 70 | + |
| 71 | + if (process.platform !== "win32" && settingsBinary.endsWith(".exe")) { |
| 72 | + // on non-Windows, remove `.exe` extension if present |
| 73 | + settingsBinary = settingsBinary.slice(0, -4); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + await workspace.fs.stat(Uri.file(settingsBinary)); |
| 78 | + return settingsBinary; |
| 79 | + } catch {} |
| 80 | + |
| 81 | + // on Windows, also check for `.exe` extension (bun uses `.exe` for its binaries) |
| 82 | + if (process.platform === "win32") { |
| 83 | + if (!settingsBinary.endsWith(".exe")) { |
| 84 | + settingsBinary += ".exe"; |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + await workspace.fs.stat(Uri.file(settingsBinary)); |
| 89 | + return settingsBinary; |
| 90 | + } catch {} |
| 91 | + } |
| 92 | + |
| 93 | + // no valid binary found |
| 94 | + return undefined; |
| 95 | +} |
| 96 | + |
| 97 | +// copied from: https://github.com/biomejs/biome-vscode/blob/ae9b6df2254d0ff8ee9d626554251600eb2ca118/src/locator.ts#L28-L49 |
| 98 | +function globalNodeModulesPaths(): string[] { |
| 99 | + const npmGlobalNodeModulesPath = safeSpawnSync("npm", ["root", "-g"]); |
| 100 | + const pnpmGlobalNodeModulesPath = safeSpawnSync("pnpm", ["root", "-g"]); |
| 101 | + const bunGlobalNodeModulesPath = path.resolve(homedir(), ".bun/install/global/node_modules"); |
| 102 | + |
| 103 | + return [npmGlobalNodeModulesPath, pnpmGlobalNodeModulesPath, bunGlobalNodeModulesPath].filter( |
| 104 | + Boolean, |
| 105 | + ) as string[]; |
| 106 | +} |
| 107 | + |
| 108 | +// only use this function with internal code, because it executes shell commands |
| 109 | +// which could be a security risk if the command or args are user-controlled |
| 110 | +const safeSpawnSync = (command: string, args: readonly string[] = []): string | undefined => { |
| 111 | + let output: string | undefined; |
| 112 | + |
| 113 | + try { |
| 114 | + const result = spawnSync(command, args, { |
| 115 | + shell: true, |
| 116 | + encoding: "utf8", |
| 117 | + }); |
| 118 | + |
| 119 | + if (result.error || result.status !== 0) { |
| 120 | + output = undefined; |
| 121 | + } else { |
| 122 | + const trimmed = result.stdout.trim(); |
| 123 | + output = trimmed ? trimmed : undefined; |
| 124 | + } |
| 125 | + } catch { |
| 126 | + output = undefined; |
| 127 | + } |
| 128 | + |
| 129 | + return output; |
| 130 | +}; |
0 commit comments