(options: {
ptyExec: PtyExecFunction;
cmd: string;
args?: string[];
cwd?: string;
env?: NodeJS.ProcessEnv;
output: Log;
resolveOn?: RegExp;
onDidInput?: Event<string>;
stdin?: string;
print?: 'off' | 'continuous' | 'end';
})
| 180 | } |
| 181 | |
| 182 | export async function runCommand(options: { |
| 183 | ptyExec: PtyExecFunction; |
| 184 | cmd: string; |
| 185 | args?: string[]; |
| 186 | cwd?: string; |
| 187 | env?: NodeJS.ProcessEnv; |
| 188 | output: Log; |
| 189 | resolveOn?: RegExp; |
| 190 | onDidInput?: Event<string>; |
| 191 | stdin?: string; |
| 192 | print?: 'off' | 'continuous' | 'end'; |
| 193 | }) { |
| 194 | const { ptyExec, cmd, args, cwd, env, output, resolveOn, onDidInput, stdin } = options; |
| 195 | const print = options.print || 'continuous'; |
| 196 | |
| 197 | const p = await ptyExec({ |
| 198 | cmd, |
| 199 | args, |
| 200 | cwd, |
| 201 | env, |
| 202 | output: output, |
| 203 | }); |
| 204 | |
| 205 | return new Promise<{ cmdOutput: string }>((resolve, reject) => { |
| 206 | let cmdOutput = ''; |
| 207 | |
| 208 | const subs: Disposable[] = []; |
| 209 | if (p.write) { |
| 210 | if (stdin) { |
| 211 | p.write(stdin); |
| 212 | } |
| 213 | if (onDidInput) { |
| 214 | subs.push(onDidInput(data => p.write!(data))); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | p.onData(chunk => { |
| 219 | cmdOutput += chunk; |
| 220 | if (print === 'continuous') { |
| 221 | output.raw(chunk); |
| 222 | } |
| 223 | if (resolveOn && resolveOn.exec(cmdOutput)) { |
| 224 | resolve({ cmdOutput }); |
| 225 | } |
| 226 | }); |
| 227 | p.exit.then(({ code, signal }) => { |
| 228 | try { |
| 229 | if (print === 'end') { |
| 230 | output.raw(cmdOutput); |
| 231 | } |
| 232 | subs.forEach(sub => sub?.dispose()); |
| 233 | if (code || signal) { |
| 234 | reject({ |
| 235 | message: `Command failed: ${cmd} ${(args || []).join(' ')}`, |
| 236 | cmdOutput, |
| 237 | code, |
| 238 | signal, |
| 239 | }); |
no test coverage detected