From d023705220279dfa1fa38bbb5bccf8d98a93c498 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 22 Mar 2026 00:37:58 +0800 Subject: [PATCH] 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 --- electron/main.js | 67 +++++++++++++++--------------------------------- package.json | 9 ++++--- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/electron/main.js b/electron/main.js index 95c7921..e16e77d 100644 --- a/electron/main.js +++ b/electron/main.js @@ -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,27 +86,28 @@ 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(` ╔═══════════════════════════════════════════════════════════╗ -║ XCTerminal (Electron Headless) ║ +║ XCTerminal (Electron Headless) ║ ╠═══════════════════════════════════════════════════════════╣ ║ 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()); }); diff --git a/package.json b/package.json index b3d1af1..e00629f 100644 --- a/package.json +++ b/package.json @@ -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