microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/rn-extension.ts
153lines · modeblame
9cf5eef2leolee10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
f1cad558Jimmy Thomson10 years ago | 3 | |
d7df9dd4digeff10 years ago | 4 | import * as fs from "fs"; |
| 5 | | |
cc7a6803digeff10 years ago | 6 | // @ifdef DEBUG |
d7df9dd4digeff10 years ago | 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 | } | |
cc7a6803digeff10 years ago | 15 | // @endif |
d7df9dd4digeff10 years ago | 16 | |
8db64d13digeff10 years ago | 17 | import * as Q from "q"; |
13ce3068Meena Kunnathur Balakrishnan10 years ago | 18 | import * as path from "path"; |
7b341316Jimmy Thomson10 years ago | 19 | import * as vscode from "vscode"; |
8db64d13digeff10 years ago | 20 | |
| 21 | import {FileSystem} from "../common/node/fileSystem"; | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 22 | import {CommandPaletteHandler} from "./commandPaletteHandler"; |
a822ac85dlebu10 years ago | 23 | import {Packager} from "../common/packager"; |
18d8ad2adigeff10 years ago | 24 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
c2bf3c4fdigeff10 years ago | 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"; | |
5bc682d4Alexander Sorokin9 years ago | 29 | import {LogHelper} from "../common/log/logHelper"; |
| 30 | import {SettingsHelper} from "./settingsHelper"; | |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 31 | import {PackagerStatusIndicator} from "./packagerStatusIndicator"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 32 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
9c9df666Meena Kunnathur Balakrishnan10 years ago | 33 | import {ReactDirManager} from "./reactDirManager"; |
6ab6f718Jimmy Thomson10 years ago | 34 | import {IntellisenseHelper} from "./intellisenseHelper"; |
9ac38e08Joshua Skelton10 years ago | 35 | import {Telemetry} from "../common/telemetry"; |
87323739dlebu10 years ago | 36 | import {TelemetryHelper} from "../common/telemetryHelper"; |
0502b7a8dlebu10 years ago | 37 | import {ExtensionServer} from "./extensionServer"; |
3837f9a0Jimmy Thomson9 years ago | 38 | import {DelayedOutputChannelLogger} from "./outputChannelLogger"; |
1c32fe84Patricio Beltran9 years ago | 39 | import {ExponentHelper} from "../common/exponent/exponentHelper"; |
9cf5eef2leolee10 years ago | 40 | |
a822ac85dlebu10 years ago | 41 | /* all components use the same packager instance */ |
38edb09eAlexander Sorokin9 years ago | 42 | const projectRootPath = SettingsHelper.getReactNativeProjectRoot(); |
| 43 | const workspaceRootPath = vscode.workspace.rootPath; | |
| 44 | const globalPackager = new Packager(workspaceRootPath, projectRootPath); | |
ffffa686Meena Kunnathur Balakrishnan10 years ago | 45 | const packagerStatusIndicator = new PackagerStatusIndicator(); |
38edb09eAlexander Sorokin9 years ago | 46 | const globalExponentHelper = new ExponentHelper(workspaceRootPath, projectRootPath); |
1c32fe84Patricio Beltran9 years ago | 47 | const commandPaletteHandler = new CommandPaletteHandler(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper); |
a822ac85dlebu10 years ago | 48 | |
3837f9a0Jimmy Thomson9 years ago | 49 | const outputChannelLogger = new DelayedOutputChannelLogger("React-Native"); |
18d8ad2adigeff10 years ago | 50 | const entryPointHandler = new EntryPointHandler(ProcessType.Extension, outputChannelLogger); |
4f4e082ddigeff10 years ago | 51 | const reactNativeProjectHelper = new ReactNativeProjectHelper(projectRootPath); |
10873e11digeff10 years ago | 52 | const fsUtil = new FileSystem(); |
8ba55f4cJimmy Thomson10 years ago | 53 | |
710f8655digeff10 years ago | 54 | interface ISetupableDisposable extends vscode.Disposable { |
| 55 | setup(): Q.Promise<any>; | |
| 56 | } | |
| 57 | | |
9cf5eef2leolee10 years ago | 58 | export function activate(context: vscode.ExtensionContext): void { |
5bc682d4Alexander Sorokin9 years ago | 59 | configureLogLevel(); |
c2bf3c4fdigeff10 years ago | 60 | entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version, |
18d8ad2adigeff10 years ago | 61 | ErrorHelper.getInternalError(InternalErrorCode.ExtensionActivationFailed), projectRootPath, () => { |
10873e11digeff10 years ago | 62 | return reactNativeProjectHelper.isReactNativeProject() |
| 63 | .then(isRNProject => { | |
| 64 | if (isRNProject) { | |
9ac38e08Joshua Skelton10 years ago | 65 | let activateExtensionEvent = TelemetryHelper.createTelemetryEvent("activate"); |
| 66 | Telemetry.send(activateExtensionEvent); | |
| 67 | | |
10873e11digeff10 years ago | 68 | warnWhenReactNativeVersionIsNotSupported(); |
c2bf3c4fdigeff10 years ago | 69 | entryPointHandler.runFunction("debugger.setupLauncherStub", |
| 70 | ErrorHelper.getInternalError(InternalErrorCode.DebuggerStubLauncherFailed), () => | |
e45838cbVladimir Kotikov9 years ago | 71 | setupAndDispose(new ReactDirManager(), context) |
710f8655digeff10 years ago | 72 | .then(() => |
1c32fe84Patricio Beltran9 years ago | 73 | setupAndDispose(new ExtensionServer(projectRootPath, globalPackager, packagerStatusIndicator, globalExponentHelper), context)) |
710f8655digeff10 years ago | 74 | .then(() => {})); |
c2bf3c4fdigeff10 years ago | 75 | entryPointHandler.runFunction("intelliSense.setup", |
| 76 | ErrorHelper.getInternalError(InternalErrorCode.IntellisenseSetupFailed), () => | |
ef24cc06digeff10 years ago | 77 | IntellisenseHelper.setupReactNativeIntellisense()); |
10873e11digeff10 years ago | 78 | } |
c2bf3c4fdigeff10 years ago | 79 | entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation", |
f6b41bbfAlexander Sorokin9 years ago | 80 | ErrorHelper.getInternalError(InternalErrorCode.NodeDebuggerConfigurationFailed), () => { |
| 81 | configureNodeDebuggerLocation(); | |
| 82 | }); | |
10873e11digeff10 years ago | 83 | registerReactNativeCommands(context); |
| 84 | }); | |
| 85 | }); | |
13ce3068Meena Kunnathur Balakrishnan10 years ago | 86 | } |
| 87 | | |
568fc5bedaserge9 years ago | 88 | export function deactivate(): Q.Promise<void> { |
| 89 | return Q.Promise<void>(function (resolve) { | |
| 90 | // Kill any packager processes that we spawned | |
| 91 | entryPointHandler.runFunction("extension.deactivate", | |
| 92 | ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackagerOnExit), | |
| 93 | () => { | |
| 94 | commandPaletteHandler.stopPackager().done(() => { | |
| 95 | // Tell vscode that we are done with deactivation | |
| 96 | resolve(void 0); | |
| 97 | }); | |
| 98 | }, /*errorsAreFatal*/ true); | |
| 99 | }); | |
10873e11digeff10 years ago | 100 | } |
| 101 | | |
5bc682d4Alexander Sorokin9 years ago | 102 | function configureLogLevel(): void { |
| 103 | LogHelper.logLevel = SettingsHelper.getLogLevel(); | |
| 104 | } | |
| 105 | | |
10873e11digeff10 years ago | 106 | function configureNodeDebuggerLocation(): Q.Promise<void> { |
e45838cbVladimir Kotikov9 years ago | 107 | const nodeDebugExtension = vscode.extensions.getExtension("ms-vscode.node-debug2"); |
8db64d13digeff10 years ago | 108 | if (!nodeDebugExtension) { |
2166bdabdigeff10 years ago | 109 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotFindLocationOfNodeDebugger)); |
8db64d13digeff10 years ago | 110 | } |
| 111 | const nodeDebugPath = nodeDebugExtension.extensionPath; | |
10873e11digeff10 years ago | 112 | return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath })); |
| 113 | } | |
4a25dae0digeff10 years ago | 114 | |
710f8655digeff10 years ago | 115 | function setupAndDispose<T extends ISetupableDisposable>(setuptableDisposable: T, context: vscode.ExtensionContext): Q.Promise<T> { |
| 116 | return setuptableDisposable.setup() | |
eb113a1fdlebu10 years ago | 117 | .then(() => { |
710f8655digeff10 years ago | 118 | context.subscriptions.push(setuptableDisposable); |
| 119 | return setuptableDisposable; | |
eb113a1fdlebu10 years ago | 120 | }); |
13ce3068Meena Kunnathur Balakrishnan10 years ago | 121 | } |
| 122 | | |
10873e11digeff10 years ago | 123 | function warnWhenReactNativeVersionIsNotSupported(): void { |
| 124 | return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => { | |
b3aec86adigeff10 years ago | 125 | TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason }); |
e44f7ea3Meena Kunnathur Balakrishnan10 years ago | 126 | const shortMessage = `React Native Tools need React Native version 0.19.0 or later to be installed in <PROJECT_ROOT>/node_modules/`; |
10873e11digeff10 years ago | 127 | const longMessage = `${shortMessage}: ${reason}`; |
| 128 | vscode.window.showWarningMessage(shortMessage); | |
c2bf3c4fdigeff10 years ago | 129 | Log.logMessage(longMessage); |
10873e11digeff10 years ago | 130 | }); |
| 131 | } | |
| 132 | | |
| 133 | function registerReactNativeCommands(context: vscode.ExtensionContext): void { | |
| 134 | // Register React Native commands | |
c2bf3c4fdigeff10 years ago | 135 | registerVSCodeCommand(context, "runAndroid", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnAndroid), () => commandPaletteHandler.runAndroid()); |
| 136 | registerVSCodeCommand(context, "runIos", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnIos), () => commandPaletteHandler.runIos()); | |
| 137 | registerVSCodeCommand(context, "startPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartPackager), () => commandPaletteHandler.startPackager()); | |
1c32fe84Patricio Beltran9 years ago | 138 | registerVSCodeCommand(context, "startExponentPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartExponentPackager), () => commandPaletteHandler.startExponentPackager()); |
c2bf3c4fdigeff10 years ago | 139 | registerVSCodeCommand(context, "stopPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackager), () => commandPaletteHandler.stopPackager()); |
f2a58eefBret Johnson9 years ago | 140 | registerVSCodeCommand(context, "restartPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToRestartPackager), () => commandPaletteHandler.restartPackager()); |
7893fb7eJimmy Thomson9 years ago | 141 | registerVSCodeCommand(context, "publishToExpHost", ErrorHelper.getInternalError(InternalErrorCode.FailedToPublishToExpHost), () => commandPaletteHandler.publishToExpHost()); |
10873e11digeff10 years ago | 142 | } |
| 143 | | |
| 144 | function registerVSCodeCommand( | |
| 145 | context: vscode.ExtensionContext, commandName: string, | |
c2bf3c4fdigeff10 years ago | 146 | error: InternalError, commandHandler: () => Q.Promise<void>): void { |
10873e11digeff10 years ago | 147 | context.subscriptions.push(vscode.commands.registerCommand( |
| 148 | `reactNative.${commandName}`, | |
| 149 | () => | |
47958817digeff10 years ago | 150 | entryPointHandler.runFunction( |
c2bf3c4fdigeff10 years ago | 151 | `commandPalette.${commandName}`, error, |
ef24cc06digeff10 years ago | 152 | commandHandler))); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 153 | } |