okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
2.3 KiB66 linesraw
1#!/usr/bin/env node
2/**
3 * One command for the whole dev loop.
4 *
5 *   npm run dev
6 *
7 * Starts the engine host (:8791) and the Vite dev server (:5199), prefixes their
8 * output, and shuts both down together. Vite proxies /api to the host, so the
9 * browser only ever talks to one origin — the same shape as the packaged app.
10 */
11import { spawn } from 'node:child_process';
12import { fileURLToPath } from 'node:url';
13import { dirname, join, resolve } from 'node:path';
14
15const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
16const HOST_PORT = process.env.STRATA_PORT ?? '8791';
17
18const palette = { host: '\u001b[38;5;114m', web: '\u001b[38;5;111m', dim: '\u001b[2m', off: '\u001b[0m' };
19
20/** @type {import('node:child_process').ChildProcess[]} */
21const children = [];
22let shuttingDown = false;
23
24function run(name, command, args, env) {
25  const child = spawn(command, args, {
26    cwd: ROOT,
27    env: { ...process.env, ...env },
28    stdio: ['ignore', 'pipe', 'pipe'],
29  });
30  const prefix = `${palette[name]}${name.padEnd(4)}${palette.off} ${palette.dim}│${palette.off} `;
31  const pipe = (stream, target) => {
32    let buffer = '';
33    stream.on('data', (chunk) => {
34      buffer += chunk.toString();
35      const lines = buffer.split('\n');
36      buffer = lines.pop() ?? '';
37      for (const line of lines) target.write(`${prefix}${line}\n`);
38    });
39  };
40  pipe(child.stdout, process.stdout);
41  pipe(child.stderr, process.stderr);
42  child.on('exit', (code, signal) => {
43    if (shuttingDown) return;
44    console.log(`${prefix}exited (${signal ?? code})`);
45    shutdown(code ?? 0);
46  });
47  children.push(child);
48  return child;
49}
50
51function shutdown(code = 0) {
52  if (shuttingDown) return;
53  shuttingDown = true;
54  for (const child of children) child.kill('SIGTERM');
55  setTimeout(() => process.exit(code), 250);
56}
57
58process.on('SIGINT', () => shutdown(0));
59process.on('SIGTERM', () => shutdown(0));
60
61console.log(`${palette.dim}strata dev · host :${HOST_PORT} · web :5199${palette.off}`);
62// --watch restarts the host on server edits without taking the web server down.
63run('host', process.execPath, ['--watch', join(ROOT, 'server', 'index.mjs')], { STRATA_PORT: HOST_PORT });
64run('web', process.execPath, [join(ROOT, 'node_modules', 'vite', 'bin', 'vite.js')], {
65  STRATA_HOST: `http://127.0.0.1:${HOST_PORT}`,
66});