-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.ts
More file actions
101 lines (83 loc) · 2.92 KB
/
Copy pathspawn.ts
File metadata and controls
101 lines (83 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { SpawnStatus } from '@codifycli/schemas';
import * as pty from '@homebridge/node-pty-prebuilt-multiarch';
import stripAnsi from 'strip-ansi';
import { Shell, ShellUtils } from './shell.js';
export interface SpawnResult {
status: SpawnStatus;
exitCode: number;
data: string;
}
export interface SpawnOptions {
cwd?: string;
env?: Record<string, unknown>,
interactive?: boolean,
requiresRoot?: boolean,
stdin?: boolean,
throws?: boolean,
}
export function testSpawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
return spawnSafe(cmd, { interactive: true, ...options, });
}
export function spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
if (cmd.toLowerCase().includes('sudo')) {
throw new Error('Command must not include sudo')
}
console.log(`Running command: ${options?.requiresRoot ? 'sudo' : ''} ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
return new Promise((resolve, reject) => {
const output: string[] = [];
const historyIgnore = ShellUtils.getShell() === Shell.ZSH ? { HISTORY_IGNORE: '*' } : { HISTIGNORE: '*' };
// If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
// in the response.
const env = {
...process.env, ...options?.env,
TERM_PROGRAM: 'codify',
COMMAND_MODE: 'unix2003',
COLORTERM: 'truecolor',
...historyIgnore
}
// Initial terminal dimensions
const initialCols = 10_000; // Set to a large value to prevent wrapping
const initialRows = process.stdout.rows ?? 24;
const command = options?.requiresRoot ? `sudo ${cmd}` : cmd;
const args = options?.interactive ? ['-i', '-c', command] : ['-c', command]
// Run the command in a pty for interactivity
const mPty = pty.spawn(ShellUtils.getDefaultShell(), args, {
...options,
cols: initialCols,
rows: initialRows,
env
});
mPty.onData((data) => {
process.stdout.write(data);
output.push(data.toString());
})
const resizeListener = () => {
const { columns, rows } = process.stdout;
mPty.resize(columns, rows);
}
const stdinListener = (data: any) => {
// console.log('stdinListener', data);
mPty.write(data.toString());
}
// Listen to resize events for the terminal window;
process.stdout.on('resize', resizeListener);
if (options?.stdin) {
process.stdin.on('data', stdinListener)
}
mPty.onExit((result) => {
process.stdout.off('resize', resizeListener);
if (options?.stdin) {
process.stdin.off('data', stdinListener);
}
if (options?.throws && result.exitCode !== 0) {
reject(new Error(stripAnsi(output.join('\n').trim())));
return;
}
resolve({
status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
exitCode: result.exitCode,
data: stripAnsi(output.join('\n').trim()),
})
})
})
}