import { spawnSync } from "child_process"; import type { CommandResult } from "../checkCodeRunner"; import { resolveCliTool } from "../cliTool"; const commandName = (command: string): string => { if (process.platform !== "win32") return command; return command === "npm" || command === "npx" ? command + ".cmd" : command; }; export const runCheckCodeCommand = ( command: string, commandArgs: string[], options: { cwd: string; env?: NodeJS.ProcessEnv } = { cwd: process.cwd() }, ): CommandResult => { const env = { ...process.env, ...options.env }; let executable = commandName(command); let executableArgs = commandArgs; if (command === "npx" && commandArgs.includes("gate-tool")) { env.NODE_OPTIONS = [env.NODE_OPTIONS, "--require", "ts-node/register"] .filter(Boolean) .join(" "); const gateTool = resolveCliTool({ root: options.cwd, binary: "gate-tool" }); const binaryAt = commandArgs.indexOf("gate-tool"); executable = gateTool.command; executableArgs = [...gateTool.argsPrefix, ...commandArgs.slice(binaryAt + 1)]; } const result = spawnSync(executable, executableArgs, { cwd: options.cwd, env, stdio: "inherit", }); return { status: result.status ?? 1, error: result.error?.message, }; };