import { spawnSync } from "node:child_process";
import { join } from "node:path";

const rawArgs = process.argv.slice(2);
const passthroughArgs = [];

for (let index = 0; index < rawArgs.length; index += 1) {
  const arg = rawArgs[index];

  if (arg === "--testPathPattern") {
    const value = rawArgs[index + 1];
    if (value) {
      passthroughArgs.push(value);
      index += 1;
    }
    continue;
  }

  if (arg.startsWith("--testPathPattern=")) {
    passthroughArgs.push(arg.slice("--testPathPattern=".length));
    continue;
  }

  passthroughArgs.push(arg);
}

const vitestBin = join(process.cwd(), "node_modules", ".bin", "vitest");
const result = spawnSync(vitestBin, ["run", ...passthroughArgs], {
  stdio: "inherit",
});

if (result.error) {
  throw result.error;
}

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