Add port customization support via --port argument

This commit is contained in:
2026-03-13 00:52:19 +08:00
parent 094c88caea
commit a078666173
2 changed files with 22 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ const fs = require('fs');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
let tray = null; let tray = null;
let serverPort = '3000';
function startServer() { function startServer() {
console.log('Starting OpenChamber server...'); console.log('Starting OpenChamber server...');
@@ -26,23 +27,26 @@ function startServer() {
console.log('CWD:', cwdPath); console.log('CWD:', cwdPath);
console.log('Exec:', execPath); console.log('Exec:', execPath);
const env = { ...process.env }; // 支持命令行参数或环境变量指定端口
env.OPENCHAMBER_PORT = '3000'; const port = process.argv.find(arg => arg.startsWith('--port='))?.split('=')[1]
env.OPENCODE_SKIP_START = 'true'; || process.env.OPENCHAMBER_PORT
|| '3000';
// 创建批处理脚本 serverPort = port;
const batContent = `"${execPath}" "${serverPath}"`; console.log('Port:', port);
const batPath = path.join(cwdPath, 'start-server.bat');
fs.writeFileSync(batPath, batContent);
console.log('Bat path:', batPath);
try { try {
const child = spawn('cmd.exe', ['/c', batPath], { // 设置环境变量
const env = { ...process.env };
env.OPENCHAMBER_PORT = port;
env.OPENCODE_SKIP_START = 'true';
// 直接 spawn用命令行参数指定端口
const child = spawn(execPath, [serverPath, '--port=' + port], {
cwd: cwdPath, cwd: cwdPath,
stdio: 'inherit', stdio: 'inherit',
env: env, env: env,
detached: false windowsHide: true
}); });
child.on('error', (err) => { child.on('error', (err) => {
@@ -57,7 +61,7 @@ function startServer() {
} }
} }
function createTray() { function createTray(port) {
try { try {
let iconPath; let iconPath;
if (app.isPackaged) { if (app.isPackaged) {
@@ -79,10 +83,10 @@ function createTray() {
tray = new Tray(icon); tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([ const contextMenu = Menu.buildFromTemplate([
{ label: 'OpenChamber Server', enabled: false }, { label: `OpenChamber Server (:${port})`, enabled: false },
{ type: 'separator' }, { type: 'separator' },
{ label: 'Open http://localhost:3000', click: () => { { label: `Open http://localhost:${port}`, click: () => {
require('electron').shell.openExternal('http://localhost:3000'); require('electron').shell.openExternal(`http://localhost:${port}`);
}}, }},
{ type: 'separator' }, { type: 'separator' },
{ label: 'Quit', click: () => { { label: 'Quit', click: () => {
@@ -90,7 +94,7 @@ function createTray() {
}} }}
]); ]);
tray.setToolTip('OpenChamber Server'); tray.setToolTip(`OpenChamber Server (:${port})`);
tray.setContextMenu(contextMenu); tray.setContextMenu(contextMenu);
} catch (e) { } catch (e) {
console.error('Failed to create tray:', e); console.error('Failed to create tray:', e);
@@ -99,7 +103,7 @@ function createTray() {
app.whenReady().then(() => { app.whenReady().then(() => {
startServer(); startServer();
createTray(); createTray(serverPort);
}); });
app.on('window-all-closed', () => { app.on('window-all-closed', () => {

1
web/start-server.bat Normal file
View File

@@ -0,0 +1 @@
"node" "D:\Xuanchi\高斯泼溅\opencode\web\server\index.js"