microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

157lines · 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";
40import { QRCodeContentProvider } from "./qrCodeContentProvider";
41
42/* all components use the same packager instance */
43const projectRootPath = SettingsHelper.getReactNativeProjectRoot();
44const workspaceRootPath = vscode.workspace.rootPath;
45const globalPackager = new Packager(workspaceRootPath, projectRootPath);
46const packagerStatusIndicator = new PackagerStatusIndicator();
47const globalExponentHelper = new ExponentHelper(workspaceRootPath, projectRootPath);
48const commandPaletteHandler = new CommandPaletteHandler(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper);
49
50const outputChannelLogger = new DelayedOutputChannelLogger("React-Native");
51const entryPointHandler = new EntryPointHandler(ProcessType.Extension, outputChannelLogger);
52const reactNativeProjectHelper = new ReactNativeProjectHelper(projectRootPath);
53const fsUtil = new FileSystem();
54
55interface ISetupableDisposable extends vscode.Disposable {
56 setup(): Q.Promise<any>;
57}
58
59export function activate(context: vscode.ExtensionContext): void {
60 configureLogLevel();
61 entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version,
62 ErrorHelper.getInternalError(InternalErrorCode.ExtensionActivationFailed), projectRootPath, () => {
63 return reactNativeProjectHelper.isReactNativeProject()
64 .then(isRNProject => {
65 if (isRNProject) {
66 let activateExtensionEvent = TelemetryHelper.createTelemetryEvent("activate");
67 Telemetry.send(activateExtensionEvent);
68
69 warnWhenReactNativeVersionIsNotSupported();
70 entryPointHandler.runFunction("debugger.setupLauncherStub",
71 ErrorHelper.getInternalError(InternalErrorCode.DebuggerStubLauncherFailed), () =>
72 setupAndDispose(new ReactDirManager(), context)
73 .then(() =>
74 setupAndDispose(new ExtensionServer(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper), context))
75 .then(() => {}));
76 entryPointHandler.runFunction("intelliSense.setup",
77 ErrorHelper.getInternalError(InternalErrorCode.IntellisenseSetupFailed), () =>
78 IntellisenseHelper.setupReactNativeIntellisense());
79 }
80 entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation",
81 ErrorHelper.getInternalError(InternalErrorCode.NodeDebuggerConfigurationFailed), () => {
82 configureNodeDebuggerLocation();
83 });
84
85 registerReactNativeCommands(context);
86 context.subscriptions.push(vscode.workspace
87 .registerTextDocumentContentProvider("exp", new QRCodeContentProvider()));
88 });
89 });
90}
91
92export function deactivate(): Q.Promise<void> {
93 return Q.Promise<void>(function (resolve) {
94 // Kill any packager processes that we spawned
95 entryPointHandler.runFunction("extension.deactivate",
96 ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackagerOnExit),
97 () => {
98 commandPaletteHandler.stopPackager().done(() => {
99 // Tell vscode that we are done with deactivation
100 resolve(void 0);
101 });
102 }, /*errorsAreFatal*/ true);
103 });
104}
105
106function configureLogLevel(): void {
107 LogHelper.logLevel = SettingsHelper.getLogLevel();
108}
109
110function configureNodeDebuggerLocation(): Q.Promise<void> {
111 const nodeDebugExtension = vscode.extensions.getExtension("ms-vscode.node-debug2");
112 if (!nodeDebugExtension) {
113 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotFindLocationOfNodeDebugger));
114 }
115 const nodeDebugPath = nodeDebugExtension.extensionPath;
116 return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath }));
117}
118
119function setupAndDispose<T extends ISetupableDisposable>(setuptableDisposable: T, context: vscode.ExtensionContext): Q.Promise<T> {
120 return setuptableDisposable.setup()
121 .then(() => {
122 context.subscriptions.push(setuptableDisposable);
123 return setuptableDisposable;
124 });
125}
126
127function warnWhenReactNativeVersionIsNotSupported(): void {
128 return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => {
129 TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason });
130 const shortMessage = `React Native Tools need React Native version 0.19.0 or later to be installed in <PROJECT_ROOT>/node_modules/`;
131 const longMessage = `${shortMessage}: ${reason}`;
132 vscode.window.showWarningMessage(shortMessage);
133 Log.logMessage(longMessage);
134 });
135}
136
137function registerReactNativeCommands(context: vscode.ExtensionContext): void {
138 // Register React Native commands
139 registerVSCodeCommand(context, "runAndroid", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnAndroid), () => commandPaletteHandler.runAndroid());
140 registerVSCodeCommand(context, "runIos", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnIos), () => commandPaletteHandler.runIos());
141 registerVSCodeCommand(context, "startPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartPackager), () => commandPaletteHandler.startPackager());
142 registerVSCodeCommand(context, "startExponentPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartExponentPackager), () => commandPaletteHandler.startExponentPackager());
143 registerVSCodeCommand(context, "stopPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackager), () => commandPaletteHandler.stopPackager());
144 registerVSCodeCommand(context, "restartPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToRestartPackager), () => commandPaletteHandler.restartPackager());
145 registerVSCodeCommand(context, "publishToExpHost", ErrorHelper.getInternalError(InternalErrorCode.FailedToPublishToExpHost), () => commandPaletteHandler.publishToExpHost());
146}
147
148function registerVSCodeCommand(
149 context: vscode.ExtensionContext, commandName: string,
150 error: InternalError, commandHandler: () => Q.Promise<void>): void {
151 context.subscriptions.push(vscode.commands.registerCommand(
152 `reactNative.${commandName}`,
153 () =>
154 entryPointHandler.runFunction(
155 `commandPalette.${commandName}`, error,
156 commandHandler)));
157}