microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a43166549798dd756fe6ed5e49d994ad83e6a3e5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/reactNativeCommandExecutor.ts

94lines · 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 {PlatformResolver} from "./../debugger/platformResolver";
7import {Packager} from "./../debugger/packager";
3d97c2a3Meena Kunnathur Balakrishnan10 years ago8import {ReactNativeProjectHelper} from "./reactNativeProjectHelper";
3194e9afMeena Kunnathur Balakrishnan10 years ago9import * as vscode from "vscode";
bef522ffMeena Kunnathur Balakrishnan10 years ago10
11export class ReactNativeCommandExecutor {
12private reactNativePackager: Packager;
13private workspaceRoot: string;
14
15constructor(workspaceRoot: string) {
fb8f49fbMeena Kunnathur Balakrishnan10 years ago16let resolver = new PlatformResolver();
17let desktopPlatform = resolver.resolveDesktopPlatform();
bef522ffMeena Kunnathur Balakrishnan10 years ago18this.workspaceRoot = workspaceRoot;
fb8f49fbMeena Kunnathur Balakrishnan10 years ago19this.reactNativePackager = new Packager(workspaceRoot, desktopPlatform);
bef522ffMeena Kunnathur Balakrishnan10 years ago20}
21
0904a0d7Meena Kunnathur Balakrishnan10 years ago22/**
3194e9afMeena Kunnathur Balakrishnan10 years ago23* Ensures that we are in a React Native project and then executes the operation
24* Otherwise, displays an error message banner
25* {operation} - a function that performs the expected operation
0904a0d7Meena Kunnathur Balakrishnan10 years ago26*/
3194e9afMeena Kunnathur Balakrishnan10 years ago27public executeCommandInContext(operation: () => void): void {
3d97c2a3Meena Kunnathur Balakrishnan10 years ago28let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
29reactNativeProjectHelper.isReactNativeProject().done(isRNProject => {
3194e9afMeena Kunnathur Balakrishnan10 years ago30if (isRNProject) {
31operation();
32} else {
33vscode.window.showErrorMessage("Current workspace is not a React Native project.");
34}
35});
bef522ffMeena Kunnathur Balakrishnan10 years ago36}
37
0904a0d7Meena Kunnathur Balakrishnan10 years ago38/**
39* Starts the React Native packager
40*/
bef522ffMeena Kunnathur Balakrishnan10 years ago41public startPackager(): void {
ec6e115dMeena Kunnathur Balakrishnan10 years ago42this.reactNativePackager.start(vscode.window.createOutputChannel("React-Native"))
3194e9afMeena Kunnathur Balakrishnan10 years ago43.done();
bef522ffMeena Kunnathur Balakrishnan10 years ago44}
45
0904a0d7Meena Kunnathur Balakrishnan10 years ago46/**
47* Kills the React Native packager invoked by the extension's packager
48*/
bef522ffMeena Kunnathur Balakrishnan10 years ago49public stopPackager(): void {
3194e9afMeena Kunnathur Balakrishnan10 years ago50this.reactNativePackager.stop(vscode.window.createOutputChannel("React-Native"));
51}
52
53/**
54* Executes the 'react-native run-android' command
55*/
56public runAndroid(): void {
3cf70711Meena Kunnathur Balakrishnan10 years ago57this.executeReactNativeRunCommand("run-android");
3194e9afMeena Kunnathur Balakrishnan10 years ago58}
59
60/**
61* Executes the 'react-native run-ios' command
62*/
63public runIos(): void {
3cf70711Meena Kunnathur Balakrishnan10 years ago64this.executeReactNativeRunCommand("run-ios");
3194e9afMeena Kunnathur Balakrishnan10 years ago65}
66
fb8f49fbMeena Kunnathur Balakrishnan10 years ago67/**
3cf70711Meena Kunnathur Balakrishnan10 years ago68* Executes a react-native command passed after starting the packager
fb8f49fbMeena Kunnathur Balakrishnan10 years ago69* {command} The command to be executed
70* {args} The arguments to be passed to the command
71*/
3cf70711Meena Kunnathur Balakrishnan10 years ago72public executeReactNativeRunCommand(command: string, args?: string[]): Q.Promise<void> {
3194e9afMeena Kunnathur Balakrishnan10 years ago73let resolver = new PlatformResolver();
74let desktopPlatform = resolver.resolveDesktopPlatform();
75
76// Invoke "react-native" with the command passed
fb8f49fbMeena Kunnathur Balakrishnan10 years ago77let runArguments = [command];
78
79if (args) {
80runArguments.concat(args);
81}
82
ec6e115dMeena Kunnathur Balakrishnan10 years ago83// Start the packager before executing the React-Native command
84let outputChannel = vscode.window.createOutputChannel("React-Native");
85Log.appendStringToOutputChannel("Attempting to start the React Native packager", outputChannel);
86
87return this.reactNativePackager.start(outputChannel)
fb8f49fbMeena Kunnathur Balakrishnan10 years ago88.then(() => {
ed395f8dMeena Kunnathur Balakrishnan10 years ago89return new CommandExecutor(this.workspaceRoot).spawn(desktopPlatform.reactNativeCommandName, runArguments, {}, vscode.window.createOutputChannel("React-Native"));
ec6e115dMeena Kunnathur Balakrishnan10 years ago90}).then(() => {
fb8f49fbMeena Kunnathur Balakrishnan10 years ago91return Q.resolve<void>(void 0);
92});
3194e9afMeena Kunnathur Balakrishnan10 years ago93}
bef522ffMeena Kunnathur Balakrishnan10 years ago94}