microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
102lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as child_process from "child_process"; |
| 5 | import Q = require("q"); |
| 6 | import {ErrorHelper} from "../error/errorHelper"; |
| 7 | import {InternalErrorCode} from "../error/internalErrorCode"; |
| 8 | |
| 9 | export interface IExecResult { |
| 10 | process: child_process.ChildProcess; |
| 11 | outcome: Q.Promise<Buffer>; |
| 12 | } |
| 13 | |
| 14 | export interface ISpawnResult { |
| 15 | spawnedProcess: child_process.ChildProcess; |
| 16 | stdin: NodeJS.WritableStream; |
| 17 | stdout: NodeJS.ReadableStream; |
| 18 | stderr: NodeJS.ReadableStream; |
| 19 | outcome: Q.Promise<void>; |
| 20 | } |
| 21 | |
| 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 { |
| 40 | public static ERROR_TIMEOUT_MILLISECONDS = 300; |
| 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) { |
| 46 | outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command)); |
| 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> { |
| 56 | return this.exec(command, options).outcome.then(stdout => stdout.toString()); |
| 57 | } |
| 58 | |
| 59 | public spawnWaitUntilStarted(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult { |
| 60 | let outcome = Q.defer<void>(); |
| 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(" "); |
| 80 | |
| 81 | let spawnedProcess = child_process.spawn(command, args, options); |
| 82 | spawnedProcess.once("error", (error: any) => { |
| 83 | outcome.reject(error); |
| 84 | }); |
| 85 | |
| 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 | }); |
| 93 | |
| 94 | return { |
| 95 | spawnedProcess: spawnedProcess, |
| 96 | stdin: spawnedProcess.stdin, |
| 97 | stdout: spawnedProcess.stdout, |
| 98 | stderr: spawnedProcess.stderr, |
| 99 | outcome: outcome.promise |
| 100 | }; |
| 101 | } |
| 102 | } |
| 103 | |