microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix-ts-error1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/elementInspector.ts

91lines · 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 "ReactNativeRunElementInspector",
31 "React Native: Run Element Inspector",
32 );
33
34 async baseFn(): Promise<void> {
35 const logger = OutputChannelLogger.getMainChannel();
36
37 void TipNotificationService.getInstance().setKnownDateForFeatureById("elementInspector");
38
39 if (elementInspector) {
40 logger.info(
41 localize(
42 "AnotherElementInspectorAlreadyRun",
43 "Another element inspector already run",
44 ),
45 );
46
47 return;
48 }
49 // Remove the following env variables to prevent running electron app in node mode.
50 // https://github.com/microsoft/vscode/issues/3011#issuecomment-184577502
51 // eslint-disable-next-line @typescript-eslint/no-unused-vars
52 const { ATOM_SHELL_INTERNAL_RUN_AS_NODE, ELECTRON_RUN_AS_NODE, ...env } = process.env;
53 const command = HostPlatform.getNpmCliCommand("react-devtools");
54
55 elementInspector = child_process.spawn(command, [], {
56 env,
57 shell: true,
58 });
59
60 if (!elementInspector.pid) {
61 elementInspector = undefined;
62 throw ErrorHelper.getInternalError(InternalErrorCode.ReactDevtoolsIsNotInstalled);
63 }
64
65 elementInspector.stdout.on("data", (data: string) => {
66 logger.info(data);
67 });
68 elementInspector.stderr.on("data", (data: string) => {
69 logger.error(data);
70 });
71 elementInspector.once("exit", () => {
72 elementInspector = undefined;
73 });
74 }
75}
76
77export class StopElementInspector extends Command {
78 codeName = "stopInspector";
79 label = "Stop Element Inspector";
80 requiresTrust = false;
81 requiresProject = false;
82
83 error = ErrorHelper.getInternalError(
84 InternalErrorCode.CommandFailed,
85 "React Native: Stop Element Inspector",
86 );
87
88 async baseFn(): Promise<void> {
89 elementInspector?.kill();
90 }
91}
92