-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.ts
More file actions
129 lines (107 loc) · 3.88 KB
/
spawn.ts
File metadata and controls
129 lines (107 loc) · 3.88 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as pty from '@homebridge/node-pty-prebuilt-multiarch';
import { SpawnStatus } from 'codify-schemas';
import stripAnsi from 'strip-ansi';
import { SpawnError } from '../common/errors.js';
import { ctx } from '../events/context.js';
import { OsUtils } from './os-utils.js';
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,
timeout?: number,
}
export async function spawn(cmd: string, options?: SpawnOptions, pluginName?: string, password?: string): Promise<SpawnResult> {
const spawnResult = await spawnSafe(cmd, options, pluginName, password);
if (spawnResult.status !== 'success') {
throw new SpawnError(Array.isArray(cmd) ? cmd.join('\n') : cmd, spawnResult.exitCode, spawnResult.data);
}
return spawnResult;
}
export async function spawnSafe(cmd: string, options?: SpawnOptions, pluginName?: string, password?: string): Promise<SpawnResult> {
if (options?.requiresRoot && !password) {
throw new Error('Password must be specified!');
}
if (cmd.toLowerCase().includes('sudo')) {
throw new Error(`Command must not include sudo. Plugin (${pluginName})`)
}
if (pluginName) {
ctx.pluginStdout(pluginName, `Running command: ${options?.requiresRoot ? 'sudo' : ''} ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
} else {
ctx.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : '') + '\n');
}
return new Promise((resolve) => {
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 = process.stdout.columns ?? 80;
const initialRows = process.stdout.rows ?? 24;
const command = options?.requiresRoot ? `sudo -k >/dev/null 2>&1; sudo -S <<< "${password}" -E ${ShellUtils.getDefaultShell()} ${options?.interactive ? '-i' : ''} -c "${cmd.replaceAll('"', '\\"')}"` : 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) => {
if (pluginName && !options?.stdin) {
ctx.pluginStdout(pluginName, data)
} else {
ctx.log(data);
}
output.push(data.toString());
})
const resizeListener = () => {
const { columns, rows } = process.stdout;
mPty.resize(columns, rows);
}
const stdinListener = (data: Buffer | string) => {
// 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);
}
resolve({
status: result.exitCode === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
exitCode: result.exitCode,
data: stripAnsi(output.join('\n').trim()),
})
});
if (options?.timeout) {
setTimeout(() => {
mPty.kill();
resolve({
status: SpawnStatus.ERROR,
exitCode: -1,
data: '',
});
}, options.timeout);
}
})
}