microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d25ea4e8955726af651edc5b23fdbc18ffe66e6f

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
4import {FileSystem} from "./utils/node/fileSystem";
5import * as path from "path";
6import * as vscode from "vscode";
7import {ReactNativeCommandExecutor} from "./utils/reactNativeCommandExecutor";
8import {ReactNativeProjectHelper} from "./utils/reactNativeProjectHelper";
9import {ReactDirManager} from "./utils/reactDirManager";
10
11export 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 */
37function 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}
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
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