microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/rn-extension.ts
91lines · 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 {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 9 | import {ReactDirManager} from "./reactDirManager"; |
| 10 | import {IntellisenseHelper} from "./IntellisenseHelper"; |
| 11 | import {Telemetry} from "../common/telemetry"; |
| 12 | import {TelemetryHelper} from "../common/TelemetryHelper"; |
| 13 | |
| 14 | const commandPaletteHandler = new CommandPaletteHandler(vscode.workspace.rootPath); |
| 15 | export function activate(context: vscode.ExtensionContext): void { |
| 16 | let workspaceRootPath = vscode.workspace.rootPath; |
| 17 | |
| 18 | // Asynchronously enable telemetry |
| 19 | Telemetry.init("react-native", require("../../package.json").version, true) |
| 20 | .then(() => { |
| 21 | const reactNativeProjectHelper = new ReactNativeProjectHelper(workspaceRootPath); |
| 22 | return reactNativeProjectHelper.isReactNativeProject() |
| 23 | .then(isRNProject => { |
| 24 | if (isRNProject) { |
| 25 | reactNativeProjectHelper.validateReactNativeVersion().fail(reason => { |
| 26 | TelemetryHelper.sendSimpleEvent("launchDebuggerError", { rnVersion: reason }); |
| 27 | const shortMessage = `React Native Tools only supports React Native versions 0.19.0 and later`; |
| 28 | const longMessage = `${shortMessage}: ${reason}`; |
| 29 | vscode.window.showWarningMessage(shortMessage); |
| 30 | let output = vscode.window.createOutputChannel("React-Native"); |
| 31 | output.appendLine(longMessage); |
| 32 | output.show(); |
| 33 | }).done(); |
| 34 | setupReactNativeDebugger(); |
| 35 | IntellisenseHelper.setupReactNativeIntellisense(); |
| 36 | context.subscriptions.push(new ReactDirManager()); |
| 37 | } |
| 38 | }).then(() => { |
| 39 | // Register React Native commands |
| 40 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid", |
| 41 | () => commandPaletteHandler.runAndroid())); |
| 42 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos", |
| 43 | () => commandPaletteHandler.runIos())); |
| 44 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager", |
| 45 | () => commandPaletteHandler.startPackager())); |
| 46 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager", |
| 47 | () => commandPaletteHandler.stopPackager())); |
| 48 | |
| 49 | const nodeDebugPath = vscode.extensions.getExtension("andreweinand.node-debug").extensionPath; |
| 50 | const fsUtil = new FileSystem(); |
| 51 | fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath })).done(); |
| 52 | }); |
| 53 | }).done(); |
| 54 | } |
| 55 | |
| 56 | export function deactivate(): void { |
| 57 | // Kill any packager processes that we spawned |
| 58 | commandPaletteHandler.stopPackager(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets up the debugger for the React Native project by dropping |
| 63 | * the debugger stub into the workspace |
| 64 | */ |
| 65 | function setupReactNativeDebugger(): void { |
| 66 | const launcherPath = require.resolve("../debugger/launcher"); |
| 67 | const pkg = require("../../package.json"); |
| 68 | const extensionVersionNumber = pkg.version; |
| 69 | const extensionName = pkg.name; |
| 70 | |
| 71 | let debuggerEntryCode = |
| 72 | `// This file is automatically generated by ${extensionName}@${extensionVersionNumber} |
| 73 | // Please do not modify it manually. All changes will be lost. |
| 74 | try { |
| 75 | var path = require("path"); |
| 76 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 77 | new Launcher(path.resolve(__dirname, "..")).launch(); |
| 78 | } catch (e) { |
| 79 | throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode."); |
| 80 | }`; |
| 81 | |
| 82 | const vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 83 | const debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 84 | const fsUtil = new FileSystem(); |
| 85 | |
| 86 | fsUtil.ensureDirectory(vscodeFolder) |
| 87 | .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode)) |
| 88 | .catch((err: Error) => { |
| 89 | vscode.window.showErrorMessage(err.message); |
| 90 | }); |
| 91 | } |
| 92 | |