|
| 1 | +import pty from '@homebridge/node-pty-prebuilt-multiarch'; |
| 2 | +import { EventEmitter } from 'node:events'; |
| 3 | +import stripAnsi from 'strip-ansi'; |
| 4 | + |
| 5 | +import { Shell, Utils } from '../utils/index.js'; |
| 6 | +import { VerbosityLevel } from '../utils/internal-utils.js'; |
| 7 | +import { IPty, SpawnError, SpawnOptions, SpawnResult, SpawnStatus } from './index.js'; |
| 8 | + |
| 9 | +EventEmitter.defaultMaxListeners = 1000; |
| 10 | + |
| 11 | +/** |
| 12 | + * The background pty is a specialized pty designed for speed. It can launch multiple tasks |
| 13 | + * in parallel by moving them to the background. It attaches unix FIFO pipes to each process |
| 14 | + * to listen to stdout and stderr. One limitation of the BackgroundPty is that the tasks run |
| 15 | + * without a tty (or even a stdin) attached so interactive commands will not work. |
| 16 | + */ |
| 17 | +export class SequentialPty implements IPty { |
| 18 | + async spawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult> { |
| 19 | + const spawnResult = await this.spawnSafe(cmd, options); |
| 20 | + |
| 21 | + if (spawnResult.status !== 'success') { |
| 22 | + throw new SpawnError(cmd, spawnResult.exitCode, spawnResult.data); |
| 23 | + } |
| 24 | + |
| 25 | + return spawnResult; |
| 26 | + } |
| 27 | + |
| 28 | + async spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> { |
| 29 | + console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : '')) |
| 30 | + |
| 31 | + return new Promise((resolve) => { |
| 32 | + const output: string[] = []; |
| 33 | + |
| 34 | + const historyIgnore = Utils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' }; |
| 35 | + |
| 36 | + // If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included |
| 37 | + // in the response. |
| 38 | + const env = { |
| 39 | + ...process.env, ...options?.env, |
| 40 | + TERM_PROGRAM: 'codify', |
| 41 | + COMMAND_MODE: 'unix2003', |
| 42 | + COLORTERM: 'truecolor', ...historyIgnore |
| 43 | + } |
| 44 | + |
| 45 | + // Initial terminal dimensions |
| 46 | + const initialCols = process.stdout.columns ?? 80; |
| 47 | + const initialRows = process.stdout.rows ?? 24; |
| 48 | + |
| 49 | + const args = (options?.interactive ?? true) ? ['-i', '-c', `"${cmd}"`] : ['-c', `"${cmd}"`] |
| 50 | + |
| 51 | + // Run the command in a pty for interactivity |
| 52 | + const mPty = pty.spawn(this.getDefaultShell(), args, { |
| 53 | + ...options, |
| 54 | + cols: initialCols, |
| 55 | + rows: initialRows, |
| 56 | + env |
| 57 | + }); |
| 58 | + |
| 59 | + mPty.onData((data) => { |
| 60 | + if (VerbosityLevel.get() > 0) { |
| 61 | + process.stdout.write(data); |
| 62 | + } |
| 63 | + |
| 64 | + output.push(data.toString()); |
| 65 | + }) |
| 66 | + |
| 67 | + const stdinListener = (data) => { |
| 68 | + mPty.write(data.toString()); |
| 69 | + }; |
| 70 | + |
| 71 | + const resizeListener = () => { |
| 72 | + const { columns, rows } = process.stdout; |
| 73 | + mPty.resize(columns, rows); |
| 74 | + } |
| 75 | + |
| 76 | + // Listen to resize events for the terminal window; |
| 77 | + process.stdout.on('resize', resizeListener); |
| 78 | + // Listen for user input |
| 79 | + process.stdin.on('data', stdinListener); |
| 80 | + |
| 81 | + mPty.onExit((result) => { |
| 82 | + process.stdout.off('resize', resizeListener); |
| 83 | + process.stdin.off('data', stdinListener); |
| 84 | + |
| 85 | + resolve({ |
| 86 | + status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR, |
| 87 | + exitCode: result.exitCode, |
| 88 | + data: stripAnsi(output.join('\n').trim()), |
| 89 | + }) |
| 90 | + }) |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + async kill(): Promise<{ exitCode: number, signal?: number | undefined }> { |
| 95 | + // No-op here. Each pty instance is stand alone and tied to the parent process. Everything should be killed as expected. |
| 96 | + return { |
| 97 | + exitCode: 0, |
| 98 | + signal: 0, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private getDefaultShell(): string { |
| 103 | + return process.env.SHELL!; |
| 104 | + } |
| 105 | +} |
0 commit comments