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