(command: string, args: string[], options: cp.SpawnOptions = { shell: true })
| 10 | } |
| 11 | |
| 12 | export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> { |
| 13 | return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => { |
| 14 | let result: string = ""; |
| 15 | |
| 16 | const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() }); |
| 17 | |
| 18 | childProc.stdout?.on("data", (data: string | Buffer) => { |
| 19 | data = data.toString(); |
| 20 | result = result.concat(data); |
| 21 | leetCodeChannel.append(data); |
| 22 | }); |
| 23 | |
| 24 | childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString())); |
| 25 | |
| 26 | childProc.on("error", reject); |
| 27 | |
| 28 | childProc.on("close", (code: number) => { |
| 29 | if (code !== 0 || result.indexOf("ERROR") > -1) { |
| 30 | const error: IExecError = new Error(`Command "${command} ${args.toString()}" failed with exit code "${code}".`); |
| 31 | if (result) { |
| 32 | error.result = result; // leetcode-cli may print useful content by exit with error code |
| 33 | } |
| 34 | reject(error); |
| 35 | } else { |
| 36 | resolve(result); |
| 37 | } |
| 38 | }); |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | export async function executeCommandWithProgress(message: string, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> { |
| 43 | let result: string = ""; |
no test coverage detected