7 lines
2.3 KiB
JavaScript
7 lines
2.3 KiB
JavaScript
import fs from 'node:fs';import path from 'node:path';import net from 'node:net';import {spawn} from 'node:child_process';
|
|
const read=p=>JSON.parse(fs.readFileSync(p,'utf8')),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),tag=process.argv[2];if(state.port!==55439||!/^[a-f0-9]{12}$/.test(tag??''))throw Error('Owned cost fixture required');const root=path.join(state.root,'private/ledger-'+tag),settings=path.join(root,'settings.json'),owned=read(settings),record=path.join(root,'cost-demo.json');if(owned.tag!==tag||owned.cluster!=='sgf_modular_regression_'+state.id||!fs.existsSync(path.join(root,'cost-fixture.json')))throw Error('Cost fixture mismatch');
|
|
if(fs.existsSync(record)){const d=read(record),r=await fetch(d.url,{signal:AbortSignal.timeout(5000)});if(!r.ok||!(await r.text()).includes('data-cost-demo="'+tag+'"'))throw Error('Saved demo stopped; inspect without reset');console.log(JSON.stringify({...d,already_running:true}));process.exit(0);}
|
|
const assets=path.join(owned.sgf.storage,'public/build');if(!fs.existsSync(assets))fs.cpSync(path.join(state.root,'private/ledger-d93394698835/storage/public/build'),assets,{recursive:true,errorOnExist:true});
|
|
let port;for(let p=8054;p<=8063&&!port;p++){try{await new Promise((resolve,reject)=>{const s=net.createServer();s.once('error',reject);s.listen(p,'127.0.0.1',()=>s.close(resolve));});port=p;}catch{}}if(!port)throw Error('No free loopback port');const log=fs.openSync(path.join(root,'cost-demo-server.log'),'a'),child=spawn('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['-S','127.0.0.1:'+port,path.resolve('scripts/ledger-cost-http-router.php')],{cwd:process.cwd(),windowsHide:true,detached:true,stdio:['ignore',log,log],env:{...process.env,LEDGER_QA_SETTINGS:settings}});child.unref();fs.closeSync(log);const url='http://127.0.0.1:'+port+'/qa/cost-demo';let ready=false;for(let n=0;n<50&&!ready;n++){try{const r=await fetch(url,{signal:AbortSignal.timeout(5000)});ready=r.ok&&(await r.text()).includes('data-cost-demo="'+tag+'"');}catch{}if(!ready)await new Promise(r=>setTimeout(r,100));}if(!ready){child.kill();throw Error('Own cost demo failed readiness');}
|
|
const result={tag,pid:child.pid,url,cookie:'sgf_cost_'+tag,loopback_only:true,separate_team_database:true,old_demos_not_reset:true};fs.writeFileSync(record,JSON.stringify(result,null,2)+'\n');console.log(JSON.stringify(result));
|