const { spawnSync } = require("node:child_process");

const args = process.argv.slice(2);
if (args.length === 0) {
  console.error("[run_python] missing script arguments");
  process.exit(1);
}

const candidates = [
  { command: "python3", args: [] },
  { command: "python", args: [] },
  { command: "py", args: ["-3"] },
];

for (const candidate of candidates) {
  const result = spawnSync(candidate.command, [...candidate.args, ...args], {
    stdio: "inherit",
  });

  if (
    (result.error && result.error.code === "ENOENT") ||
    (process.platform === "win32" && result.status === 9009)
  ) {
    continue;
  }

  process.exit(result.status ?? 1);
}

console.error("[run_python] unable to find python3, python, or py -3");
process.exit(1);
