package file→ .shtukbase/packages/live/actions/serve.ts
actions/serve.ts
37 lines, 1.3 KB.
- // Action `serve [--port N]`: the live server on the project in the working folder, until Ctrl-C.
- // The port: --port, then `port:` of /base/live.md, then 4322.
- import { start } from '../server/server.ts';
- const args = process.argv.slice(2);
- let port: number | undefined;
- for (let i = 0; i < args.length; i++) {
- const a = args[i];
- const v = a === '--port' ? args[++i] : a.startsWith('--port=') ? a.slice(7) : /^\d+$/.test(a) ? a : null;
- if (v === null) {
- console.error(`did not understand "${a}": shtuk run live serve [--port N]`);
- process.exit(2);
- }
- port = Number(v);
- if (!Number.isInteger(port) || port < 0 || port > 65535) {
- console.error(`port "${v}" does not fit: a number 1-65535 is needed`);
- process.exit(2);
- }
- }
- let live;
- try {
- live = start({ root: process.cwd(), port });
- } catch (e) {
- const m = (e as Error).message;
- console.error(/EADDRINUSE|in use/i.test(m) ? `port ${port ?? ''} is busy: choose another one, --port N` : m);
- process.exit(2);
- }
- console.log(`Live server of the project ${process.cwd()}`);
- console.log(` editing (link with the session token): ${live.link}`);
- console.log(` read only: ${live.url}`);
- console.log(' Ctrl-C to stop');
- for (const sig of ['SIGINT', 'SIGTERM'] as const)
- process.on(sig, () => {
- live.stop();
- process.exit(0);
- });