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 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); const args = process.argv.join(' ');
let port = 9997; const portMatch = args.match(/--port[=\s]+(\d+)/);
let debugPort = null; const PORT = portMatch ? parseInt(portMatch[1], 10) : 9997;
for (let i = 0; i < args.length; i++) { const SHELL = process.platform === 'win32' ? 'powershell.exe' : 'bash';
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];
}
}
if (isNaN(port)) { const expressApp = express();
console.error('Invalid port number:', args[i + 1]); const server = http.createServer(expressApp);
process.exit(1); 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'); expressApp.get('/', (req, res) => {
const child = spawn(process.execPath, [serverPath], { res.sendFile(path.join(__dirname, '../src/frontend/index.html'));
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);
}); });
const terminals = new Map(); const terminals = new Map();
@@ -112,27 +86,28 @@ process.on('SIGINT', () => {
app.commandLine.appendSwitch('disable-gpu'); app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('no-sandbox'); app.commandLine.appendSwitch('no-sandbox');
app.commandLine.appendSwitch('headless');
app.disableHardwareAcceleration(); app.disableHardwareAcceleration();
app.on('window-all-closed', () => {
});
app.whenReady().then(() => { app.whenReady().then(() => {
server.listen(PORT, () => { server.listen(PORT, () => {
console.log(` console.log(`
╔═══════════════════════════════════════════════════════════╗ ╔═══════════════════════════════════════════════════════════╗
║ XCTerminal (Electron Headless) ║ XCTerminal (Electron Headless) ║
╠═══════════════════════════════════════════════════════════╣ ╠═══════════════════════════════════════════════════════════╣
║ Server running at: http://localhost:${PORT} ║ Server running at: http://localhost:${PORT}
║ WebSocket endpoint: ws://localhost:${PORT} ║ WebSocket endpoint: ws://localhost:${PORT}
║ ║ ║ ║
║ Usage: electron . --port 9997 ║ Usage: XCTerminal.exe --port 9997 ║
╚═══════════════════════════════════════════════════════════╝ ╚═══════════════════════════════════════════════════════════╝
`); `);
}); });
}); });
app.on('window-all-closed', () => {
});
process.on('exit', () => { process.on('exit', () => {
terminals.forEach((proc) => proc.kill()); terminals.forEach((proc) => proc.kill());
}); });

View File

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