import fs from "fs"; import path from "path"; export interface ResolvedCliTool { command: string; argsPrefix: string[]; } export interface ResolveCliToolOptions { root: string; binary: string; platform?: NodeJS.Platform; } export const resolveCliTool = ( options: ResolveCliToolOptions, ): ResolvedCliTool => { if (!/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(options.binary)) { throw new Error("CLI binary name is invalid"); } const platform = options.platform || process.platform; const executable = platform === "win32" ? `${options.binary}.cmd` : options.binary; const localBinary = path.join(options.root, "node_modules", ".bin", executable); if (fs.existsSync(localBinary) && fs.statSync(localBinary).isFile()) { return { command: localBinary, argsPrefix: [] }; } return { command: platform === "win32" ? "npx.cmd" : "npx", argsPrefix: ["--no-install", options.binary], }; };