microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/rn-extension.ts
62lines · 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 | import {ReactDirManager} from "./utils/reactDirManager"; |
| 10 | |
| 11 | export function activate(context: vscode.ExtensionContext): void { |
| 12 | let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath); |
| 13 | reactNativeProjectHelper.isReactNativeProject().then(isRNProject => { |
| 14 | if (isRNProject) { |
| 15 | setupReactNativeDebugger(); |
| 16 | context.subscriptions.push(new ReactDirManager()); |
| 17 | } |
| 18 | }); |
| 19 | |
| 20 | let reactNativeCommandExecutor = new ReactNativeCommandExecutor(vscode.workspace.rootPath); |
| 21 | |
| 22 | // Register React Native commands |
| 23 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid", |
| 24 | () => reactNativeCommandExecutor.runAndroid())); |
| 25 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos", |
| 26 | () => reactNativeCommandExecutor.runIos())); |
| 27 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager", |
| 28 | () => reactNativeCommandExecutor.startPackager())); |
| 29 | context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager", |
| 30 | () => reactNativeCommandExecutor.stopPackager())); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Sets up the debugger for the React Native project by dropping |
| 35 | * the debugger stub into the workspace |
| 36 | */ |
| 37 | function setupReactNativeDebugger(): void { |
| 38 | let launcherPath = require.resolve("./debugger/launcher"); |
| 39 | let pkg = require("../package.json"); |
| 40 | const extensionVersionNumber = pkg.version; |
| 41 | const extensionName = pkg.name; |
| 42 | |
| 43 | let debuggerEntryCode = |
| 44 | `// This file is automatically generated by ${extensionName}@${extensionVersionNumber} |
| 45 | try { |
| 46 | var path = require("path"); |
| 47 | var Launcher = require(${JSON.stringify(launcherPath)}).Launcher; |
| 48 | new Launcher(path.resolve(__dirname, "..")).launch(); |
| 49 | } catch (e) { |
| 50 | throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode."); |
| 51 | }`; |
| 52 | |
| 53 | let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode"); |
| 54 | let debugStub = path.join(vscodeFolder, "launchReactNative.js"); |
| 55 | let fsUtil = new FileSystem(); |
| 56 | |
| 57 | fsUtil.ensureDirectory(vscodeFolder).then(() => { |
| 58 | fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode); |
| 59 | }).catch((err: Error) => { |
| 60 | vscode.window.showErrorMessage(err.message); |
| 61 | }); |
| 62 | } |
| 63 | |