microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
108lines · 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"; |
3fb37ad5unknown10 years ago | 5 | import Q = require("q"); |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 6 | import {ErrorHelper} from "../error/errorHelper"; |
| 7 | import {InternalErrorCode} from "../error/internalErrorCode"; | |
3fb37ad5unknown10 years ago | 8 | |
c7f1165cdigeff10 years ago | 9 | // Uncomment the following lines to record all spawned processes executions |
3c172a05Artem Egorov8 years ago | 10 | // import {Recorder} from "../../../test/resources/processExecution/recorder"; |
c7f1165cdigeff10 years ago | 11 | // Recorder.installGlobalRecorder(); |
| 12 | | |
3fb37ad5unknown10 years ago | 13 | export interface IExecResult { |
6f8b71fedigeff10 years ago | 14 | process: nodeChildProcess.ChildProcess; |
81f88231Patricio Beltran9 years ago | 15 | outcome: Q.Promise<string>; |
3fb37ad5unknown10 years ago | 16 | } |
| 17 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 18 | export interface ISpawnResult { |
6f8b71fedigeff10 years ago | 19 | spawnedProcess: nodeChildProcess.ChildProcess; |
5b0582f3digeff10 years ago | 20 | stdin: NodeJS.WritableStream; |
| 21 | stdout: NodeJS.ReadableStream; | |
| 22 | stderr: NodeJS.ReadableStream; | |
9596aa53digeff10 years ago | 23 | startup: Q.Promise<void>; // The app started succesfully |
| 24 | outcome: Q.Promise<void>; // The app finished succesfully | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 25 | } |
| 26 | | |
3fb37ad5unknown10 years ago | 27 | interface IExecOptions { |
| 28 | cwd?: string; | |
| 29 | stdio?: any; | |
| 30 | env?: any; | |
| 31 | encoding?: string; | |
| 32 | timeout?: number; | |
| 33 | maxBuffer?: number; | |
| 34 | killSignal?: string; | |
| 35 | } | |
| 36 | | |
| 37 | interface ISpawnOptions { | |
| 38 | cwd?: string; | |
| 39 | stdio?: any; | |
| 40 | env?: any; | |
| 41 | detached?: boolean; | |
| 42 | } | |
| 43 | | |
| 44 | export class ChildProcess { | |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 45 | public static ERROR_TIMEOUT_MILLISECONDS = 300; |
6f8b71fedigeff10 years ago | 46 | private childProcess: typeof nodeChildProcess; |
aab2095edigeff10 years ago | 47 | |
6f8b71fedigeff10 years ago | 48 | constructor({childProcess = nodeChildProcess} = {}) { |
aab2095edigeff10 years ago | 49 | this.childProcess = childProcess; |
| 50 | } | |
| 51 | | |
3fb37ad5unknown10 years ago | 52 | public exec(command: string, options: IExecOptions = {}): IExecResult { |
81f88231Patricio Beltran9 years ago | 53 | let outcome = Q.defer<string>(); |
3fb37ad5unknown10 years ago | 54 | |
81f88231Patricio Beltran9 years ago | 55 | let execProcess = this.childProcess.exec(command, options, (error: Error, stdout: string, stderr: string) => { |
3fb37ad5unknown10 years ago | 56 | if (error) { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 57 | outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command)); |
3fb37ad5unknown10 years ago | 58 | } else { |
| 59 | outcome.resolve(stdout); | |
| 60 | } | |
| 61 | }); | |
| 62 | | |
| 63 | return { process: execProcess, outcome: outcome.promise }; | |
| 64 | } | |
| 65 | | |
| 66 | public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> { | |
2f567aafdlebu10 years ago | 67 | return this.exec(command, options).outcome.then(stdout => stdout.toString()); |
3fb37ad5unknown10 years ago | 68 | } |
| 69 | | |
b3753d4eRedMickey6 years ago | 70 | public execFileSync(command: string, args: string[] = [], options: IExecOptions = {}): Buffer | string { |
| 71 | return this.childProcess.execFileSync(command, args, options); | |
| 72 | } | |
| 73 | | |
9596aa53digeff10 years ago | 74 | public spawn(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult { |
| 75 | const startup = Q.defer<void>(); | |
| 76 | const outcome = Q.defer<void>(); | |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 77 | |
9596aa53digeff10 years ago | 78 | const spawnedProcess = this.childProcess.spawn(command, args, options); |
3fb37ad5unknown10 years ago | 79 | |
| 80 | spawnedProcess.once("error", (error: any) => { | |
9596aa53digeff10 years ago | 81 | startup.reject(error); |
10873e11digeff10 years ago | 82 | outcome.reject(error); |
3fb37ad5unknown10 years ago | 83 | }); |
| 84 | | |
9596aa53digeff10 years ago | 85 | Q.delay(ChildProcess.ERROR_TIMEOUT_MILLISECONDS).done(() => |
| 86 | startup.resolve(void 0)); | |
| 87 | | |
93c4b670digeff10 years ago | 88 | startup.promise.done(() => {}, () => {}); // Most callers don't use startup, and Q prints a warning if we don't attach any .done() |
| 89 | | |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 90 | spawnedProcess.once("exit", (code: number) => { |
| 91 | if (code === 0) { | |
| 92 | outcome.resolve(void 0); | |
| 93 | } else { | |
9596aa53digeff10 years ago | 94 | const commandWithArgs = command + " " + args.join(" "); |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 95 | outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code)); |
| 96 | } | |
| 97 | }); | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 98 | |
| 99 | return { | |
| 100 | spawnedProcess: spawnedProcess, | |
| 101 | stdin: spawnedProcess.stdin, | |
| 102 | stdout: spawnedProcess.stdout, | |
| 103 | stderr: spawnedProcess.stderr, | |
9596aa53digeff10 years ago | 104 | startup: startup.promise, |
cdf34447digeff10 years ago | 105 | outcome: outcome.promise, |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 106 | }; |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 107 | } |
3fb37ad5unknown10 years ago | 108 | } |