microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
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 | import * as fs from "fs"; |
| 5 | |
| 6 | // @ifdef DEBUG |
| 7 | try { |
| 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 | |
| 17 | import * as Q from "q"; |
| 18 | import * as path from "path"; |
| 19 | import * as vscode from "vscode"; |
| 20 | |
| 21 | import {FileSystem} from "../common/node/fileSystem"; |
| 22 | import {CommandPaletteHandler} from "./commandPaletteHandler"; |
| 23 | import {Packager} from "../common/packager"; |
| 24 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
| 25 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 26 | import {InternalError} from "../common/error/internalError"; |
| 27 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 28 | import {Log} from "../common/log/log"; |
| 29 | import {LogHelper} from "../common/log/logHelper"; |
| 30 | import {SettingsHelper} from "./settingsHelper"; |
| 31 | import {PackagerStatusIndicator} from "./packagerStatusIndicator"; |
| 32 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 33 | import {ReactDirManager} from "./reactDirManager"; |
| 34 | import {IntellisenseHelper} from "./intellisenseHelper"; |
| 35 | import {Telemetry} from "../common/telemetry"; |
| 36 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 37 | import {ExtensionServer} from "./extensionServer"; |
| 38 | import {DelayedOutputChannelLogger} from "./outputChannelLogger"; |
| 39 | import { ExponentHelper } from "../common/exponent/exponentHelper"; |
| 40 | import { QRCodeContentProvider } from "./qrCodeContentProvider"; |
| 41 | import { ConfigurationReader } from "../common/configurationReader"; |
| 42 | |
| 43 | /* all components use the same packager instance */ |
| 44 | const projectRootPath = SettingsHelper.getReactNativeProjectRoot(); |
| 45 | const workspaceRootPath = vscode.workspace.rootPath; |
| 46 | |
| 47 | const packagerPort = ConfigurationReader.readIntWithDefaultSync( |
| 48 | Packager.DEFAULT_PORT, SettingsHelper.getPackagerPort()); |
| 49 | |
| 50 | const globalPackager = new Packager(workspaceRootPath, projectRootPath, packagerPort); |
| 51 | const packagerStatusIndicator = new PackagerStatusIndicator(); |
| 52 | const globalExponentHelper = new ExponentHelper(workspaceRootPath, projectRootPath); |
| 53 | const commandPaletteHandler = new CommandPaletteHandler(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper); |
| 54 | |
| 55 | const outputChannelLogger = new DelayedOutputChannelLogger("React-Native"); |
| 56 | const entryPointHandler = new EntryPointHandler(ProcessType.Extension, outputChannelLogger); |
| 57 | const reactNativeProjectHelper = new ReactNativeProjectHelper(projectRootPath); |
| 58 | const fsUtil = new FileSystem(); |
| 59 | |
| 60 | interface ISetupableDisposable extends vscode.Disposable { |
| 61 | setup(): Q.Promise<any>; |
| 62 | } |
| 63 | |
| 64 | export 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 | |
| 97 | export 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 | |
| 111 | function configureLogLevel(): void { |
| 112 | LogHelper.logLevel = SettingsHelper.getLogLevel(); |
| 113 | } |
| 114 | |
| 115 | function 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 | |
| 124 | function 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 | |
| 132 | function 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 | |
| 142 | function 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 | |
| 153 | function 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 | |