microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commands/util/reactNativeCommand.ts
65lines · 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 assert = require("assert"); |
| 5 | import * as vscode from "vscode"; |
| 6 | import { ReactNativeProjectHelper } from "../../../common/reactNativeProjectHelper"; |
| 7 | import { TelemetryHelper } from "../../../common/telemetryHelper"; |
| 8 | import { OutputChannelLogger } from "../../log/OutputChannelLogger"; |
| 9 | import { SettingsHelper } from "../../settingsHelper"; |
| 10 | import { Command } from "./command"; |
| 11 | |
| 12 | export abstract class ReactNativeCommand<ArgT extends unknown[] = never[]> extends Command<ArgT> { |
| 13 | /** Execute base command with some telemetry */ |
| 14 | async executeLocally(...args: ArgT): Promise<void> { |
| 15 | await this.onBeforeExecute(...args); |
| 16 | await this.executeInContext((this.baseFn as any).bind(this, ...args)); |
| 17 | } |
| 18 | |
| 19 | /** Execute some task before RN telemetry */ |
| 20 | protected async onBeforeExecute(...args: ArgT): Promise<void> { |
| 21 | await super.onBeforeExecute(...args); |
| 22 | } |
| 23 | |
| 24 | protected createHandler(fn = this.baseFn.bind(this)): (...args: ArgT) => Promise<void> { |
| 25 | return super.createHandler(async (...args: ArgT) => { |
| 26 | await this.executeInContext((fn as any).bind(this, ...args)); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | private async executeInContext(operation: () => Promise<void>) { |
| 31 | assert(this.project); |
| 32 | |
| 33 | const logger = OutputChannelLogger.getMainChannel(); |
| 34 | const projectRoot = SettingsHelper.getReactNativeProjectRoot( |
| 35 | this.project.getWorkspaceFolder().uri.fsPath, |
| 36 | ); |
| 37 | const isRNProject = await ReactNativeProjectHelper.isReactNativeProject(projectRoot); |
| 38 | |
| 39 | logger.debug(`Command palette: run project ${projectRoot} in context`); |
| 40 | |
| 41 | await TelemetryHelper.generate( |
| 42 | "RNCommand", |
| 43 | { |
| 44 | platform: { |
| 45 | value: this.platform, |
| 46 | isPii: false, |
| 47 | }, |
| 48 | }, |
| 49 | async generator => { |
| 50 | generator.add("command", this.codeName, false); |
| 51 | generator.add("isRNProject", isRNProject, false); |
| 52 | |
| 53 | if (!isRNProject) { |
| 54 | void vscode.window.showErrorMessage( |
| 55 | `${projectRoot} workspace is not a React Native project.`, |
| 56 | ); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | logger.setFocusOnLogChannel(); |
| 61 | await operation(); |
| 62 | }, |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | |