microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
98lines · 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 { | |
| 40 | public exec(command: string, options: IExecOptions = {}): IExecResult { | |
| 41 | let outcome = Q.defer<Buffer>(); | |
| 42 | | |
| 43 | let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => { | |
| 44 | if (error) { | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 45 | outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command)); |
3fb37ad5unknown10 years ago | 46 | } else { |
| 47 | outcome.resolve(stdout); | |
| 48 | } | |
| 49 | }); | |
| 50 | | |
| 51 | return { process: execProcess, outcome: outcome.promise }; | |
| 52 | } | |
| 53 | | |
| 54 | public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> { | |
2f567aafdlebu10 years ago | 55 | return this.exec(command, options).outcome.then(stdout => stdout.toString()); |
3fb37ad5unknown10 years ago | 56 | } |
| 57 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 58 | public spawnWithExitHandler(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult { |
3d69a9b4digeff10 years ago | 59 | let outcome = Q.defer<void>(); |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 60 | let commandWithArgs = command + " " + args.join(" "); |
3fb37ad5unknown10 years ago | 61 | |
| 62 | let spawnedProcess = child_process.spawn(command, args, options); | |
| 63 | spawnedProcess.once("error", (error: any) => { | |
10873e11digeff10 years ago | 64 | outcome.reject(error); |
3fb37ad5unknown10 years ago | 65 | }); |
6c330a04dlebu10 years ago | 66 | spawnedProcess.once("exit", (code: number) => { |
3fb37ad5unknown10 years ago | 67 | if (code === 0) { |
3d69a9b4digeff10 years ago | 68 | outcome.resolve(void 0); |
6c330a04dlebu10 years ago | 69 | } else { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 70 | outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailedWithErrorCode, commandWithArgs, code)); |
3fb37ad5unknown10 years ago | 71 | } |
| 72 | }); | |
| 73 | | |
b70ce8b3Meena10 years ago | 74 | return { |
| 75 | spawnedProcess: spawnedProcess, | |
| 76 | stdin: spawnedProcess.stdin, | |
| 77 | stdout: spawnedProcess.stdout, | |
3fb37ad5unknown10 years ago | 78 | stderr: spawnedProcess.stderr, |
b70ce8b3Meena10 years ago | 79 | outcome: outcome.promise }; |
3fb37ad5unknown10 years ago | 80 | } |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 81 | |
| 82 | public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult { | |
3d69a9b4digeff10 years ago | 83 | let outcome = Q.defer<void>(); |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 84 | let commandWithArgs = command + " " + args.join(" "); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 85 | let spawnedProcess = child_process.spawn(command, args, options); |
| 86 | spawnedProcess.once("error", (error: any) => { | |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 87 | outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, commandWithArgs)); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 88 | }); |
| 89 | | |
| 90 | return { | |
| 91 | spawnedProcess: spawnedProcess, | |
| 92 | stdin: spawnedProcess.stdin, | |
| 93 | stdout: spawnedProcess.stdout, | |
| 94 | stderr: spawnedProcess.stderr, | |
| 95 | outcome: outcome.promise | |
| 96 | }; | |
| 97 | } | |
3fb37ad5unknown10 years ago | 98 | } |