microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.1

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