microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

100lines · 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
6f8b71fedigeff10 years ago4import * as nodeChildProcess from "child_process";
3fb37ad5unknown10 years ago5import Q = require("q");
898cb3c6Meena Kunnathur Balakrishnan10 years ago6import {ErrorHelper} from "../error/errorHelper";
7import {InternalErrorCode} from "../error/internalErrorCode";
3fb37ad5unknown10 years ago8
9export interface IExecResult {
6f8b71fedigeff10 years ago10process: nodeChildProcess.ChildProcess;
3fb37ad5unknown10 years ago11outcome: Q.Promise<Buffer>;
12}
13
45944d15Meena Kunnathur Balakrishnan10 years ago14export interface ISpawnResult {
6f8b71fedigeff10 years ago15spawnedProcess: nodeChildProcess.ChildProcess;
5b0582f3digeff10 years ago16stdin: NodeJS.WritableStream;
17stdout: NodeJS.ReadableStream;
18stderr: NodeJS.ReadableStream;
9596aa53digeff10 years ago19startup: Q.Promise<void>; // The app started succesfully
20outcome: Q.Promise<void>; // The app finished succesfully
45944d15Meena Kunnathur Balakrishnan10 years ago21}
22
3fb37ad5unknown10 years ago23interface IExecOptions {
24cwd?: string;
25stdio?: any;
26env?: any;
27encoding?: string;
28timeout?: number;
29maxBuffer?: number;
30killSignal?: string;
31}
32
33interface ISpawnOptions {
34cwd?: string;
35stdio?: any;
36env?: any;
37detached?: boolean;
38}
39
40export class ChildProcess {
88246fe9Meena Kunnathur Balakrishnan10 years ago41public static ERROR_TIMEOUT_MILLISECONDS = 300;
6f8b71fedigeff10 years ago42private childProcess: typeof nodeChildProcess;
aab2095edigeff10 years ago43
6f8b71fedigeff10 years ago44constructor({childProcess = nodeChildProcess} = {}) {
aab2095edigeff10 years ago45this.childProcess = childProcess;
46}
47
3fb37ad5unknown10 years ago48public exec(command: string, options: IExecOptions = {}): IExecResult {
49let outcome = Q.defer<Buffer>();
50
aab2095edigeff10 years ago51let execProcess = this.childProcess.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
3fb37ad5unknown10 years ago52if (error) {
898cb3c6Meena Kunnathur Balakrishnan10 years ago53outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
3fb37ad5unknown10 years ago54} else {
55outcome.resolve(stdout);
56}
57});
58
59return { process: execProcess, outcome: outcome.promise };
60}
61
62public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
2f567aafdlebu10 years ago63return this.exec(command, options).outcome.then(stdout => stdout.toString());
3fb37ad5unknown10 years ago64}
65
9596aa53digeff10 years ago66public spawn(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult {
67const startup = Q.defer<void>();
68const outcome = Q.defer<void>();
88246fe9Meena Kunnathur Balakrishnan10 years ago69
9596aa53digeff10 years ago70const spawnedProcess = this.childProcess.spawn(command, args, options);
3fb37ad5unknown10 years ago71
72spawnedProcess.once("error", (error: any) => {
9596aa53digeff10 years ago73startup.reject(error);
10873e11digeff10 years ago74outcome.reject(error);
3fb37ad5unknown10 years ago75});
76
9596aa53digeff10 years ago77Q.delay(ChildProcess.ERROR_TIMEOUT_MILLISECONDS).done(() =>
78startup.resolve(void 0));
79
93c4b670digeff10 years ago80startup.promise.done(() => {}, () => {}); // Most callers don't use startup, and Q prints a warning if we don't attach any .done()
81
88246fe9Meena Kunnathur Balakrishnan10 years ago82spawnedProcess.once("exit", (code: number) => {
83if (code === 0) {
84outcome.resolve(void 0);
85} else {
9596aa53digeff10 years ago86const commandWithArgs = command + " " + args.join(" ");
88246fe9Meena Kunnathur Balakrishnan10 years ago87outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code));
88}
89});
c3a987a7Meena Kunnathur Balakrishnan10 years ago90
91return {
92spawnedProcess: spawnedProcess,
93stdin: spawnedProcess.stdin,
94stdout: spawnedProcess.stdout,
95stderr: spawnedProcess.stderr,
9596aa53digeff10 years ago96startup: startup.promise,
cdf34447digeff10 years ago97outcome: outcome.promise,
88246fe9Meena Kunnathur Balakrishnan10 years ago98};
c3a987a7Meena Kunnathur Balakrishnan10 years ago99}
3fb37ad5unknown10 years ago100}