dev-web-remote-control-desktop.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env node
  2. import { spawn } from "node:child_process";
  3. import process from "node:process";
  4. const DEFAULT_WEB_REMOTE_CONTROL_URL = "http://localhost:5173/remote/v4";
  5. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  6. const webRemoteControlUrl =
  7. process.env.ZCODE_WEB_REMOTE_CONTROL_URL?.trim() || DEFAULT_WEB_REMOTE_CONTROL_URL;
  8. console.log(`[dev:web-remote-control] desktop ZCODE_WEB_REMOTE_CONTROL_URL=${webRemoteControlUrl}`);
  9. const child = spawn(pnpmCommand, ["--filter", "@zcode/desktop", "dev:local-cli"], {
  10. stdio: "inherit",
  11. env: {
  12. ...process.env,
  13. ZCODE_WEB_REMOTE_CONTROL_URL: webRemoteControlUrl,
  14. },
  15. });
  16. let signalForwarded = false;
  17. const forwardSignal = (signal) => {
  18. if (signalForwarded) {
  19. return;
  20. }
  21. signalForwarded = true;
  22. child.kill(signal);
  23. };
  24. process.on("SIGINT", () => forwardSignal("SIGINT"));
  25. process.on("SIGTERM", () => forwardSignal("SIGTERM"));
  26. child.on("error", (error) => {
  27. console.error("[dev:web-remote-control] failed to start desktop dev process", error);
  28. process.exit(1);
  29. });
  30. child.on("exit", (code, signal) => {
  31. if (signal) {
  32. process.exit(1);
  33. }
  34. process.exit(code ?? 0);
  35. });