microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
88e05f1bcfe0ebe1535b7664699022e383da4811

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

75lines · 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 "../common/node/fileSystem";
5import * as path from "path";
6import * as vscode from "vscode";
7import {CommandPaletteHandler} from "./commandPaletteHandler";
8import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
9import {ReactDirManager} from "./reactDirManager";
10import {IntellisenseHelper} from "./IntellisenseHelper";
11import {Telemetry} from "../common/telemetry";
12
13export function activate(context: vscode.ExtensionContext): void {
14 // Asynchronously enable telemetry
15 Telemetry.init("react-native", require("../../package.json").version, true)
16 .then(() => {
17 const reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
18 return reactNativeProjectHelper.isReactNativeProject()
19 .then(isRNProject => {
20 if (isRNProject) {
21 setupReactNativeDebugger();
22 IntellisenseHelper.setupReactNativeIntellisense();
23 context.subscriptions.push(new ReactDirManager());
24 }
25 }).then(() => {
26 const commandPaletteHandler = new CommandPaletteHandler(vscode.workspace.rootPath);
27
28 // Register React Native commands
29 context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid",
30 () => commandPaletteHandler.runAndroid()));
31 context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos",
32 () => commandPaletteHandler.runIos()));
33 context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager",
34 () => commandPaletteHandler.startPackager()));
35 context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager",
36 () => commandPaletteHandler.stopPackager()));
37
38 const nodeDebugPath = vscode.extensions.getExtension("andreweinand.node-debug").extensionPath;
39 const fsUtil = new FileSystem();
40 fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath })).done();
41 });
42 }).done();
43}
44
45/**
46 * Sets up the debugger for the React Native project by dropping
47 * the debugger stub into the workspace
48 */
49function setupReactNativeDebugger(): void {
50 const launcherPath = require.resolve("../debugger/launcher");
51 const pkg = require("../../package.json");
52 const extensionVersionNumber = pkg.version;
53 const extensionName = pkg.name;
54
55 let debuggerEntryCode =
56 `// This file is automatically generated by ${extensionName}@${extensionVersionNumber}
57// Please do not modify it manually. All changes will be lost.
58try {
59 var path = require("path");
60 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
61 new Launcher(path.resolve(__dirname, "..")).launch();
62} catch (e) {
63 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
64}`;
65
66 const vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
67 const debugStub = path.join(vscodeFolder, "launchReactNative.js");
68 const fsUtil = new FileSystem();
69
70 fsUtil.ensureDirectory(vscodeFolder)
71 .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode))
72 .catch((err: Error) => {
73 vscode.window.showErrorMessage(err.message);
74 });
75}
76