microsoft/vscode-react-native

Public

mirrored from https://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/util/reactNativeCommand.ts

65lines · modeblame

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