2026-12-04 08:37:28 -08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 由 lib/server.js 以 fork + --require fake-clock.cjs 启动;只通过 IPC 接受启动/停止指令。
|
|
|
|
|
|
// 启动参数来自环境变量 E2E_SERVER_OPTIONS(JSON):{mode,dataDir,startup:'create'|'production'}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
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') {
|
2026-12-04 08:37:28 -08:00
|
|
|
|
// 与 server.js 正式入口一致:先备份升级前旧库,再 createApp,再显式迁移,最后监听。
|
2026-12-04 08:58:53 -08:00
|
|
|
|
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: () => {},
|
|
|
|
|
|
});
|
2026-12-04 08:37:28 -08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
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 });
|
2026-12-04 08:37:28 -08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
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);
|
2026-12-04 08:37:28 -08:00
|
|
|
|
});
|