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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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";
  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(
  9. `[dev:web-remote-control] desktop ZCODE_WEB_REMOTE_CONTROL_URL=${webRemoteControlUrl}`,
  10. );
  11. const child = spawn(pnpmCommand, ["--filter", "@zcode/desktop", "dev:local-cli"], {
  12. stdio: "inherit",
  13. env: {
  14. ...process.env,
  15. ZCODE_WEB_REMOTE_CONTROL_URL: webRemoteControlUrl,
  16. },
  17. });
  18. let signalForwarded = false;
  19. const forwardSignal = (signal) => {
  20. if (signalForwarded) {
  21. return;
  22. }
  23. signalForwarded = true;
  24. child.kill(signal);
  25. };
  26. process.on("SIGINT", () => forwardSignal("SIGINT"));
  27. process.on("SIGTERM", () => forwardSignal("SIGTERM"));
  28. child.on("error", (error) => {
  29. console.error("[dev:web-remote-control] failed to start desktop dev process", error);
  30. process.exit(1);
  31. });
  32. child.on("exit", (code, signal) => {
  33. if (signal) {
  34. process.exit(1);
  35. }
  36. process.exit(code ?? 0);
  37. });