-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodify-spawn.ts
More file actions
193 lines (161 loc) · 5.21 KB
/
Copy pathcodify-spawn.ts
File metadata and controls
193 lines (161 loc) · 5.21 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Ajv } from 'ajv';
import { SudoError, VerbosityLevel, Utils } from '@codifycli/plugin-core';
import {
CommandRequestResponseData, CommandRequestResponseDataSchema,
IpcMessageV2,
MessageCmd,
} from '@codifycli/schemas';
import { nanoid } from 'nanoid';
import { SpawnOptions, spawn } from 'node:child_process';
import stripAnsi from 'strip-ansi';
const ajv = new Ajv({
strict: true,
});
const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema);
export enum SpawnStatus {
SUCCESS = 'success',
ERROR = 'error',
}
export interface SpawnResult {
status: SpawnStatus,
data: string;
}
type CodifySpawnOptions = {
cwd?: string;
throws?: boolean,
requiresRoot?: boolean
requestsTTY?: boolean,
} & Omit<SpawnOptions, 'detached' | 'shell' | 'stdio'>
/**
*
* @param cmd Command to run. Ex: `rm -rf`
* @param opts Standard options for node spawn. Additional argument:
* throws determines if a shell will throw a JS error. Defaults to true
*
* @see promiseSpawn
* @see spawn
*
* @returns SpawnResult { status: SUCCESS | ERROR; data: string }
*/
export async function codifySpawn(
cmd: string,
opts?: CodifySpawnOptions,
): Promise<SpawnResult> {
const throws = opts?.throws ?? true;
console.log(`Running command: ${cmd}` + (opts?.cwd ? `(${opts?.cwd})` : ''))
try {
// TODO: Need to benchmark the effects of using sh vs zsh for shell.
// Seems like zsh shells run slower
const result = await (opts?.requiresRoot
? externalSpawnWithSudo(
cmd,
opts,
)
: internalSpawn(
cmd,
opts ?? {},
));
if (result.status !== SpawnStatus.SUCCESS) {
throw new Error(result.data);
}
return result;
} catch (error) {
if (isDebug()) {
console.error(`CodifySpawn error for command ${cmd}`, error);
}
// @ts-ignore
if (error.message?.startsWith('sudo:')) {
throw new SudoError(cmd);
}
if (throws) {
throw error;
}
if (error instanceof Error) {
return {
status: SpawnStatus.ERROR,
data: error.message,
}
}
return {
status: SpawnStatus.ERROR,
data: String(error),
}
}
}
async function internalSpawn(
cmd: string,
opts: CodifySpawnOptions
): Promise<{ status: SpawnStatus, data: string }> {
return new Promise((resolve) => {
const output: string[] = [];
// If TERM_PROGRAM=Apple_Terminal is set then ANSI escape characters may be included
// in the response.
const env = { ...process.env, ...opts.env, TERM_PROGRAM: 'codify', COMMAND_MODE: 'unix2003', COLORTERM: 'truecolor' }
// Detect shell and appropriate rc file
const shell = process.env.SHELL || '/bin/bash';
const shellName = shell.includes('zsh') ? 'zsh' : 'bash';
const shellRc = Utils.getPrimaryShellRc();
// Source start up shells to emulate a users environment vs. a non-interactive non-login shell script
// Ignore all stdin
// If tty is requested then we'll need to sleep 1 to avoid race conditions. This is because if the terminal updates async after the tty message is
// displayed then it'll disappear. By adding sleep 1 it'll allow ink.js to finish all the updates before the tty message is shown
const _process = spawn(`source ${shellRc}; ${ opts.requestsTTY ? 'sleep 1;' : '' }${cmd}`, [], {
...opts,
stdio: ['ignore', 'pipe', 'pipe'],
shell: shellName,
env
});
const { stdout, stderr, stdin } = _process
stdout.setEncoding('utf8');
stderr.setEncoding('utf8');
stdout.on('data', (data) => {
output.push(data.toString());
})
stderr.on('data', (data) => {
output.push(data.toString());
})
_process.on('error', (data) => {})
// please node that this is not a full replacement for 'inherit'
// the child process can and will detect if stdout is a pty and change output based on it
// the terminal context is lost & ansi information (coloring) etc will be lost
if (stdout && stderr && VerbosityLevel.get() > 0) {
stdout.pipe(process.stdout)
stderr.pipe(process.stderr)
}
_process.on('close', (code) => {
resolve({
status: code === 0 ? SpawnStatus.SUCCESS : SpawnStatus.ERROR,
data: stripAnsi(output.join('\n')),
})
})
})
}
async function externalSpawnWithSudo(
cmd: string,
opts: CodifySpawnOptions
): Promise<{ status: SpawnStatus, data: string }> {
return new Promise((resolve) => {
const requestId = nanoid(8);
const listener = (data: IpcMessageV2)=> {
if (data.requestId === requestId) {
process.removeListener('message', listener);
if (!validateSudoRequestResponse(data.data)) {
throw new Error(`Invalid response for sudo request: ${JSON.stringify(validateSudoRequestResponse.errors, null, 2)}`);
}
resolve(data.data as unknown as CommandRequestResponseData);
}
}
process.on('message', listener);
process.send!(<IpcMessageV2>{
cmd: MessageCmd.COMMAND_REQUEST,
data: {
command: cmd,
options: opts ?? {},
},
requestId
})
});
}
export function isDebug(): boolean {
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
}