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 {ReactNativeCommandExecutor} from "./utils/reactNativeCommandExecutor"; |
| 8 | import {ReactNativeProjectHelper} from "./utils/reactNativeProjectHelper"; |
| 9 | |
| 10 | export function activate(context: vscode.ExtensionContext): void { |
| 11 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 12 | reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { |
| 13 | if (isRNProject) { |
| 14 | setupReactNativeDebugger(); |
| 15 | let reactDir = new ReactDirManager(); |
| 16 | reactDir.init(); |
| 17 | context.subscriptions.push(reactDir); |
| 18 | } |
| 19 | }); |
| 20 | |
| 21 | let reactNativeCommandExecutor = new ReactNativeCommandExecutor(vscode.workspace.rootPath); |
| 22 | |
| 23 | // Register React Native commands |
| 24 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid", |
| 25 | () => reactNativeCommandExecutor.executeCommandInContext(() => reactNativeCommandExecutor.runAndroid()))); |
| 26 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos", |
| 27 | () => reactNativeCommandExecutor.executeCommandInContext(() => reactNativeCommandExecutor.runIos()))); |
| 28 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager", |
| 29 | () => reactNativeCommandExecutor.executeCommandInContext(() => reactNativeCommandExecutor.startPackager()))); |
| 30 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager", |
| 31 | () => reactNativeCommandExecutor.executeCommandInContext(() => reactNativeCommandExecutor.stopPackager()))); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Sets up the debugger for the React Native project by dropping |
| 36 | * the debugger stub into the workspace |
| 37 | */ |
| 38 | function setupReactNativeDebugger(): void { |
| 39 | let launcherPath = require.resolve("./debugger/launcher"); |
| 40 | const extensionVersionNumber = require("../package.json").version; |
| 41 | |
| 42 | let debuggerEntryCode = |
| 43 | `// This file is automatically generated. version:${extensionVersionNumber} |
| 44 | try { |
| 45 | var path = require("path"); |
| 46 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 47 | new Launcher(path.resolve(__dirname, "..")).launch(); |
| 48 | } catch (e) { |
| 49 | throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode."); |
| 50 | }`; |
| 51 | let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 52 | let debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 53 | |
| 54 | let fsUtil = new FileSystem(); |
| 55 | |
| 56 | fsUtil.ensureDirectory(vscodeFolder).then(() => { |
| 57 | fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode); |
| 58 | }).catch((err: Error) => { |
| 59 | vscode.window.showErrorMessage(err.message); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Manages the lifecycle of the .vscode/.react folder, which hosts the temporary source/map files we need for debugging. |
| 65 | * We use synchronous operations here because we want to return after the init/cleanup has been done. |
| 66 | */ |
| 67 | class ReactDirManager implements vscode.Disposable { |
| 68 | public static ReactDirPath = path.join(vscode.workspace.rootPath, ".vscode", ".react"); |
| 69 | |
| 70 | public init(): void { |
| 71 | let fs = new FileSystem(); |
| 72 | /* if the folder exists, remove it, then recreate it */ |
| 73 | fs.removePathRecursivelyAsync(ReactDirManager.ReactDirPath) |
| 74 | .done(() => fs.mkDir(ReactDirManager.ReactDirPath)); |
| 75 | } |
| 76 | |
| 77 | public dispose(): void { |
| 78 | new FileSystem().removePathRecursivelySync(ReactDirManager.ReactDirPath); |
| 79 | } |
| 80 | } |