microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/elementInspector.ts

89lines · 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 child_process from "child_process";
5import * as nls from "vscode-nls";
6import { ErrorHelper } from "../../common/error/errorHelper";
7import { InternalErrorCode } from "../../common/error/internalErrorCode";
8import { TipNotificationService } from "../services/tipsNotificationsService/tipsNotificationService";
9import { HostPlatform } from "../../common/hostPlatform";
10import { OutputChannelLogger } from "../log/OutputChannelLogger";
11import { Command } from "./util/command";
12
13nls.config({
14 messageFormat: nls.MessageFormat.bundle,
15 bundleFormat: nls.BundleFormat.standalone,
16})();
17const localize = nls.loadMessageBundle();
18
19// #todo!> commands should not maintain state
20let elementInspector: child_process.ChildProcess | undefined;
21
22export class RunElementInspector extends Command {
23 codeName = "runInspector";
24 label = "Run Element Inspector";
25 requiresTrust = false;
26 requiresProject = false;
27
28 error = ErrorHelper.getInternalError(
29 InternalErrorCode.CommandFailed,
30 localize("ReactNativeRunElementInspector", "React Native: Run Element Inspector"),
31 );
32
33 async baseFn(): Promise<void> {
34 const logger = OutputChannelLogger.getMainChannel();
35
36 void TipNotificationService.getInstance().setKnownDateForFeatureById("elementInspector");
37
38 if (elementInspector) {
39 logger.info(
40 localize(
41 "AnotherElementInspectorAlreadyRun",
42 "Another element inspector already run",
43 ),
44 );
45
46 return;
47 }
48 // Remove the following env variables to prevent running electron app in node mode.
49 // https://github.com/microsoft/vscode/issues/3011#issuecomment-184577502
50 // eslint-disable-next-line @typescript-eslint/no-unused-vars
51 const { ATOM_SHELL_INTERNAL_RUN_AS_NODE, ELECTRON_RUN_AS_NODE, ...env } = process.env;
52 const command = HostPlatform.getNpmCliCommand("react-devtools");
53
54 elementInspector = child_process.spawn(command, [], {
55 env,
56 });
57
58 if (!elementInspector.pid) {
59 elementInspector = undefined;
60 throw ErrorHelper.getInternalError(InternalErrorCode.ReactDevtoolsIsNotInstalled);
61 }
62
63 elementInspector.stdout.on("data", (data: string) => {
64 logger.info(data);
65 });
66 elementInspector.stderr.on("data", (data: string) => {
67 logger.error(data);
68 });
69 elementInspector.once("exit", () => {
70 elementInspector = undefined;
71 });
72 }
73}
74
75export class StopElementInspector extends Command {
76 codeName = "stopInspector";
77 label = "Stop Element Inspector";
78 requiresTrust = false;
79 requiresProject = false;
80
81 error = ErrorHelper.getInternalError(
82 InternalErrorCode.CommandFailed,
83 localize("ReactNativeStopElementInspector", "React Native: Stop Element Inspector"),
84 );
85
86 async baseFn(): Promise<void> {
87 elementInspector?.kill();
88 }
89}