microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6126d89952da3aff613e4e1b9f64d5ffeca852f7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

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