microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a9925ee8b659b3f9dba8a521555c8ee58fd8f31

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

95lines · modeblame

a31b007cunknown10 years ago1// 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 ago4import * as child_process from "child_process";
5import Q = require("q");
cb6d0922digeff10 years ago6import {NestedError} from "../nestedError";
3fb37ad5unknown10 years ago7
8export interface IExecResult {
9process: child_process.ChildProcess;
10outcome: Q.Promise<Buffer>;
11}
12
45944d15Meena Kunnathur Balakrishnan10 years ago13export interface ISpawnResult {
14spawnedProcess: child_process.ChildProcess;
5b0582f3digeff10 years ago15stdin: NodeJS.WritableStream;
16stdout: NodeJS.ReadableStream;
17stderr: NodeJS.ReadableStream;
3d69a9b4digeff10 years ago18outcome: Q.Promise<void>;
45944d15Meena Kunnathur Balakrishnan10 years ago19}
20
3fb37ad5unknown10 years ago21interface IExecOptions {
22cwd?: string;
23stdio?: any;
24env?: any;
25encoding?: string;
26timeout?: number;
27maxBuffer?: number;
28killSignal?: string;
29}
30
31interface ISpawnOptions {
32cwd?: string;
33stdio?: any;
34env?: any;
35detached?: boolean;
36}
37
38export class ChildProcess {
39public exec(command: string, options: IExecOptions = {}): IExecResult {
40let outcome = Q.defer<Buffer>();
41
42let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
43if (error) {
cb6d0922digeff10 years ago44outcome.reject(new NestedError(`Error while executing command ${command}`, error, stderr));
3fb37ad5unknown10 years ago45} else {
46outcome.resolve(stdout);
47}
48});
49
50return { process: execProcess, outcome: outcome.promise };
51}
52
53public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
54return this.exec(command).outcome.then(stdout => stdout.toString());
55}
56
c3a987a7Meena Kunnathur Balakrishnan10 years ago57public spawnWithExitHandler(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
3d69a9b4digeff10 years ago58let outcome = Q.defer<void>();
3fb37ad5unknown10 years ago59
60let spawnedProcess = child_process.spawn(command, args, options);
61spawnedProcess.once("error", (error: any) => {
10873e11digeff10 years ago62outcome.reject(error);
3fb37ad5unknown10 years ago63});
6c330a04dlebu10 years ago64spawnedProcess.once("exit", (code: number) => {
3fb37ad5unknown10 years ago65if (code === 0) {
3d69a9b4digeff10 years ago66outcome.resolve(void 0);
6c330a04dlebu10 years ago67} else {
8ee905e8digeff10 years ago68outcome.reject(new Error(`Command ${command} failed with error code ${code}`));
3fb37ad5unknown10 years ago69}
70});
71
b70ce8b3Meena10 years ago72return {
73spawnedProcess: spawnedProcess,
74stdin: spawnedProcess.stdin,
75stdout: spawnedProcess.stdout,
3fb37ad5unknown10 years ago76stderr: spawnedProcess.stderr,
b70ce8b3Meena10 years ago77outcome: outcome.promise };
3fb37ad5unknown10 years ago78}
c3a987a7Meena Kunnathur Balakrishnan10 years ago79
80public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
3d69a9b4digeff10 years ago81let outcome = Q.defer<void>();
c3a987a7Meena Kunnathur Balakrishnan10 years ago82let spawnedProcess = child_process.spawn(command, args, options);
83spawnedProcess.once("error", (error: any) => {
cb6d0922digeff10 years ago84outcome.reject(new NestedError(`Error while executing command ${command}`, error));
c3a987a7Meena Kunnathur Balakrishnan10 years ago85});
86
87return {
88spawnedProcess: spawnedProcess,
89stdin: spawnedProcess.stdin,
90stdout: spawnedProcess.stdout,
91stderr: spawnedProcess.stderr,
92outcome: outcome.promise
93};
94}
3fb37ad5unknown10 years ago95}