microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
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 | |
| 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 | "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 | |
| 77 | export 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 | |