okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
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 ) { 25const child = spawn ( command , args , { 26cwd :ROOT , 27env :{ ...process . env , ...env }, 28stdio :[ 'ignore' , 'pipe' , 'pipe' ], 29}); 30const prefix = ` ${ palette [ name ]} ${ name . padEnd ( 4 )} ${ palette . off } ${ palette . dim } │ ${ palette . off } ` ; 31const pipe = ( stream , target ) => { 32let buffer = '' ; 33stream . on ( 'data' , ( chunk ) => { 34buffer += chunk . toString (); 35const lines = buffer . split ( '\n' ); 36buffer = lines . pop () ?? '' ; 37for ( const line of lines ) target . write ( ` ${ prefix } ${ line } \n` ); 38}); 39}; 40pipe ( child . stdout , process . stdout ); 41pipe ( child . stderr , process . stderr ); 42child . on ( 'exit' , ( code , signal ) => { 43if ( shuttingDown ) return ; 44console . log ( ` ${ prefix } exited ( ${ signal ?? code } )` ); 45shutdown ( code ?? 0 ); 46}); 47children . push ( child ); 48return child ; 49} 50 51function shutdown ( code = 0 ) { 52if ( shuttingDown ) return ; 53shuttingDown = true ; 54for ( const child of children ) child . kill ( 'SIGTERM' ); 55setTimeout (() => 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' )], { 65STRATA_HOST :`http://127.0.0.1: ${ HOST_PORT } ` , 66});