File size: 1,458 Bytes
7f88bdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

const repoRoot = process.cwd();
const mode = process.argv[2] ?? "dev";

function loadEnv(filePath) {
  if (!fs.existsSync(filePath)) {
    return {};
  }

  const env = {};
  const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/);
  for (const raw of lines) {
    const line = raw.trim();
    if (!line || line.startsWith("#")) {
      continue;
    }
    const idx = line.indexOf("=");
    if (idx < 0) {
      continue;
    }
    const key = line.slice(0, idx).trim();
    const value = line.slice(idx + 1);
    env[key] = value;
  }

  return env;
}

const rootEnv = loadEnv(path.join(repoRoot, ".env.local"));
const appEnv = loadEnv(path.join(repoRoot, "apps", "web", ".env.local"));

const allowedModes = new Set(["dev", "build", "start", "lint", "typecheck"]);
if (!allowedModes.has(mode)) {
  console.error(`Unsupported mode: ${mode}`);
  process.exit(1);
}

const child = spawn("cmd.exe", ["/d", "/s", "/c", "npm.cmd", "run", mode, "--workspace", "apps/web"], {
  cwd: repoRoot,
  env: {
    ...rootEnv,
    ...appEnv,
    ...process.env
  },
  stdio: "inherit",
  shell: false
});

child.on("exit", (code, signal) => {
  if (signal) {
    process.kill(process.pid, signal);
    return;
  }
  process.exit(code ?? 0);
});

child.on("error", (error) => {
  console.error(`Failed to run web command: ${error.message}`);
  process.exit(1);
});