microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
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 | |
| 4 | import * as child_process from "child_process"; |
| 5 | import * as nls from "vscode-nls"; |
| 6 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 7 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 8 | import { TipNotificationService } from "../services/tipsNotificationsService/tipsNotificationService"; |
| 9 | import { HostPlatform } from "../../common/hostPlatform"; |
| 10 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 11 | import { Command } from "./util/command"; |
| 12 | |
| 13 | nls.config({ |
| 14 | messageFormat: nls.MessageFormat.bundle, |
| 15 | bundleFormat: nls.BundleFormat.standalone, |
| 16 | })(); |
| 17 | const localize = nls.loadMessageBundle(); |
| 18 | |
| 19 | // #todo!> commands should not maintain state |
| 20 | let elementInspector: child_process.ChildProcess | undefined; |
| 21 | |
| 22 | export 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 | |
| 75 | export 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 | } |