Files
weiguang-jewelry-ledger/新账系统/e2e/lib/server-child.cjs
T

30 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
// 由 lib/server.js 以 fork + --require fake-clock.cjs 启动;只通过 IPC 接受启动/停止指令。
// 启动参数来自环境变量 E2E_SERVER_OPTIONS(JSON):{mode,dataDir,startup:'create'|'production'}
const path=require('node:path');
const appDir=path.join(__dirname,'..','..');
const options=JSON.parse(process.env.E2E_SERVER_OPTIONS||'{}');
const send=message=>process.send&&process.send(message);
let app=null;
async function start(){
const server=require(path.join(appDir,'server.js'));
let startup={};
if(options.startup==='production'){
// 与 server.js 正式入口一致:先备份升级前旧库,再 createApp,再显式迁移,最后监听。
startup.team=await server.prepareTeamStartupDatabase(options.dataDir);
app=server.createApp({mode:options.mode,dataDir:options.dataDir,host:'127.0.0.1',port:0});
startup.migration=await server.prepareStartupMigrations(app);
}else{
app=server.createApp({mode:options.mode||'trial',dataDir:options.dataDir,host:'127.0.0.1',port:0,inventorySnapshotLogger:()=>{}});
}
await new Promise((resolve,reject)=>{app.server.once('error',reject);app.server.listen(0,'127.0.0.1',resolve);});
send({type:'ready',port:app.server.address().port,startup});
}
process.on('message',async message=>{
if(message?.type!=='stop')return;
try{if(app){app.stop();app.ledger.close();}}catch(error){send({type:'log',message:'stop: '+error.message});}
send({type:'stopped'});setTimeout(()=>process.exit(0),20);
});
process.on('disconnect',()=>process.exit(0));
start().catch(error=>{send({type:'fatal',message:error.stack||String(error)});setTimeout(()=>process.exit(1),20);});