microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b75bb2a549d9ddc9993613ac315ea74032d08a61

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/reactNativeCommandExecutor.ts

80lines · modeblame

bef522ffMeena Kunnathur Balakrishnan10 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
4import {CommandExecutor} from "./commands/commandExecutor";
ec6e115dMeena Kunnathur Balakrishnan10 years ago5import {Log} from "./commands/log";
bef522ffMeena Kunnathur Balakrishnan10 years ago6import {Packager} from "./../debugger/packager";
3d97c2a3Meena Kunnathur Balakrishnan10 years ago7import {ReactNativeProjectHelper} from "./reactNativeProjectHelper";
3194e9afMeena Kunnathur Balakrishnan10 years ago8import * as vscode from "vscode";
bef522ffMeena Kunnathur Balakrishnan10 years ago9
10export class ReactNativeCommandExecutor {
11private reactNativePackager: Packager;
12private workspaceRoot: string;
13
14constructor(workspaceRoot: string) {
15this.workspaceRoot = workspaceRoot;
f8d32439dlebu10 years ago16this.reactNativePackager = new Packager(workspaceRoot);
bef522ffMeena Kunnathur Balakrishnan10 years ago17}
18
0904a0d7Meena Kunnathur Balakrishnan10 years ago19/**
20* Starts the React Native packager
21*/
bef522ffMeena Kunnathur Balakrishnan10 years ago22public startPackager(): void {
b3a793eeNisheet Jain10 years ago23this.executeCommandInContext(() => this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native")).done());
bef522ffMeena Kunnathur Balakrishnan10 years ago24}
25
0904a0d7Meena Kunnathur Balakrishnan10 years ago26/**
27* Kills the React Native packager invoked by the extension's packager
28*/
bef522ffMeena Kunnathur Balakrishnan10 years ago29public stopPackager(): void {
b3a793eeNisheet Jain10 years ago30this.executeCommandInContext(() => this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native")));
3194e9afMeena Kunnathur Balakrishnan10 years ago31}
32
33/**
34* Executes the 'react-native run-android' command
35*/
36public runAndroid(): void {
b3a793eeNisheet Jain10 years ago37this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-android"));
3194e9afMeena Kunnathur Balakrishnan10 years ago38}
39
40/**
41* Executes the 'react-native run-ios' command
42*/
43public runIos(): void {
b3a793eeNisheet Jain10 years ago44this.executeCommandInContext(() => this.executeReactNativeRunCommand("run-ios"));
3194e9afMeena Kunnathur Balakrishnan10 years ago45}
46
fb8f49fbMeena Kunnathur Balakrishnan10 years ago47/**
3cf70711Meena Kunnathur Balakrishnan10 years ago48* Executes a react-native command passed after starting the packager
fb8f49fbMeena Kunnathur Balakrishnan10 years ago49* {command} The command to be executed
50* {args} The arguments to be passed to the command
51*/
b3a793eeNisheet Jain10 years ago52private executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
ec6e115dMeena Kunnathur Balakrishnan10 years ago53// Start the packager before executing the React-Native command
54let outputChannel = vscode.window.createOutputChannel("React-Native");
55Log.appendStringToOutputChannel("Attempting to start the React Native packager", outputChannel);
56
57return this.reactNativePackager.start(outputChannel)
f8d32439dlebu10 years ago58.then(() => {
59return new CommandExecutor(this.workspaceRoot).spawnReactCommand(command, args, undefined, outputChannel);
60}).then(() => {
61return Q.resolve<void>(void 0);
62});
3194e9afMeena Kunnathur Balakrishnan10 years ago63}
b3a793eeNisheet Jain10 years ago64
65/**
66* Ensures that we are in a React Native project and then executes the operation
67* Otherwise, displays an error message banner
68* {operation} - a function that performs the expected operation
69*/
70private executeCommandInContext(operation: () => void): void {
71let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
72reactNativeProjectHelper.isReactNativeProject().done(isRNProject => {
73if (isRNProject) {
74operation();
75} else {
76vscode.window.showErrorMessage("Current workspace is not a React Native project.");
77}
78});
79}
bef522ffMeena Kunnathur Balakrishnan10 years ago80}