microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
108lines · 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 nodeChildProcess 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: nodeChildProcess.ChildProcess; |
| 11 | outcome: Q.Promise<Buffer>; |
| 12 | } |
| 13 | |
| 14 | export interface ISpawnResult { |
| 15 | spawnedProcess: nodeChildProcess.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 | private childProcess: typeof nodeChildProcess; |
| 42 | |
| 43 | constructor({childProcess = nodeChildProcess} = {}) { |
| 44 | this.childProcess = childProcess; |
| 45 | } |
| 46 | |
| 47 | public exec(command: string, options: IExecOptions = {}): IExecResult { |
| 48 | let outcome = Q.defer<Buffer>(); |
| 49 | |
| 50 | let execProcess = this.childProcess.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => { |
| 51 | if (error) { |
| 52 | outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command)); |
| 53 | } else { |
| 54 | outcome.resolve(stdout); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | return { process: execProcess, outcome: outcome.promise }; |
| 59 | } |
| 60 | |
| 61 | public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> { |
| 62 | return this.exec(command, options).outcome.then(stdout => stdout.toString()); |
| 63 | } |
| 64 | |
| 65 | public spawnWaitUntilStarted(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult { |
| 66 | let outcome = Q.defer<void>(); |
| 67 | let spawnedProcess = this.childProcess.spawn(command, args, options); |
| 68 | spawnedProcess.once("error", (error: any) => { |
| 69 | outcome.reject(error); |
| 70 | }); |
| 71 | |
| 72 | Q.delay(ChildProcess.ERROR_TIMEOUT_MILLISECONDS).done(() => outcome.resolve(void 0)); |
| 73 | |
| 74 | return { |
| 75 | spawnedProcess: spawnedProcess, |
| 76 | stdin: spawnedProcess.stdin, |
| 77 | stdout: spawnedProcess.stdout, |
| 78 | stderr: spawnedProcess.stderr, |
| 79 | outcome: outcome.promise, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | public spawnWaitUntilFinished(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult { |
| 84 | let outcome = Q.defer<void>(); |
| 85 | let commandWithArgs = command + " " + args.join(" "); |
| 86 | |
| 87 | let spawnedProcess = this.childProcess.spawn(command, args, options); |
| 88 | spawnedProcess.once("error", (error: any) => { |
| 89 | outcome.reject(error); |
| 90 | }); |
| 91 | |
| 92 | spawnedProcess.once("exit", (code: number) => { |
| 93 | if (code === 0) { |
| 94 | outcome.resolve(void 0); |
| 95 | } else { |
| 96 | outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code)); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | return { |
| 101 | spawnedProcess: spawnedProcess, |
| 102 | stdin: spawnedProcess.stdin, |
| 103 | stdout: spawnedProcess.stdout, |
| 104 | stderr: spawnedProcess.stderr, |
| 105 | outcome: outcome.promise, |
| 106 | }; |
| 107 | } |
| 108 | } |
| 109 | |