microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
80922027b17af83ccdabd0024fa7759df4f526d2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/rn-extension.ts

79lines · 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";
10import {TsConfigHelper} from "./utils/tsconfigHelper";
11
12export function activate(context: vscode.ExtensionContext): void {
13 let reactNativeProjectHelper = new ReactNativeProjectHelper(vscode.workspace.rootPath);
14 reactNativeProjectHelper.isReactNativeProject().then(isRNProject => {
15 if (isRNProject) {
16 setupReactNativeDebugger();
17 setupReactNativeIntellisense();
18 context.subscriptions.push(new ReactDirManager());
19 }
20 });
21
22 let reactNativeCommandExecutor = new ReactNativeCommandExecutor(vscode.workspace.rootPath);
23
24 // Register React Native commands
25 context.subscriptions.push(vscode.commands.registerCommand("reactNative.runAndroid",
26 () => reactNativeCommandExecutor.runAndroid()));
27 context.subscriptions.push(vscode.commands.registerCommand("reactNative.runIos",
28 () => reactNativeCommandExecutor.runIos()));
29 context.subscriptions.push(vscode.commands.registerCommand("reactNative.startPackager",
30 () => reactNativeCommandExecutor.startPackager()));
31 context.subscriptions.push(vscode.commands.registerCommand("reactNative.stopPackager",
32 () => reactNativeCommandExecutor.stopPackager()));
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 let pkg = require("../package.json");
42 const extensionVersionNumber = pkg.version;
43 const extensionName = pkg.name;
44
45 let debuggerEntryCode =
46`// This file is automatically generated by ${extensionName}@${extensionVersionNumber}
47try {
48 var path = require("path");
49 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
50 new Launcher(path.resolve(__dirname, "..")).launch();
51} catch (e) {
52 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
53}`;
54
55 let vscodeFolder = path.join(vscode.workspace.rootPath, ".vscode");
56 let debugStub = path.join(vscodeFolder, "launchReactNative.js");
57 let fsUtil = new FileSystem();
58
59 fsUtil.ensureDirectory(vscodeFolder).then(() => {
60 fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode);
61 }).catch((err: Error) => {
62 vscode.window.showErrorMessage(err.message);
63 });
64}
65
66function setupReactNativeIntellisense(): void {
67 if (!process.env.VSCODE_TSJS) {
68 return;
69 }
70
71 // Enable JavaScript intellisense through Salsa language service
72 TsConfigHelper.allowJs(true).done();
73
74 let reactTypingsSource = path.resolve(__dirname, "..", "ReactTypings");
75 let reactTypingsDest = path.resolve(vscode.workspace.rootPath, ".vscode", "typings");
76 let fileSystem = new FileSystem();
77
78 fileSystem.copyRecursive(reactTypingsSource, reactTypingsDest);
79}