microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1174ee3dd094e08cacea7cb751e143257d5641fe

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

104lines · 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
c7f1165cdigeff10 years ago9// Uncomment the following lines to record all spawned processes executions
3c172a05Artem Egorov8 years ago10// import {Recorder} from "../../../test/resources/processExecution/recorder";
c7f1165cdigeff10 years ago11// Recorder.installGlobalRecorder();
12
3fb37ad5unknown10 years ago13export interface IExecResult {
6f8b71fedigeff10 years ago14process: nodeChildProcess.ChildProcess;
81f88231Patricio Beltran9 years ago15outcome: Q.Promise<string>;
3fb37ad5unknown10 years ago16}
17
45944d15Meena Kunnathur Balakrishnan10 years ago18export interface ISpawnResult {
6f8b71fedigeff10 years ago19spawnedProcess: nodeChildProcess.ChildProcess;
5b0582f3digeff10 years ago20stdin: NodeJS.WritableStream;
21stdout: NodeJS.ReadableStream;
22stderr: NodeJS.ReadableStream;
9596aa53digeff10 years ago23startup: Q.Promise<void>; // The app started succesfully
24outcome: Q.Promise<void>; // The app finished succesfully
45944d15Meena Kunnathur Balakrishnan10 years ago25}
26
3fb37ad5unknown10 years ago27interface IExecOptions {
28cwd?: string;
29stdio?: any;
30env?: any;
31encoding?: string;
32timeout?: number;
33maxBuffer?: number;
34killSignal?: string;
35}
36
37interface ISpawnOptions {
38cwd?: string;
39stdio?: any;
40env?: any;
41detached?: boolean;
42}
43
44export class ChildProcess {
88246fe9Meena Kunnathur Balakrishnan10 years ago45public static ERROR_TIMEOUT_MILLISECONDS = 300;
6f8b71fedigeff10 years ago46private childProcess: typeof nodeChildProcess;
aab2095edigeff10 years ago47
6f8b71fedigeff10 years ago48constructor({childProcess = nodeChildProcess} = {}) {
aab2095edigeff10 years ago49this.childProcess = childProcess;
50}
51
3fb37ad5unknown10 years ago52public exec(command: string, options: IExecOptions = {}): IExecResult {
81f88231Patricio Beltran9 years ago53let outcome = Q.defer<string>();
3fb37ad5unknown10 years ago54
81f88231Patricio Beltran9 years ago55let execProcess = this.childProcess.exec(command, options, (error: Error, stdout: string, stderr: string) => {
3fb37ad5unknown10 years ago56if (error) {
898cb3c6Meena Kunnathur Balakrishnan10 years ago57outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
3fb37ad5unknown10 years ago58} else {
59outcome.resolve(stdout);
60}
61});
62
63return { process: execProcess, outcome: outcome.promise };
64}
65
66public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
2f567aafdlebu10 years ago67return this.exec(command, options).outcome.then(stdout => stdout.toString());
3fb37ad5unknown10 years ago68}
69
9596aa53digeff10 years ago70public spawn(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult {
71const startup = Q.defer<void>();
72const outcome = Q.defer<void>();
88246fe9Meena Kunnathur Balakrishnan10 years ago73
9596aa53digeff10 years ago74const spawnedProcess = this.childProcess.spawn(command, args, options);
3fb37ad5unknown10 years ago75
76spawnedProcess.once("error", (error: any) => {
9596aa53digeff10 years ago77startup.reject(error);
10873e11digeff10 years ago78outcome.reject(error);
3fb37ad5unknown10 years ago79});
80
9596aa53digeff10 years ago81Q.delay(ChildProcess.ERROR_TIMEOUT_MILLISECONDS).done(() =>
82startup.resolve(void 0));
83
93c4b670digeff10 years ago84startup.promise.done(() => {}, () => {}); // Most callers don't use startup, and Q prints a warning if we don't attach any .done()
85
88246fe9Meena Kunnathur Balakrishnan10 years ago86spawnedProcess.once("exit", (code: number) => {
87if (code === 0) {
88outcome.resolve(void 0);
89} else {
9596aa53digeff10 years ago90const commandWithArgs = command + " " + args.join(" ");
88246fe9Meena Kunnathur Balakrishnan10 years ago91outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code));
92}
93});
c3a987a7Meena Kunnathur Balakrishnan10 years ago94
95return {
96spawnedProcess: spawnedProcess,
97stdin: spawnedProcess.stdin,
98stdout: spawnedProcess.stdout,
99stderr: spawnedProcess.stderr,
9596aa53digeff10 years ago100startup: startup.promise,
cdf34447digeff10 years ago101outcome: outcome.promise,
88246fe9Meena Kunnathur Balakrishnan10 years ago102};
c3a987a7Meena Kunnathur Balakrishnan10 years ago103}
3fb37ad5unknown10 years ago104}