microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

153lines · 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 * as fs from "fs";
5
6// @ifdef DEBUG
7try {
8 fs.statSync(`${__filename}.map`); // We check if source maps are available
9 /* tslint:disable:no-var-requires */
10 require("source-map-support").install(); // If they are, we enable stack traces translation to typescript
11 /* tslint:enable:no-var-requires */
12} catch (exceptions) {
13 // If something goes wrong, we just ignore the errors
14}
15// @endif
16
17import * as Q from "q";
18import * as path from "path";
19import * as vscode from "vscode";
20
21import {FileSystem} from "../common/node/fileSystem";
22import {CommandPaletteHandler} from "./commandPaletteHandler";
23import {Packager} from "../common/packager";
24import {EntryPointHandler, ProcessType} from "../common/entryPointHandler";
25import {ErrorHelper} from "../common/error/errorHelper";
26import {InternalError} from "../common/error/internalError";
27import {InternalErrorCode} from "../common/error/internalErrorCode";
28import {Log} from "../common/log/log";
29import {LogHelper} from "../common/log/logHelper";
30import {SettingsHelper} from "./settingsHelper";
31import {PackagerStatusIndicator} from "./packagerStatusIndicator";
32import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
33import {ReactDirManager} from "./reactDirManager";
34import {IntellisenseHelper} from "./intellisenseHelper";
35import {Telemetry} from "../common/telemetry";
36import {TelemetryHelper} from "../common/telemetryHelper";
37import {ExtensionServer} from "./extensionServer";
38import {DelayedOutputChannelLogger} from "./outputChannelLogger";
39import {ExponentHelper} from "../common/exponent/exponentHelper";
40
41/* all components use the same packager instance */
42const projectRootPath = SettingsHelper.getReactNativeProjectRoot();
43const workspaceRootPath = vscode.workspace.rootPath;
44const globalPackager = new Packager(workspaceRootPath, projectRootPath);
45const packagerStatusIndicator = new PackagerStatusIndicator();
46const globalExponentHelper = new ExponentHelper(workspaceRootPath, projectRootPath);
47const commandPaletteHandler = new CommandPaletteHandler(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper);
48
49const outputChannelLogger = new DelayedOutputChannelLogger("React-Native");
50const entryPointHandler = new EntryPointHandler(ProcessType.Extension, outputChannelLogger);
51const reactNativeProjectHelper = new ReactNativeProjectHelper(projectRootPath);
52const fsUtil = new FileSystem();
53
54interface ISetupableDisposable extends vscode.Disposable {
55 setup(): Q.Promise<any>;
56}
57
58export function activate(context: vscode.ExtensionContext): void {
59 configureLogLevel();
60 entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version,
61 ErrorHelper.getInternalError(InternalErrorCode.ExtensionActivationFailed), projectRootPath, () => {
62 return reactNativeProjectHelper.isReactNativeProject()
63 .then(isRNProject => {
64 if (isRNProject) {
65 let activateExtensionEvent = TelemetryHelper.createTelemetryEvent("activate");
66 Telemetry.send(activateExtensionEvent);
67
68 warnWhenReactNativeVersionIsNotSupported();
69 entryPointHandler.runFunction("debugger.setupLauncherStub",
70 ErrorHelper.getInternalError(InternalErrorCode.DebuggerStubLauncherFailed), () =>
71 setupAndDispose(new ReactDirManager(), context)
72 .then(() =>
73 setupAndDispose(new ExtensionServer(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper), context))
74 .then(() => {}));
75 entryPointHandler.runFunction("intelliSense.setup",
76 ErrorHelper.getInternalError(InternalErrorCode.IntellisenseSetupFailed), () =>
77 IntellisenseHelper.setupReactNativeIntellisense());
78 }
79 entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation",
80 ErrorHelper.getInternalError(InternalErrorCode.NodeDebuggerConfigurationFailed), () => {
81 configureNodeDebuggerLocation();
82 });
83 registerReactNativeCommands(context);
84 });
85 });
86}
87
88export function deactivate(): Q.Promise<void> {
89 return Q.Promise<void>(function (resolve) {
90 // Kill any packager processes that we spawned
91 entryPointHandler.runFunction("extension.deactivate",
92 ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackagerOnExit),
93 () => {
94 commandPaletteHandler.stopPackager().done(() => {
95 // Tell vscode that we are done with deactivation
96 resolve(void 0);
97 });
98 }, /*errorsAreFatal*/ true);
99 });
100}
101
102function configureLogLevel(): void {
103 LogHelper.logLevel = SettingsHelper.getLogLevel();
104}
105
106function configureNodeDebuggerLocation(): Q.Promise<void> {
107 const nodeDebugExtension = vscode.extensions.getExtension("ms-vscode.node-debug2");
108 if (!nodeDebugExtension) {
109 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotFindLocationOfNodeDebugger));
110 }
111 const nodeDebugPath = nodeDebugExtension.extensionPath;
112 return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath }));
113}
114
115function setupAndDispose<T extends ISetupableDisposable>(setuptableDisposable: T, context: vscode.ExtensionContext): Q.Promise<T> {
116 return setuptableDisposable.setup()
117 .then(() => {
118 context.subscriptions.push(setuptableDisposable);
119 return setuptableDisposable;
120 });
121}
122
123function warnWhenReactNativeVersionIsNotSupported(): void {
124 return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => {
125 TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason });
126 const shortMessage = `React Native Tools need React Native version 0.19.0 or later to be installed in <PROJECT_ROOT>/node_modules/`;
127 const longMessage = `${shortMessage}: ${reason}`;
128 vscode.window.showWarningMessage(shortMessage);
129 Log.logMessage(longMessage);
130 });
131}
132
133function registerReactNativeCommands(context: vscode.ExtensionContext): void {
134 // Register React Native commands
135 registerVSCodeCommand(context, "runAndroid", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnAndroid), () => commandPaletteHandler.runAndroid());
136 registerVSCodeCommand(context, "runIos", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnIos), () => commandPaletteHandler.runIos());
137 registerVSCodeCommand(context, "startPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartPackager), () => commandPaletteHandler.startPackager());
138 registerVSCodeCommand(context, "startExponentPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartExponentPackager), () => commandPaletteHandler.startExponentPackager());
139 registerVSCodeCommand(context, "stopPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackager), () => commandPaletteHandler.stopPackager());
140 registerVSCodeCommand(context, "restartPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToRestartPackager), () => commandPaletteHandler.restartPackager());
141 registerVSCodeCommand(context, "publishToExpHost", ErrorHelper.getInternalError(InternalErrorCode.FailedToPublishToExpHost), () => commandPaletteHandler.publishToExpHost());
142}
143
144function registerVSCodeCommand(
145 context: vscode.ExtensionContext, commandName: string,
146 error: InternalError, commandHandler: () => Q.Promise<void>): void {
147 context.subscriptions.push(vscode.commands.registerCommand(
148 `reactNative.${commandName}`,
149 () =>
150 entryPointHandler.runFunction(
151 `commandPalette.${commandName}`, error,
152 commandHandler)));
153}
154