ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/live/actions/serve.ts

actions/serve.ts

37 líneas, 1.3 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // Action `serve [--port N]`: the live server on the project in the working folder, until Ctrl-C.
  2. // The port: --port, then `port:` of /base/live.md, then 4322.
  3. import { start } from '../server/server.ts';
  4. const args = process.argv.slice(2);
  5. let port: number | undefined;
  6. for (let i = 0; i < args.length; i++) {
  7. const a = args[i];
  8. const v = a === '--port' ? args[++i] : a.startsWith('--port=') ? a.slice(7) : /^\d+$/.test(a) ? a : null;
  9. if (v === null) {
  10. console.error(`did not understand "${a}": shtuk run live serve [--port N]`);
  11. process.exit(2);
  12. }
  13. port = Number(v);
  14. if (!Number.isInteger(port) || port < 0 || port > 65535) {
  15. console.error(`port "${v}" does not fit: a number 1-65535 is needed`);
  16. process.exit(2);
  17. }
  18. }
  19. let live;
  20. try {
  21. live = start({ root: process.cwd(), port });
  22. } catch (e) {
  23. const m = (e as Error).message;
  24. console.error(/EADDRINUSE|in use/i.test(m) ? `port ${port ?? ''} is busy: choose another one, --port N` : m);
  25. process.exit(2);
  26. }
  27. console.log(`Live server of the project ${process.cwd()}`);
  28. console.log(` editing (link with the session token): ${live.link}`);
  29. console.log(` read only: ${live.url}`);
  30. console.log(' Ctrl-C to stop');
  31. for (const sig of ['SIGINT', 'SIGTERM'] as const)
  32. process.on(sig, () => {
  33. live.stop();
  34. process.exit(0);
  35. });