microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/rn-extension.ts
80lines · 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 "./utils/node/fileSystem"; |
| 5 | import * as path from "path"; |
| 6 | import * as vscode from "vscode"; |
| 7 | import {CommandPaletteHandler} from "./utils/commandPaletteHandler"; |
| 8 | import {ReactNativeProjectHelper} from "./utils/reactNativeProjectHelper"; |
| 9 | import {ReactDirManager} from "./utils/reactDirManager"; |
| 10 | import {TsConfigHelper} from "./utils/tsconfigHelper"; |
| 11 | |
| 12 | export function activate(context: vscode.ExtensionContext): void { |
| 13 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 14 | reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { |
| 15 | if (isRNProject) { |
| 16 | setupReactNativeDebugger(); |
| 17 | setupReactNativeIntellisense(); |
| 18 | context.subscriptions.push(new ReactDirManager()); |
| 19 | } |
| 20 | }); |
| 21 | |
| 22 | let commandPaletteHandler = new CommandPaletteHandler(vscode.workspace.rootPath); |
| 23 | |
| 24 | // Register React Native commands |
| 25 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid", |
| 26 | () => commandPaletteHandler.runAndroid())); |
| 27 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos", |
| 28 | () => commandPaletteHandler.runIos())); |
| 29 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager", |
| 30 | () => commandPaletteHandler.startPackager())); |
| 31 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager", |
| 32 | () => commandPaletteHandler.stopPackager())); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Sets up the debugger for the React Native project by dropping |
| 37 | * the debugger stub into the workspace |
| 38 | */ |
| 39 | function setupReactNativeDebugger(): void { |
| 40 | let launcherPath = require.resolve("./debugger/launcher"); |
| 41 | let pkg = require("../package.json"); |
| 42 | const extensionVersionNumber = pkg.version; |
| 43 | const extensionName = pkg.name; |
| 44 | |
| 45 | let debuggerEntryCode = |
| 46 | `// This file is automatically generated by ${extensionName}@${extensionVersionNumber} |
| 47 | // Please do not modify it manually. All changes will be lost. |
| 48 | try { |
| 49 | var path = require("path"); |
| 50 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 51 | new Launcher(path.resolve(__dirname, "..")).launch(); |
| 52 | } catch (e) { |
| 53 | throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode."); |
| 54 | }`; |
| 55 | |
| 56 | let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 57 | let debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 58 | let fsUtil = new FileSystem(); |
| 59 | |
| 60 | fsUtil.ensureDirectory(vscodeFolder) |
| 61 | .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode)) |
| 62 | .catch((err: Error) => { |
| 63 | vscode.window.showErrorMessage(err.message); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | function setupReactNativeIntellisense(): void { |
| 68 | if (!process.env.VSCODE_TSJS) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // Enable JavaScript intellisense through Salsa language service |
| 73 | TsConfigHelper.allowJs(true).done(); |
| 74 | |
| 75 | let reactTypingsSource = path.resolve(__dirname, "..", "ReactTypings"); |
| 76 | let reactTypingsDest = path.resolve(vscode.workspace.rootPath, ".vscode", "typings"); |
| 77 | let fileSystem = new FileSystem(); |
| 78 | |
| 79 | fileSystem.copyRecursive(reactTypingsSource, reactTypingsDest).done(); |
| 80 | } |
| 81 | |