microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
da86e6fffddfaf7f6ca5a97cf9f1c345394f3bdc

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

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