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