microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4f4e082dd1d2adc773da8fdd1c2a71ffaa5837dd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/rn-extension.ts

162lines · 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 * as fs from "fs";
5
6try {
7 fs.statSync(`${__filename}.map`); // We check if source maps are available
8 /* tslint:disable:no-var-requires */
9 require("source-map-support").install(); // If they are, we enable stack traces translation to typescript
10 /* tslint:enable:no-var-requires */
11} catch (exceptions) {
12 // If something goes wrong, we just ignore the errors
13}
14
15import * as Q from "q";
16import * as path from "path";
17import * as vscode from "vscode";
18
19import {FileSystem} from "../common/node/fileSystem";
20import {CommandPaletteHandler} from "./commandPaletteHandler";
21import {Packager} from "../common/packager";
22import {EntryPointHandler} from "../common/entryPointHandler";
23import {ErrorHelper} from "../common/error/errorHelper";
24import {InternalError} from "../common/error/internalError";
25import {InternalErrorCode} from "../common/error/internalErrorCode";
26import {Log} from "../common/log/log";
27import {PackagerStatusIndicator} from "./packagerStatusIndicator";
28import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
29import {ReactDirManager} from "./reactDirManager";
30import {IntellisenseHelper} from "./intellisenseHelper";
31import {TelemetryHelper} from "../common/telemetryHelper";
32import {ExtensionServer} from "./extensionServer";
33import {OutputChannelLogger} from "./outputChannelLogger";
34
35/* all components use the same packager instance */
36const projectRootPath = vscode.workspace.rootPath;
37const globalPackager = new Packager(projectRootPath);
38const packagerStatusIndicator = new PackagerStatusIndicator();
39const commandPaletteHandler = new CommandPaletteHandler(projectRootPath, globalPackager, packagerStatusIndicator);
40
41const outputChannelLogger = new OutputChannelLogger(vscode.window.createOutputChannel("React-Native"));
42const entryPointHandler = new EntryPointHandler(false, outputChannelLogger);
43const reactNativeProjectHelper = new ReactNativeProjectHelper(projectRootPath);
44const fsUtil = new FileSystem();
45
46interface ISetupableDisposable extends vscode.Disposable {
47 setup(): Q.Promise<any>;
48}
49
50export function activate(context: vscode.ExtensionContext): void {
51 entryPointHandler.runApp("react-native", () => <string>require("../../package.json").version,
52 ErrorHelper.getInternalError(InternalErrorCode.ExtensionActivationFailed), () => {
53 return reactNativeProjectHelper.isReactNativeProject()
54 .then(isRNProject => {
55 if (isRNProject) {
56 warnWhenReactNativeVersionIsNotSupported();
57 entryPointHandler.runFunction("debugger.setupLauncherStub",
58 ErrorHelper.getInternalError(InternalErrorCode.DebuggerStubLauncherFailed), () =>
59 setupReactNativeDebugger()
60 .then(() =>
61 setupAndDispose(new ReactDirManager(), context))
62 .then(() =>
63 setupAndDispose(new ExtensionServer(projectRootPath, globalPackager, packagerStatusIndicator), context))
64 .then(() => {}));
65 entryPointHandler.runFunction("intelliSense.setup",
66 ErrorHelper.getInternalError(InternalErrorCode.IntellisenseSetupFailed), () =>
67 IntellisenseHelper.setupReactNativeIntellisense());
68 }
69 entryPointHandler.runFunction("debugger.setupNodeDebuggerLocation",
70 ErrorHelper.getInternalError(InternalErrorCode.NodeDebuggerConfigurationFailed), () =>
71 configureNodeDebuggerLocation());
72 registerReactNativeCommands(context);
73 });
74 });
75}
76
77export function deactivate(): void {
78 // Kill any packager processes that we spawned
79 entryPointHandler.runFunction("extension.deactivate",
80 ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackagerOnExit),
81 () => {
82 commandPaletteHandler.stopPackager();
83 }, /*errorsAreFatal*/ true);
84}
85
86function configureNodeDebuggerLocation(): Q.Promise<void> {
87 const nodeDebugExtension = vscode.extensions.getExtension("ms-vscode.node-debug") // We try to get the new version
88 || vscode.extensions.getExtension("andreweinand.node-debug"); // If it's not available, we try to get the old version
89 if (!nodeDebugExtension) {
90 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotFindLocationOfNodeDebugger));
91 }
92 const nodeDebugPath = nodeDebugExtension.extensionPath;
93 return fsUtil.writeFile(path.resolve(__dirname, "../", "debugger", "nodeDebugLocation.json"), JSON.stringify({ nodeDebugPath }));
94}
95
96function setupAndDispose<T extends ISetupableDisposable>(setuptableDisposable: T, context: vscode.ExtensionContext): Q.Promise<T> {
97 return setuptableDisposable.setup()
98 .then(() => {
99 context.subscriptions.push(setuptableDisposable);
100 return setuptableDisposable;
101 });
102}
103
104function warnWhenReactNativeVersionIsNotSupported(): void {
105 return reactNativeProjectHelper.validateReactNativeVersion().done(() => { }, reason => {
106 TelemetryHelper.sendSimpleEvent("unsupportedRNVersion", { rnVersion: reason });
107 const shortMessage = `React Native Tools only supports React Native versions 0.19.0 and later`;
108 const longMessage = `${shortMessage}: ${reason}`;
109 vscode.window.showWarningMessage(shortMessage);
110 Log.logMessage(longMessage);
111 });
112}
113
114function registerReactNativeCommands(context: vscode.ExtensionContext): void {
115 // Register React Native commands
116 registerVSCodeCommand(context, "runAndroid", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnAndroid), () => commandPaletteHandler.runAndroid());
117 registerVSCodeCommand(context, "runIos", ErrorHelper.getInternalError(InternalErrorCode.FailedToRunOnIos), () => commandPaletteHandler.runIos());
118 registerVSCodeCommand(context, "startPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStartPackager), () => commandPaletteHandler.startPackager());
119 registerVSCodeCommand(context, "stopPackager", ErrorHelper.getInternalError(InternalErrorCode.FailedToStopPackager), () => commandPaletteHandler.stopPackager());
120}
121
122function registerVSCodeCommand(
123 context: vscode.ExtensionContext, commandName: string,
124 error: InternalError, commandHandler: () => Q.Promise<void>): void {
125 context.subscriptions.push(vscode.commands.registerCommand(
126 `reactNative.${commandName}`,
127 () =>
128 entryPointHandler.runFunction(
129 `commandPalette.${commandName}`, error,
130 commandHandler)));
131}
132
133/**
134 * Sets up the debugger for the React Native project by dropping
135 * the debugger stub into the workspace
136 */
137function setupReactNativeDebugger(): Q.Promise<void> {
138 const launcherPath = require.resolve("../debugger/launcher");
139 const pkg = require("../../package.json");
140 const extensionVersionNumber = pkg.version;
141 const extensionName = pkg.name;
142
143 let debuggerEntryCode =
144 `// This file is automatically generated by ${extensionName}@${extensionVersionNumber}
145// Please do not modify it manually. All changes will be lost.
146try {
147 var path = require("path");
148 var Launcher = require(${JSON.stringify(launcherPath)}).Launcher;
149 new Launcher(path.resolve(__dirname, "..")).launch();
150} catch (e) {
151 throw new Error("Unable to launch application. Try deleting .vscode/launchReactNative.js and restarting vscode.");
152}`;
153
154 const vscodeFolder = path.join(projectRootPath, ".vscode");
155 const debugStub = path.join(vscodeFolder, "launchReactNative.js");
156
157 return fsUtil.ensureDirectory(vscodeFolder)
158 .then(() => fsUtil.ensureFileWithContents(debugStub, debuggerEntryCode))
159 .catch((err: Error) => {
160 vscode.window.showErrorMessage(err.message);
161 });
162}