microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a9925ee8b659b3f9dba8a521555c8ee58fd8f31

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

133lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import {FileSystem} from "../common/node/fileSystem";
5import * as path from "path";
6import * as vscode from "vscode";
7import {CommandPaletteHandler} from "./commandPaletteHandler";
8import {Packager} from "../common/packager";
9import {EntryPointHandler} from "../common/entryPointHandler";
10import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
11import {ReactDirManager} from "./reactDirManager";
12import {IntellisenseHelper} from "./intellisenseHelper";
13import {TelemetryHelper} from "../common/telemetryHelper";
14import {ExtensionServer} from "./extensionServer";
15
16/* all components use the same packager instance */
17const globalPackager = new Packager(vscode.workspace.rootPath);
18const commandPaletteHandler = new CommandPaletteHandler(vscode.workspace.rootPath, globalPackager);
19const extensionServer = new ExtensionServer(globalPackager);
20
21const outputChannel = vscode.window.createOutputChannel("React-Native");
22const entryPointHandler = new EntryPointHandler(outputChannel);
23const reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
24const fsUtil = new FileSystem();
25
26export function activate(context: vscode.ExtensionContext): void {
27 entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version, "Failed to activate the React Native Tools extension", () => {
28 return reactNativeProjectHelper.isReactNativeProject()
29 .then(isRNProject => {
30 if (isRNProject) {
31 warnWhenReactNativeVersionIsNotSupported();
32 entryPointHandler.runFunction("debugger.setupLauncherStub", "Failed to setup the stub launcher for the debugger", () =>
33 setupReactNativeDebugger()
34 .then(() => setupReactDir(context))
35 .then(() => setupExtensionServer(context)));
36 entryPointHandler.runFunction("intelliSense.setup", "Failed to setup IntelliSense", () =>
37 IntellisenseHelper.setupReactNativeIntellisense());
38 }
39 entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation", "Failed to configure the node debugger location for the debugger", () =>
40 configureNodeDebuggerLocation());
41 registerReactNativeCommands(context);
42 });
43 });
44}
45
46export function deactivate(): void {
47 // Kill any packager processes that we spawned
48 entryPointHandler.runFunction("extension.deactivate", "Failed to stop the packager while closing React Native Tools",
49 () => {
50 commandPaletteHandler.stopPackager();
51 }, /*errorsAreFatal*/ true);
52}
53
54function configureNodeDebuggerLocation(): Q.Promise<void> {
55 const nodeDebugPath = vscode.extensions.getExtension("andreweinand.node-debug").extensionPath;
56 return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath }));
57}
58
59function setupReactDir(context: vscode.ExtensionContext): Q.Promise<void> {
60 const reactDirManager = new ReactDirManager();
61 return reactDirManager.create()
62 .then(() => {
63 context.subscriptions.push(reactDirManager);
64 });
65}
66
67function setupExtensionServer(context: vscode.ExtensionContext): Q.Promise<void> {
68 return extensionServer.setup()
69 .then(() => {
70 context.subscriptions.push(extensionServer);
71 });
72}
73
74function warnWhenReactNativeVersionIsNotSupported(): void {
75 return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => {
76 TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason });
77 const shortMessage = `React Native Tools only supports React Native versions 0.19.0 and later`;
78 const longMessage = `${shortMessage}: ${reason}`;
79 vscode.window.showWarningMessage(shortMessage);
80 outputChannel.appendLine(longMessage);
81 outputChannel.show();
82 });
83}
84
85function registerReactNativeCommands(context: vscode.ExtensionContext): void {
86 // Register React Native commands
87 registerVSCodeCommand(context, "runAndroid", "Failed to run the application in Android", () => commandPaletteHandler.runAndroid());
88 registerVSCodeCommand(context, "runIos", "Failed to run the application in iOS", () => commandPaletteHandler.runIos());
89 registerVSCodeCommand(context, "startPackager", "Failed to start the React Native packager", () => commandPaletteHandler.startPackager());
90 registerVSCodeCommand(context, "stopPackager", "Failed to stop the React Native packager", () => commandPaletteHandler.stopPackager());
91}
92
93function registerVSCodeCommand(
94 context: vscode.ExtensionContext, commandName: string,
95 errorDescription: string, commandHandler: () => Q.Promise<void>): void {
96 context.subscriptions.push(vscode.commands.registerCommand(
97 `reactNative.${commandName}`,
98 () =>
99 entryPointHandler.runFunction(
100 `commandPalette.${commandName}`, errorDescription,
101 commandHandler)));
102}
103
104/**
105 * Sets up the debugger for the React Native project by dropping
106 * the debugger stub into the workspace
107 */
108function setupReactNativeDebugger(): Q.Promise<void> {
109 const launcherPath = require.resolve("../debugger/launcher");
110 const pkg = require("../../package.json");
111 const extensionVersionNumber = pkg.version;
112 const extensionName = pkg.name;
113
114 let debuggerEntryCode =
115 `// This file is automatically generated by ${extensionName}@${extensionVersionNumber}
116// Please do not modify it manually. All changes will be lost.
117try {
118 var path = require("path");
119 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
120 new Launcher(path.resolve(__dirname, "..")).launch();
121} catch (e) {
122 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
123}`;
124
125 const vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
126 const debugStub = path.join(vscodeFolder, "launchReactNative.js");
127
128 return fsUtil.ensureDirectory(vscodeFolder)
129 .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode))
130 .catch((err: Error) => {
131 vscode.window.showErrorMessage(err.message);
132 });
133}