microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
160lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
6f8b71fedigeff10 years ago | 4 | import * as nodeChildProcess from "child_process"; |
ce5e88eeYuri Skorokhodov5 years ago | 5 | import { ErrorHelper } from "../error/errorHelper"; |
| 6 | import { InternalErrorCode } from "../error/internalErrorCode"; | |
3fb37ad5unknown10 years ago | 7 | |
c7f1165cdigeff10 years ago | 8 | // Uncomment the following lines to record all spawned processes executions |
3c172a05Artem Egorov8 years ago | 9 | // import {Recorder} from "../../../test/resources/processExecution/recorder"; |
c7f1165cdigeff10 years ago | 10 | // Recorder.installGlobalRecorder(); |
| 11 | | |
3fb37ad5unknown10 years ago | 12 | export interface IExecResult { |
6f8b71fedigeff10 years ago | 13 | process: nodeChildProcess.ChildProcess; |
ce5e88eeYuri Skorokhodov5 years ago | 14 | outcome: Promise<string>; |
3fb37ad5unknown10 years ago | 15 | } |
| 16 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 17 | export interface ISpawnResult { |
6f8b71fedigeff10 years ago | 18 | spawnedProcess: nodeChildProcess.ChildProcess; |
5b0582f3digeff10 years ago | 19 | stdin: NodeJS.WritableStream; |
| 20 | stdout: NodeJS.ReadableStream; | |
| 21 | stderr: NodeJS.ReadableStream; | |
ce5e88eeYuri Skorokhodov5 years ago | 22 | outcome: Promise<void>; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 23 | } |
| 24 | | |
3fb37ad5unknown10 years ago | 25 | interface IExecOptions { |
| 26 | cwd?: string; | |
| 27 | stdio?: any; | |
| 28 | env?: any; | |
| 29 | encoding?: string; | |
| 30 | timeout?: number; | |
| 31 | maxBuffer?: number; | |
| 32 | killSignal?: string; | |
| 33 | } | |
| 34 | | |
| 35 | interface ISpawnOptions { | |
| 36 | cwd?: string; | |
| 37 | stdio?: any; | |
| 38 | env?: any; | |
| 39 | detached?: boolean; | |
| 40 | } | |
| 41 | | |
| 42 | export class ChildProcess { | |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 43 | public static ERROR_TIMEOUT_MILLISECONDS = 300; |
6f8b71fedigeff10 years ago | 44 | private childProcess: typeof nodeChildProcess; |
aab2095edigeff10 years ago | 45 | |
ce5e88eeYuri Skorokhodov5 years ago | 46 | constructor({ childProcess = nodeChildProcess } = {}) { |
aab2095edigeff10 years ago | 47 | this.childProcess = childProcess; |
| 48 | } | |
| 49 | | |
ce5e88eeYuri Skorokhodov5 years ago | 50 | public exec(command: string, options: IExecOptions = {}): Promise<IExecResult> { |
| 51 | let outcome: Promise<string>; | |
| 52 | let process: nodeChildProcess.ChildProcess; | |
34472878RedMickey5 years ago | 53 | return new Promise<IExecResult>(resolveRes => { |
ce5e88eeYuri Skorokhodov5 years ago | 54 | outcome = new Promise<string>((resolve, reject) => { |
34472878RedMickey5 years ago | 55 | process = this.childProcess.exec( |
| 56 | command, | |
| 57 | options, | |
| 58 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 59 | (error: Error, stdout: string, stderr: string) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 60 | if (error) { |
34472878RedMickey5 years ago | 61 | reject( |
| 62 | ErrorHelper.getNestedError( | |
| 63 | error, | |
| 64 | InternalErrorCode.CommandFailed, | |
| 65 | command, | |
| 66 | ), | |
| 67 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 68 | } else { |
| 69 | resolve(stdout); | |
| 70 | } | |
34472878RedMickey5 years ago | 71 | }, |
| 72 | ); | |
| 73 | }); | |
| 74 | resolveRes({ process: process, outcome: outcome }); | |
3fb37ad5unknown10 years ago | 75 | }); |
| 76 | } | |
| 77 | | |
ce5e88eeYuri Skorokhodov5 years ago | 78 | public execToString(command: string, options: IExecOptions = {}): Promise<string> { |
34472878RedMickey5 years ago | 79 | return this.exec(command, options).then(result => |
| 80 | result.outcome.then(stdout => stdout.toString()), | |
| 81 | ); | |
3fb37ad5unknown10 years ago | 82 | } |
| 83 | | |
34472878RedMickey5 years ago | 84 | public execFileSync( |
| 85 | command: string, | |
| 86 | args: string[] = [], | |
| 87 | options: IExecOptions = {}, | |
| 88 | ): Buffer | string { | |
b3753d4eRedMickey6 years ago | 89 | return this.childProcess.execFileSync(command, args, options); |
| 90 | } | |
| 91 | | |
34472878RedMickey5 years ago | 92 | public spawn( |
| 93 | command: string, | |
| 94 | args: string[] = [], | |
| 95 | options: ISpawnOptions = {}, | |
| 96 | showStdOutputsOnError: boolean = false, | |
| 97 | ): ISpawnResult { | |
9596aa53digeff10 years ago | 98 | const spawnedProcess = this.childProcess.spawn(command, args, options); |
ce5e88eeYuri Skorokhodov5 years ago | 99 | let outcome: Promise<void> = new Promise((resolve, reject) => { |
| 100 | spawnedProcess.once("error", (error: any) => { | |
| 101 | reject(error); | |
| 102 | }); | |
| 103 | | |
dd8375caJiglioNero5 years ago | 104 | const stderrChunks: string[] = []; |
| 105 | const stdoutChunks: string[] = []; | |
| 106 | | |
34472878RedMickey5 years ago | 107 | spawnedProcess.stderr.on("data", data => { |
dd8375caJiglioNero5 years ago | 108 | stderrChunks.push(data.toString()); |
| 109 | }); | |
| 110 | | |
34472878RedMickey5 years ago | 111 | spawnedProcess.stdout.on("data", data => { |
dd8375caJiglioNero5 years ago | 112 | stdoutChunks.push(data.toString()); |
| 113 | }); | |
| 114 | | |
ce5e88eeYuri Skorokhodov5 years ago | 115 | spawnedProcess.once("exit", (code: number) => { |
| 116 | if (code === 0) { | |
| 117 | resolve(); | |
| 118 | } else { | |
| 119 | const commandWithArgs = command + " " + args.join(" "); | |
dd8375caJiglioNero5 years ago | 120 | if (showStdOutputsOnError) { |
| 121 | let details = ""; | |
| 122 | if (stdoutChunks.length > 0) { | |
34472878RedMickey5 years ago | 123 | details = details.concat( |
| 124 | `\n\tSTDOUT: ${stdoutChunks[stdoutChunks.length - 1]}`, | |
| 125 | ); | |
dd8375caJiglioNero5 years ago | 126 | } |
| 127 | if (stderrChunks.length > 0) { | |
| 128 | details = details.concat(`\n\tSTDERR: ${stderrChunks.join("\n\t")}`); | |
| 129 | } | |
| 130 | if (details === "") { | |
| 131 | details = "STDOUT and STDERR are empty!"; | |
| 132 | } | |
34472878RedMickey5 years ago | 133 | reject( |
| 134 | ErrorHelper.getInternalError( | |
| 135 | InternalErrorCode.CommandFailedWithDetails, | |
| 136 | commandWithArgs, | |
| 137 | details, | |
| 138 | ), | |
| 139 | ); | |
| 140 | } else { | |
| 141 | reject( | |
| 142 | ErrorHelper.getInternalError( | |
| 143 | InternalErrorCode.CommandFailed, | |
| 144 | commandWithArgs, | |
| 145 | code, | |
| 146 | ), | |
| 147 | ); | |
dd8375caJiglioNero5 years ago | 148 | } |
ce5e88eeYuri Skorokhodov5 years ago | 149 | } |
| 150 | }); | |
3fb37ad5unknown10 years ago | 151 | }); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 152 | return { |
ce5e88eeYuri Skorokhodov5 years ago | 153 | spawnedProcess: spawnedProcess, |
| 154 | stdin: spawnedProcess.stdin, | |
| 155 | stdout: spawnedProcess.stdout, | |
| 156 | stderr: spawnedProcess.stderr, | |
| 157 | outcome: outcome, | |
34472878RedMickey5 years ago | 158 | }; |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 159 | } |
3fb37ad5unknown10 years ago | 160 | } |