microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

98lines · 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");
898cb3c6Meena Kunnathur Balakrishnan10 years ago6import {ErrorHelper} from "../error/errorHelper";
7import {InternalErrorCode} from "../error/internalErrorCode";
3fb37ad5unknown10 years ago8
9export interface IExecResult {
10process: child_process.ChildProcess;
11outcome: Q.Promise<Buffer>;
12}
13
45944d15Meena Kunnathur Balakrishnan10 years ago14export interface ISpawnResult {
15spawnedProcess: child_process.ChildProcess;
5b0582f3digeff10 years ago16stdin: NodeJS.WritableStream;
17stdout: NodeJS.ReadableStream;
18stderr: NodeJS.ReadableStream;
3d69a9b4digeff10 years ago19outcome: Q.Promise<void>;
45944d15Meena Kunnathur Balakrishnan10 years ago20}
21
3fb37ad5unknown10 years ago22interface IExecOptions {
23cwd?: string;
24stdio?: any;
25env?: any;
26encoding?: string;
27timeout?: number;
28maxBuffer?: number;
29killSignal?: string;
30}
31
32interface ISpawnOptions {
33cwd?: string;
34stdio?: any;
35env?: any;
36detached?: boolean;
37}
38
39export class ChildProcess {
40public exec(command: string, options: IExecOptions = {}): IExecResult {
41let outcome = Q.defer<Buffer>();
42
43let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
44if (error) {
898cb3c6Meena Kunnathur Balakrishnan10 years ago45outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
3fb37ad5unknown10 years ago46} else {
47outcome.resolve(stdout);
48}
49});
50
51return { process: execProcess, outcome: outcome.promise };
52}
53
54public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
2f567aafdlebu10 years ago55return this.exec(command, options).outcome.then(stdout => stdout.toString());
3fb37ad5unknown10 years ago56}
57
c3a987a7Meena Kunnathur Balakrishnan10 years ago58public spawnWithExitHandler(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
3d69a9b4digeff10 years ago59let outcome = Q.defer<void>();
4f7c7447Meena Kunnathur Balakrishnan10 years ago60let commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago61
62let spawnedProcess = child_process.spawn(command, args, options);
63spawnedProcess.once("error", (error: any) => {
10873e11digeff10 years ago64outcome.reject(error);
3fb37ad5unknown10 years ago65});
6c330a04dlebu10 years ago66spawnedProcess.once("exit", (code: number) => {
3fb37ad5unknown10 years ago67if (code === 0) {
3d69a9b4digeff10 years ago68outcome.resolve(void 0);
6c330a04dlebu10 years ago69} else {
4f7c7447Meena Kunnathur Balakrishnan10 years ago70outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailedWithErrorCode, commandWithArgs, code));
3fb37ad5unknown10 years ago71}
72});
73
b70ce8b3Meena10 years ago74return {
75spawnedProcess: spawnedProcess,
76stdin: spawnedProcess.stdin,
77stdout: spawnedProcess.stdout,
3fb37ad5unknown10 years ago78stderr: spawnedProcess.stderr,
b70ce8b3Meena10 years ago79outcome: outcome.promise };
3fb37ad5unknown10 years ago80}
c3a987a7Meena Kunnathur Balakrishnan10 years ago81
82public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
3d69a9b4digeff10 years ago83let outcome = Q.defer<void>();
4f7c7447Meena Kunnathur Balakrishnan10 years ago84let commandWithArgs = command + " " + args.join(" ");
c3a987a7Meena Kunnathur Balakrishnan10 years ago85let spawnedProcess = child_process.spawn(command, args, options);
86spawnedProcess.once("error", (error: any) => {
4f7c7447Meena Kunnathur Balakrishnan10 years ago87outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, commandWithArgs));
c3a987a7Meena Kunnathur Balakrishnan10 years ago88});
89
90return {
91spawnedProcess: spawnedProcess,
92stdin: spawnedProcess.stdin,
93stdout: spawnedProcess.stdout,
94stderr: spawnedProcess.stderr,
95outcome: outcome.promise
96};
97}
3fb37ad5unknown10 years ago98}