#!/usr/bin/env node

import { spawnSync } from "node:child_process";
import process from "node:process";
import { resolveRealWorkspaceRoot } from "./load-env.mjs";

const args = process.argv.slice(2);

if (args.length === 0) {
  console.error("Usage: node scripts/run-from-workspace-root.mjs <command> [args...]");
  process.exit(1);
}

const workspaceRoot = resolveRealWorkspaceRoot();
process.chdir(workspaceRoot);

const result = spawnSync(args[0], args.slice(1), {
  stdio: "inherit",
  env: process.env,
  cwd: workspaceRoot,
});

if (result.error) {
  console.error(result.error.message);
  process.exit(1);
}

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