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