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