#!/usr/bin/env node

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

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

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

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

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);
