Add port customization support via --port argument
This commit is contained in:
@@ -4,6 +4,7 @@ const fs = require('fs');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
let tray = null;
|
||||
let serverPort = '3000';
|
||||
|
||||
function startServer() {
|
||||
console.log('Starting OpenChamber server...');
|
||||
@@ -26,23 +27,26 @@ function startServer() {
|
||||
console.log('CWD:', cwdPath);
|
||||
console.log('Exec:', execPath);
|
||||
|
||||
const env = { ...process.env };
|
||||
env.OPENCHAMBER_PORT = '3000';
|
||||
env.OPENCODE_SKIP_START = 'true';
|
||||
// 支持命令行参数或环境变量指定端口
|
||||
const port = process.argv.find(arg => arg.startsWith('--port='))?.split('=')[1]
|
||||
|| process.env.OPENCHAMBER_PORT
|
||||
|| '3000';
|
||||
|
||||
// 创建批处理脚本
|
||||
const batContent = `"${execPath}" "${serverPath}"`;
|
||||
const batPath = path.join(cwdPath, 'start-server.bat');
|
||||
fs.writeFileSync(batPath, batContent);
|
||||
|
||||
console.log('Bat path:', batPath);
|
||||
serverPort = port;
|
||||
console.log('Port:', port);
|
||||
|
||||
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,
|
||||
stdio: 'inherit',
|
||||
env: env,
|
||||
detached: false
|
||||
windowsHide: true
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
@@ -57,7 +61,7 @@ function startServer() {
|
||||
}
|
||||
}
|
||||
|
||||
function createTray() {
|
||||
function createTray(port) {
|
||||
try {
|
||||
let iconPath;
|
||||
if (app.isPackaged) {
|
||||
@@ -79,10 +83,10 @@ function createTray() {
|
||||
tray = new Tray(icon);
|
||||
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: 'OpenChamber Server', enabled: false },
|
||||
{ label: `OpenChamber Server (:${port})`, enabled: false },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Open http://localhost:3000', click: () => {
|
||||
require('electron').shell.openExternal('http://localhost:3000');
|
||||
{ label: `Open http://localhost:${port}`, click: () => {
|
||||
require('electron').shell.openExternal(`http://localhost:${port}`);
|
||||
}},
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', click: () => {
|
||||
@@ -90,7 +94,7 @@ function createTray() {
|
||||
}}
|
||||
]);
|
||||
|
||||
tray.setToolTip('OpenChamber Server');
|
||||
tray.setToolTip(`OpenChamber Server (:${port})`);
|
||||
tray.setContextMenu(contextMenu);
|
||||
} catch (e) {
|
||||
console.error('Failed to create tray:', e);
|
||||
@@ -99,7 +103,7 @@ function createTray() {
|
||||
|
||||
app.whenReady().then(() => {
|
||||
startServer();
|
||||
createTray();
|
||||
createTray(serverPort);
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
|
||||
1
web/start-server.bat
Normal file
1
web/start-server.bat
Normal file
@@ -0,0 +1 @@
|
||||
"node" "D:\Xuanchi\高斯泼溅\opencode\web\server\index.js"
|
||||
Reference in New Issue
Block a user