fix: properly parse --port argument in packaged Electron app

- Use regex on full argv string instead of iterating args
- Move express to dependencies for packaging
- Add electron folder to files list in build config
This commit is contained in:
2026-03-22 00:37:58 +08:00
parent 38f6d5e97c
commit d023705220
2 changed files with 26 additions and 50 deletions

View File

@@ -1,50 +1,24 @@
const { spawn } = require('child_process');
const WebSocket = require('ws');
const express = require('express');
const path = require('path');
const fs = require('fs');
const pty = require('node-pty');
const http = require('http');
const { app } = require('electron');
const args = process.argv.slice(2);
let port = 9997;
let debugPort = null;
const args = process.argv.join(' ');
const portMatch = args.match(/--port[=\s]+(\d+)/);
const PORT = portMatch ? parseInt(portMatch[1], 10) : 9997;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--port' && i + 1 < args.length) {
port = parseInt(args[i + 1], 10);
} else if (args[i] === '--debug-port' && i + 1 < args.length) {
debugPort = args[i + 1];
}
}
const SHELL = process.platform === 'win32' ? 'powershell.exe' : 'bash';
if (isNaN(port)) {
console.error('Invalid port number:', args[i + 1]);
process.exit(1);
}
const expressApp = express();
const server = http.createServer(expressApp);
const wss = new WebSocket.Server({ server });
const env = { ...process.env, PORT: port.toString() };
expressApp.use(express.static(path.join(__dirname, '../src/frontend')));
const serverPath = path.join(__dirname, '../src/backend/server.js');
const child = spawn(process.execPath, [serverPath], {
env,
stdio: 'inherit',
detached: false
});
child.on('error', (err) => {
console.error('Failed to start server:', err);
process.exit(1);
});
child.on('exit', (code, signal) => {
process.exit(code || 0);
});
process.on('SIGINT', () => {
child.kill('SIGINT');
process.exit(0);
});
process.on('SIGTERM', () => {
child.kill('SIGTERM');
process.exit(0);
expressApp.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../src/frontend/index.html'));
});
const terminals = new Map();
@@ -112,9 +86,13 @@ process.on('SIGINT', () => {
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('no-sandbox');
app.commandLine.appendSwitch('headless');
app.disableHardwareAcceleration();
app.on('window-all-closed', () => {
});
app.whenReady().then(() => {
server.listen(PORT, () => {
console.log(`
@@ -124,15 +102,12 @@ app.whenReady().then(() => {
║ Server running at: http://localhost:${PORT}
║ WebSocket endpoint: ws://localhost:${PORT}
║ ║
║ Usage: electron . --port 9997
║ Usage: XCTerminal.exe --port 9997 ║
╚═══════════════════════════════════════════════════════════╝
`);
});
});
app.on('window-all-closed', () => {
});
process.on('exit', () => {
terminals.forEach((proc) => proc.kill());
});

View File

@@ -2,7 +2,7 @@
"name": "ghostty-web",
"version": "0.1.0",
"description": "Web-based terminal using libghostty-vt and xterm.js",
"main": "src/backend/server.js",
"main": "electron/main.js",
"scripts": {
"start": "electron .",
"dev": "electron . --port 9997",
@@ -19,13 +19,13 @@
"license": "MIT",
"author": "XCTerminal",
"dependencies": {
"express": "^4.18.2",
"node-pty": "^1.0.0",
"ws": "^8.18.0"
},
"devDependencies": {
"electron": "^41.0.3",
"electron-builder": "^26.8.1",
"express": "^4.18.2"
"electron-builder": "^26.8.1"
},
"build": {
"appId": "com.xcterminal.app",
@@ -49,12 +49,13 @@
"target": ["dmg"]
},
"files": [
"electron/**/*",
"src/**/*",
"node_modules/**/*",
"!node_modules/.cache/**/*"
],
"extraMetadata": {
"main": "src/backend/server.js"
"main": "electron/main.js"
},
"nodeGypRebuild": false,
"npmRebuild": false