-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspawn.ts
More file actions
158 lines (135 loc) · 5.48 KB
/
Copy pathspawn.ts
File metadata and controls
158 lines (135 loc) · 5.48 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as pty from '@homebridge/node-pty-prebuilt-multiarch';
import { SpawnStatus } from '@codifycli/schemas';
import { unlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
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';
import { VerbosityLevel } from './verbosity-level.js';
export interface SpawnResult {
status: SpawnStatus;
exitCode: number;
data: string;
}
export interface SpawnOptions {
cwd?: string;
env?: Record<string, unknown>,
interactive?: boolean,
requiresRoot?: boolean,
requiresSudoAskpass?: boolean,
stdin?: boolean,
timeout?: number,
allowSudoInCommand?: boolean,
}
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 || options?.requiresSudoAskpass) && !password) {
throw new Error('Password must be specified!');
}
if (cmd.toLowerCase().includes('sudo') && !options?.allowSudoInCommand) {
throw new Error(`Command must not include sudo. Plugin (${pluginName})`)
}
if (pluginName) {
ctx.pluginStdout(pluginName, `Running command: ${options?.requiresRoot ? 'sudo' : options?.requiresSudoAskpass ? 'sudo (askpass)' : ''} ${cmd}` + (options?.cwd ? `(${options?.cwd})` : '') + '\n')
} else {
ctx.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : '') + '\n');
}
let tmpFile: string | undefined;
if (options?.requiresSudoAskpass && password) {
tmpFile = join(tmpdir(), `codify-askpass-${Date.now()}.sh`);
const escapedPassword = password.replace(/'/g, "'\\''");
writeFileSync(tmpFile, `#!/bin/bash\necho '${escapedPassword}'`, { mode: 0o700 });
}
try {
return await new Promise<SpawnResult>((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,
...(tmpFile ? { SUDO_ASKPASS: tmpFile } : {}),
TERM_PROGRAM: 'codify',
COMMAND_MODE: 'unix2003',
COLORTERM: 'truecolor',
...historyIgnore
}
// Initial terminal dimensions
const initialCols = process.stdout.columns ?? 80;
const initialRows = process.stdout.rows ?? 24;
// zsh autocorrect prompts (e.g. "zsh: correct 'config' to '.config' [nyae]?") will hang
// the pty waiting for interactive input. Can't be disabled via env var; must unset the options explicitly.
const disableAutocorrect = ShellUtils.getShell() === Shell.ZSH ? 'unsetopt CORRECT CORRECT_ALL 2>/dev/null; ' : '';
const command = options?.requiresRoot
? `sudo -k >/dev/null 2>&1; sudo -S <<< "${password}" -E ${ShellUtils.getDefaultShell()} ${options?.interactive ? '-i' : ''} -c "${(disableAutocorrect + cmd).replaceAll('"', '\\"')}"`
: `${disableAutocorrect}${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 (options?.stdin) {
// Write directly — ctx.log appends '\n' to every chunk which breaks
// in-place spinner/cursor animations that rely on \r without \n.
process.stdout.write(data);
} else if (pluginName) {
ctx.pluginStdout(pluginName, data)
} else if (VerbosityLevel.get() > 0) {
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('binary'));
}
// 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);
}
});
} finally {
if (tmpFile) {
try { unlinkSync(tmpFile); } catch { /* best effort */ }
}
}
}