dev-web-share-env.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { spawn } from "node:child_process";
  2. const TEST_SHARE_ENDPOINT_ORIGIN = "https://zcode.z.ai";
  3. const mode = process.argv[2]?.trim().toLowerCase();
  4. if (mode !== "test" && mode !== "mock") {
  5. console.error("Usage: node scripts/dev-web-share-env.mjs <test|mock>");
  6. process.exit(1);
  7. }
  8. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  9. const env = {
  10. ...process.env,
  11. ZCODE_ENV: "test",
  12. VITE_ZCODE_BASE_URL: TEST_SHARE_ENDPOINT_ORIGIN,
  13. VITE_DEV_ORIGIN: "http://localhost:5173",
  14. VITE_CONVERSATION_SHARE_PREVIEW_MOCK: mode === "mock" ? "true" : "false",
  15. };
  16. const args = [
  17. "--filter",
  18. "@zcode/web-share",
  19. "exec",
  20. "vite",
  21. "dev",
  22. "--strictPort",
  23. "--host",
  24. "127.0.0.1",
  25. // 分享页同时支持 /cn/share/<code>(中文)与 /share/<code>(英文)。
  26. // 之前这里写死 --base=/cn/share/,Vite 就只服务该前缀,访问 /share/<code> 会被
  27. // dev server 直接拒掉("server is configured with a public base URL of /cn/share/"),
  28. // 英文路由在本地根本没法验。base 用 / 让两个前缀都能落到 SPA。
  29. //
  30. // 注意生产构建(package.json 的 build:web-share)仍是 --base=/cn/share/:那决定的是
  31. // 静态资源的绝对路径,要不要改取决于站点怎么托管,不能只看本地。
  32. "--base=/",
  33. ];
  34. if (mode === "mock") {
  35. args.push("--open", "/cn/share/mock-importable");
  36. }
  37. console.info(
  38. `[dev:web-share] mode=${mode} endpoint=${TEST_SHARE_ENDPOINT_ORIGIN} ` +
  39. "share code is read from /cn/share/<code> (zh) or /share/<code> (en)",
  40. );
  41. const child = spawn(pnpmCommand, args, {
  42. cwd: process.cwd(),
  43. env,
  44. stdio: "inherit",
  45. shell: process.platform === "win32",
  46. });
  47. const forwardSignal = (signal) => child.kill(signal);
  48. process.on("SIGINT", () => forwardSignal("SIGINT"));
  49. process.on("SIGTERM", () => forwardSignal("SIGTERM"));
  50. child.on("error", (error) => {
  51. console.error("[dev:web-share] failed to start Vite", error);
  52. process.exit(1);
  53. });
  54. child.on("exit", (code, signal) => {
  55. if (signal) process.kill(process.pid, signal);
  56. process.exit(code ?? 1);
  57. });