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