|
| 1 | +import path from "node:path"; |
| 2 | + |
| 3 | +import { getConfigDir } from "@ignored/hardhat-vnext-core/global-dir"; |
| 4 | +import { isCi } from "@ignored/hardhat-vnext-utils/ci"; |
| 5 | +import { |
| 6 | + exists, |
| 7 | + readJsonFile, |
| 8 | + writeJsonFile, |
| 9 | +} from "@ignored/hardhat-vnext-utils/fs"; |
| 10 | + |
| 11 | +import { confirmationPromptWithTimeout } from "../prompt/prompt.js"; |
| 12 | + |
| 13 | +interface TelemetryConsent { |
| 14 | + consent: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Ensure that the user's telemetry consent is set. If the consent is already provided, returns the answer. |
| 19 | + * If not, prompts the user to provide it. |
| 20 | + * Consent is only asked in interactive environments. |
| 21 | + * |
| 22 | + * @returns True if the user consents to telemetry and if current environment supports telemetry, false otherwise. |
| 23 | + */ |
| 24 | +export async function ensureTelemetryConsent(): Promise<boolean> { |
| 25 | + if (!isTelemetryAllowedInEnvironment()) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + const consent = await getTelemetryConsent(); |
| 30 | + if (consent !== undefined) { |
| 31 | + return consent; |
| 32 | + } |
| 33 | + |
| 34 | + // Telemetry consent not provided yet, ask for it |
| 35 | + return requestTelemetryConsent(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Checks whether telemetry is supported in the current environment and whether the user has provided consent. |
| 40 | + * |
| 41 | + * @returns True if the user consents to telemetry and if current environment supports telemetry, false otherwise. |
| 42 | + */ |
| 43 | +export async function isTelemetryAllowed(): Promise<boolean> { |
| 44 | + if (!isTelemetryAllowedInEnvironment()) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + const consent = await getTelemetryConsent(); |
| 49 | + return consent !== undefined ? consent : false; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Retrieves the user's telemetry consent status from the consent file. |
| 54 | + * |
| 55 | + * @returns True if the user consents to telemetry, false if they do not consent, |
| 56 | + * and undefined if no consent has been provided. |
| 57 | + */ |
| 58 | +export async function getTelemetryConsent(): Promise<boolean | undefined> { |
| 59 | + const telemetryConsentFilePath = await getTelemetryConsentFilePath(); |
| 60 | + |
| 61 | + if (await exists(telemetryConsentFilePath)) { |
| 62 | + // Telemetry consent was already provided, hence return the answer |
| 63 | + return (await readJsonFile<TelemetryConsent>(telemetryConsentFilePath)) |
| 64 | + .consent; |
| 65 | + } |
| 66 | + |
| 67 | + return undefined; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Determines if telemetry is allowed in the current environment. |
| 72 | + * This function checks various environmental factors to decide if telemetry data can be collected. |
| 73 | + * It verifies that the environment is not a continuous integration (CI) environment, that the terminal is interactive, |
| 74 | + * and that telemetry has not been explicitly disabled through an environment variable. |
| 75 | + * |
| 76 | + * @returns True if telemetry is allowed in the environment, false otherwise. |
| 77 | + */ |
| 78 | +export function isTelemetryAllowedInEnvironment(): boolean { |
| 79 | + return ( |
| 80 | + (!isCi() && |
| 81 | + process.stdout.isTTY === true && |
| 82 | + process.env.HARDHAT_DISABLE_TELEMETRY_PROMPT !== "true") || |
| 83 | + process.env.HARDHAT_ENABLE_TELEMETRY_IN_TEST === "true" // Used in tests to force telemetry execution |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +async function getTelemetryConsentFilePath() { |
| 88 | + const configDir = await getConfigDir(); |
| 89 | + return path.join(configDir, "telemetry-consent.json"); |
| 90 | +} |
| 91 | + |
| 92 | +async function requestTelemetryConsent(): Promise<boolean> { |
| 93 | + const consent = await confirmTelemetryConsent(); |
| 94 | + |
| 95 | + if (consent === undefined) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + // Store user's consent choice |
| 100 | + await writeJsonFile(await getTelemetryConsentFilePath(), { consent }); |
| 101 | + |
| 102 | + // TODO: this will be enabled in a following PR as soon as the function to send telemetry is implemented |
| 103 | + // const subprocessFilePath = path.join( |
| 104 | + // path.dirname(fileURLToPath(import.meta.url)), |
| 105 | + // "report-telemetry-consent.js", |
| 106 | + // ); |
| 107 | + // await spawnDetachedSubProcess(subprocessFilePath, [consent ? "yes" : "no"]); |
| 108 | + |
| 109 | + return consent; |
| 110 | +} |
| 111 | + |
| 112 | +async function confirmTelemetryConsent(): Promise<boolean | undefined> { |
| 113 | + return confirmationPromptWithTimeout( |
| 114 | + "telemetryConsent", |
| 115 | + "Help us improve Hardhat with anonymous crash reports & basic usage data?", |
| 116 | + ); |
| 117 | +} |
0 commit comments