(options = {})
| 1210 | } |
| 1211 | |
| 1212 | function startTerminal(options = {}) { |
| 1213 | if (!pty) { |
| 1214 | return { success: false, error: 'node-pty 未安装,请先安装依赖' } |
| 1215 | } |
| 1216 | |
| 1217 | const { cwd, shell } = typeof options === 'string' ? { cwd: options } : options |
| 1218 | const resolvedCwd = resolveCwd(cwd) |
| 1219 | const resolvedShell = normalizeShell(shell) |
| 1220 | const shellArgs = resolveShellArgs(shell || resolvedShell) |
| 1221 | const sessionId = ++ptySessionId |
| 1222 | |
| 1223 | try { |
| 1224 | console.log('[Terminal] spawn', { |
| 1225 | requestedShell: shell, |
| 1226 | resolvedShell, |
| 1227 | shellArgs, |
| 1228 | cwd: resolvedCwd, |
| 1229 | useConpty: USE_CONPTY, |
| 1230 | sessionId |
| 1231 | }) |
| 1232 | const ptyOptions = { |
| 1233 | name: 'xterm-256color', |
| 1234 | cols: 80, |
| 1235 | rows: 24, |
| 1236 | cwd: resolvedCwd, |
| 1237 | env: process.env |
| 1238 | } |
| 1239 | if (IS_WINDOWS) { |
| 1240 | ptyOptions.useConpty = USE_CONPTY |
| 1241 | } |
| 1242 | const spawned = pty.spawn(resolvedShell, shellArgs, ptyOptions) |
| 1243 | ptySessions.set(sessionId, { |
| 1244 | id: sessionId, |
| 1245 | pty: spawned, |
| 1246 | shell: resolvedShell |
| 1247 | }) |
| 1248 | |
| 1249 | spawned.onData((data) => { |
| 1250 | if (!ptySessions.has(sessionId)) return |
| 1251 | if (mainWindow && !mainWindow.isDestroyed()) { |
| 1252 | mainWindow.webContents.send('terminal-data', { sessionId, data }) |
| 1253 | } |
| 1254 | }) |
| 1255 | |
| 1256 | spawned.onExit(({ exitCode }) => { |
| 1257 | const exists = ptySessions.has(sessionId) |
| 1258 | console.log('[Terminal] exit', { resolvedShell, exitCode, sessionId, exists }) |
| 1259 | if (!exists) return |
| 1260 | ptySessions.delete(sessionId) |
| 1261 | if (mainWindow && !mainWindow.isDestroyed()) { |
| 1262 | mainWindow.webContents.send('terminal-exit', { sessionId, exitCode }) |
| 1263 | } |
| 1264 | }) |
| 1265 | } catch (err) { |
| 1266 | return { success: false, error: err.message } |
| 1267 | } |
| 1268 | |
| 1269 | return { success: true, sessionId } |
no test coverage detected