microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0904a0d7ddf15d888e1d494ac66aa4f27e49aefb

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
5import {FileSystem} from "./utils/node/fileSystem";
6import {Package} from "./utils/node/package";
7import * as path from "path";
8import * as vscode from "vscode";
9
10import {ReactNativeCommandExecutor} from "./utils/reactNativeCommandExecutor";
11
12export function activate(context: vscode.ExtensionContext): void {
13 let currentPackage = new Package(vscode.workspace.rootPath);
14 currentPackage.dependencies().then(dependencies => {
15 if (dependencies && dependencies["react-native"]) {
16 // We are in a React Native project
17 // Setup the debugger for the project
18 setupReactNativeDebugger();
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.executeReactNativeCommand("run-android")));
25 context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos",
26 () => reactNativeCommandExecutor.executeReactNativeCommand("run-ios")));
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 }).catch(() => { });
33}
34
35/**
36 * Sets up the debugger for the React Native project by dropping
37 * the debugger stub into the workspace
38 */
39function setupReactNativeDebugger(): void {
40 let launcherPath = require.resolve("./debugger/launcher");
41 const extensionVersionNumber = require("../package.json").version;
42
43 let debuggerEntryCode =
44 `// This file is automatically generated. version:${extensionVersionNumber}
45try {
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 let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
53 let debugStub = path.join(vscodeFolder, "launchReactNative.js");
54
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}