microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
914a7c909092cd9effc56a839d104f0932af80e9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

192lines · 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
10873e11digeff10 years ago4import * as Q from "q";
2e15926eMeena Kunnathur Balakrishnan10 years ago5import {ChildProcess} from "child_process";
3fb37ad5unknown10 years ago6import {Log} from "./log";
b0061ac6Meena Kunnathur Balakrishnan10 years ago7import {Node} from "./node/node";
cb6d0922digeff10 years ago8import {ISpawnResult} from "./node/childProcess";
5e5bf86eMeena Kunnathur Balakrishnan10 years ago9import {OutputChannel} from "vscode";
10873e11digeff10 years ago10import {NestedError} from "./nestedError";
3fb37ad5unknown10 years ago11
12interface EnvironmentOptions {
13REACT_DEBUGGER?: string;
14}
15
16interface Options {
17env?: EnvironmentOptions;
18}
19
20export class CommandExecutor {
21private currentWorkingDirectory: string;
22
110558c0Jimmy Thomson10 years ago23constructor(currentWorkingDirectory?: string) {
3fb37ad5unknown10 years ago24this.currentWorkingDirectory = currentWorkingDirectory;
25}
26
fb2bae06Daniel10 years ago27public execute(command: string, options: Options = {}): Q.Promise<void> {
bedf110funknown10 years ago28Log.commandStarted(command);
29return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago30.then(stdout => {
bedf110funknown10 years ago31Log.logMessage(stdout);
32Log.commandEnded(command);
3fb37ad5unknown10 years ago33},
cb6d0922digeff10 years ago34(reason: Error) =>
35this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago36}
37
45944d15Meena Kunnathur Balakrishnan10 years ago38/**
39* Spawns a child process with the params passed and returns promise of the spawned ChildProcess
40* This method does not wait for the spawned process to finish execution
41* {command} - The command to be invoked in the child process
42* {args} - Arguments to be passed to the command
43* {options} - additional options with which the child process needs to be spawned
44* {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
45*/
5b0582f3digeff10 years ago46public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess {
47return this.spawnChildProcess(command, args, options, outputChannel).spawnedProcess;
45944d15Meena Kunnathur Balakrishnan10 years ago48}
49
50/**
51* Spawns a child process with the params passed
52* This method waits until the spawned process finishes execution
53* {command} - The command to be invoked in the child process
54* {args} - Arguments to be passed to the command
55* {options} - additional options with which the child process needs to be spawned
56* {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
57*/
3d69a9b4digeff10 years ago58public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
5b0582f3digeff10 years ago59return this.spawnChildProcess(command, args, options, outputChannel).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago60}
61
6126d899Meena Kunnathur Balakrishnan10 years ago62/**
63* Spawns the React Native packager in a child process.
64*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago65public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
66let deferred = Q.defer<ChildProcess>();
6126d899Meena Kunnathur Balakrishnan10 years ago67let command = this.getReactCommandName();
68let runArguments = ["start"];
69
70if (args) {
71runArguments = runArguments.concat(args);
72}
73
74let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
75
c3a987a7Meena Kunnathur Balakrishnan10 years ago76let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions);
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago77result.spawnedProcess.once("error", (error: any) => {
cb6d0922digeff10 years ago78deferred.reject(new NestedError(`Error while executing React Native Packager`, error));
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago79});
80
c3a987a7Meena Kunnathur Balakrishnan10 years ago81result.stderr.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago82if (outputChannel) {
83outputChannel.append(data.toString());
84} else {
85process.stderr.write(data);
86}
87});
88
c3a987a7Meena Kunnathur Balakrishnan10 years ago89result.stdout.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago90if (outputChannel) {
91outputChannel.append(data.toString());
92} else {
93process.stdout.write(data);
94}
95});
96
65fd8e85digeff10 years ago97// TODO #83 - PROMISE: We need to consume result.outcome here
10873e11digeff10 years ago98Q.delay(300).done(() => deferred.resolve(result.spawnedProcess));
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago99return deferred.promise;
6126d899Meena Kunnathur Balakrishnan10 years ago100}
101
c3a987a7Meena Kunnathur Balakrishnan10 years ago102/**
103* Kills the React Native packager in a child process.
104*/
10873e11digeff10 years ago105public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): Q.Promise<void> {
c3a987a7Meena Kunnathur Balakrishnan10 years ago106Log.logMessage("Stopping Packager", outputChannel);
107
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago108if (packagerProcess) {
109/* To reliably kill the child process on all versions of Windows,
c3a987a7Meena Kunnathur Balakrishnan10 years ago110* please use taskkill to end the packager process */
111if (process.platform === "win32") {
8ee905e8digeff10 years ago112return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => {
c3a987a7Meena Kunnathur Balakrishnan10 years ago113Log.logMessage("Packager stopped", outputChannel);
114});
115} else {
116packagerProcess.kill();
117Log.logMessage("Packager stopped", outputChannel);
8ee905e8digeff10 years ago118return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago119}
120} else {
121Log.logMessage("Packager not found", outputChannel);
10873e11digeff10 years ago122return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago123}
124}
125
126
127
f8d32439dlebu10 years ago128/**
129* Executes a react native command and waits for its completion.
130*/
3d69a9b4digeff10 years ago131public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
5b0582f3digeff10 years ago132return this.spawnChildReactCommandProcess(command, args, options, outputChannel).outcome;
133}
134
135public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
f8d32439dlebu10 years ago136let runArguments = [command];
137if (args) {
f7208a21Jimmy Thomson10 years ago138runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago139}
5b0582f3digeff10 years ago140return this.spawnChildProcess(this.getReactCommandName(), runArguments, options, outputChannel);
f8d32439dlebu10 years ago141}
142
143/**
144* Resolves the dev machine, desktop platform.
145*/
146private getReactCommandName() {
147let platform = process.platform;
148switch (platform) {
149case "darwin":
150return "react-native";
151case "win32":
152default:
153return "react-native.cmd";
154}
155}
156
5b0582f3digeff10 years ago157private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
08e81a53unknown10 years ago158let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago159let commandWithArgs = command + " " + args.join(" ");
160
3194e9afMeena Kunnathur Balakrishnan10 years ago161Log.commandStarted(commandWithArgs, outputChannel);
c3a987a7Meena Kunnathur Balakrishnan10 years ago162let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions);
3fb37ad5unknown10 years ago163
164result.stderr.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago165if (outputChannel) {
166outputChannel.append(data.toString());
167} else {
168process.stderr.write(data);
169}
3fb37ad5unknown10 years ago170});
171
172result.stdout.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago173if (outputChannel) {
174outputChannel.append(data.toString());
175} else {
176process.stdout.write(data);
177}
3fb37ad5unknown10 years ago178});
179
10873e11digeff10 years ago180result.outcome = result.outcome.then(
181() =>
182Log.commandEnded(commandWithArgs, outputChannel),
183reason =>
3677173cdigeff10 years ago184this.generateRejectionForCommand(command, reason));
5b0582f3digeff10 years ago185
186return result;
3fb37ad5unknown10 years ago187}
10873e11digeff10 years ago188
3677173cdigeff10 years ago189private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
10873e11digeff10 years ago190return Q.reject<void>(new NestedError(`Error while executing: ${command}`, reason));
191}
3fb37ad5unknown10 years ago192}