microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
297822a1eaadca4f95c0a56cc22ee8f12d0362bd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

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