microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/rn-extension.ts
143lines · 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 {FileSystem} from "../common/node/fileSystem"; |
| 5 | import * as path from "path"; |
| 6 | import * as vscode from "vscode"; |
| 7 | import {CommandPaletteHandler} from "./commandPaletteHandler"; |
| 8 | import {Packager} from "../common/packager"; |
| 9 | import {EntryPointHandler} from "../common/entryPointHandler"; |
| 10 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 11 | import {InternalError} from "../common/error/internalError"; |
| 12 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 13 | import {Log} from "../common/log/log"; |
| 14 | import {PackagerStatusIndicator} from "./packagerStatusIndicator"; |
| 15 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 16 | import {ReactDirManager} from "./reactDirManager"; |
| 17 | import {IntellisenseHelper} from "./intellisenseHelper"; |
| 18 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 19 | import {ExtensionServer} from "./extensionServer"; |
| 20 | import {OutputChannelLogger} from "./outputChannelLogger"; |
| 21 | |
| 22 | /* all components use the same packager instance */ |
| 23 | const globalPackager = new Packager(vscode.workspace.rootPath); |
| 24 | const packagerStatusIndicator = new PackagerStatusIndicator(); |
| 25 | const commandPaletteHandler = new CommandPaletteHandler(vscode.workspace.rootPath, globalPackager, packagerStatusIndicator); |
| 26 | |
| 27 | const outputChannelLogger = new OutputChannelLogger(vscode.window.createOutputChannel("React-Native")); |
| 28 | const entryPointHandler = new EntryPointHandler(false, outputChannelLogger); |
| 29 | const reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 30 | const fsUtil = new FileSystem(); |
| 31 | |
| 32 | interface ISetupableDisposable extends vscode.Disposable { |
| 33 | setup(): Q.Promise<any>; |
| 34 | } |
| 35 | |
| 36 | export function activate(context: vscode.ExtensionContext): void { |
| 37 | entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version, |
| 38 | ErrorHelper.getInternalError(InternalErrorCode.ExtensionActivationFailed), () => { |
| 39 | return reactNativeProjectHelper.isReactNativeProject() |
| 40 | .then(isRNProject => { |
| 41 | if (isRNProject) { |
| 42 | warnWhenReactNativeVersionIsNotSupported(); |
| 43 | entryPointHandler.runFunction("debugger.setupLauncherStub", |
| 44 | ErrorHelper.getInternalError(InternalErrorCode.DebuggerStubLauncherFailed), () => |
| 45 | setupReactNativeDebugger() |
| 46 | .then(() => |
| 47 | setupAndDispose(new ReactDirManager(), context)) |
| 48 | .then(() => |
| 49 | setupAndDispose(new ExtensionServer(globalPackager, packagerStatusIndicator), context)) |
| 50 | .then(() => {})); |
| 51 | entryPointHandler.runFunction("intelliSense.setup", |
| 52 | ErrorHelper.getInternalError(InternalErrorCode.IntellisenseSetupFailed), () => |
| 53 | IntellisenseHelper.setupReactNativeIntellisense()); |
| 54 | } |
| 55 | entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation", |
| 56 | ErrorHelper.getInternalError(InternalErrorCode.NodeDebuggerConfigurationFailed), () => |
| 57 | configureNodeDebuggerLocation()); |
| 58 | registerReactNativeCommands(context); |
| 59 | }); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | export function deactivate(): void { |
| 64 | // Kill any packager processes that we spawned |
| 65 | entryPointHandler.runFunction("extension.deactivate", |
| 66 | ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackagerOnExit), |
| 67 | () => { |
| 68 | commandPaletteHandler.stopPackager(); |
| 69 | }, /*errorsAreFatal*/ true); |
| 70 | } |
| 71 | |
| 72 | function configureNodeDebuggerLocation(): Q.Promise<void> { |
| 73 | const nodeDebugPath = vscode.extensions.getExtension("andreweinand.node-debug").extensionPath; |
| 74 | return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath })); |
| 75 | } |
| 76 | |
| 77 | function setupAndDispose<T extends ISetupableDisposable>(setuptableDisposable: T, context: vscode.ExtensionContext): Q.Promise<T> { |
| 78 | return setuptableDisposable.setup() |
| 79 | .then(() => { |
| 80 | context.subscriptions.push(setuptableDisposable); |
| 81 | return setuptableDisposable; |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | function warnWhenReactNativeVersionIsNotSupported(): void { |
| 86 | return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => { |
| 87 | TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason }); |
| 88 | const shortMessage = `React Native Tools only supports React Native versions 0.19.0 and later`; |
| 89 | const longMessage = `${shortMessage}: ${reason}`; |
| 90 | vscode.window.showWarningMessage(shortMessage); |
| 91 | Log.logMessage(longMessage); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | function registerReactNativeCommands(context: vscode.ExtensionContext): void { |
| 96 | // Register React Native commands |
| 97 | registerVSCodeCommand(context, "runAndroid", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnAndroid), () => commandPaletteHandler.runAndroid()); |
| 98 | registerVSCodeCommand(context, "runIos", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnIos), () => commandPaletteHandler.runIos()); |
| 99 | registerVSCodeCommand(context, "startPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartPackager), () => commandPaletteHandler.startPackager()); |
| 100 | registerVSCodeCommand(context, "stopPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackager), () => commandPaletteHandler.stopPackager()); |
| 101 | } |
| 102 | |
| 103 | function registerVSCodeCommand( |
| 104 | context: vscode.ExtensionContext, commandName: string, |
| 105 | error: InternalError, commandHandler: () => Q.Promise<void>): void { |
| 106 | context.subscriptions.push(vscode.commands.registerCommand( |
| 107 | `reactNative.${commandName}`, |
| 108 | () => |
| 109 | entryPointHandler.runFunction( |
| 110 | `commandPalette.${commandName}`, error, |
| 111 | commandHandler))); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets up the debugger for the React Native project by dropping |
| 116 | * the debugger stub into the workspace |
| 117 | */ |
| 118 | function setupReactNativeDebugger(): Q.Promise<void> { |
| 119 | const launcherPath = require.resolve("../debugger/launcher"); |
| 120 | const pkg = require("../../package.json"); |
| 121 | const extensionVersionNumber = pkg.version; |
| 122 | const extensionName = pkg.name; |
| 123 | |
| 124 | let debuggerEntryCode = |
| 125 | `// This file is automatically generated by ${extensionName}@${extensionVersionNumber} |
| 126 | // Please do not modify it manually. All changes will be lost. |
| 127 | try { |
| 128 | var path = require("path"); |
| 129 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 130 | new Launcher(path.resolve(__dirname, "..")).launch(); |
| 131 | } catch (e) { |
| 132 | throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode."); |
| 133 | }`; |
| 134 | |
| 135 | const vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 136 | const debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 137 | |
| 138 | return fsUtil.ensureDirectory(vscodeFolder) |
| 139 | .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode)) |
| 140 | .catch((err: Error) => { |
| 141 | vscode.window.showErrorMessage(err.message); |
| 142 | }); |
| 143 | } |