|
| 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 | + * Get the user's telemetry consent. If already provided, returns the answer. |
| 19 | + * If not, prompts the user. |
| 20 | + * Consent is only asked in interactive environments. |
| 21 | + * @returns True if the user consents to telemetry, false otherwise. |
| 22 | + */ |
| 23 | +export async function getTelemetryConsent(): Promise<boolean> { |
| 24 | + if (canTelemetryBeEnabled() === false) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + const telemetryConsentFilePath = await getTelemetryConsentFilePath(); |
| 29 | + |
| 30 | + if (await exists(telemetryConsentFilePath)) { |
| 31 | + // Telemetry consent was already provided, hence return the answer |
| 32 | + return (await readJsonFile<TelemetryConsent>(telemetryConsentFilePath)) |
| 33 | + .consent; |
| 34 | + } |
| 35 | + |
| 36 | + // Telemetry consent not provided yet, ask for it |
| 37 | + return requestTelemetryConsent(); |
| 38 | +} |
| 39 | + |
| 40 | +function canTelemetryBeEnabled(): boolean { |
| 41 | + return ( |
| 42 | + !isCi() && |
| 43 | + process.stdout.isTTY === true && |
| 44 | + process.env.HARDHAT_DISABLE_TELEMETRY_PROMPT !== "true" |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +async function getTelemetryConsentFilePath() { |
| 49 | + const configDir = await getConfigDir(); |
| 50 | + return path.join(configDir, "telemetry-consent.json"); |
| 51 | +} |
| 52 | + |
| 53 | +async function requestTelemetryConsent(): Promise<boolean> { |
| 54 | + const consent = await confirmTelemetryConsent(); |
| 55 | + |
| 56 | + if (consent === undefined) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // Store user's consent choice |
| 61 | + await writeJsonFile(await getTelemetryConsentFilePath(), { consent }); |
| 62 | + |
| 63 | + // TODO: this will be enabled in a following PR as soon as the function to send telemetry is implemented |
| 64 | + // const subprocessFilePath = path.join( |
| 65 | + // path.dirname(fileURLToPath(import.meta.url)), |
| 66 | + // "report-telemetry-consent.js", |
| 67 | + // ); |
| 68 | + // await spawnDetachedSubProcess(subprocessFilePath, [consent ? "yes" : "no"]); |
| 69 | + |
| 70 | + return consent; |
| 71 | +} |
| 72 | + |
| 73 | +async function confirmTelemetryConsent(): Promise<boolean | undefined> { |
| 74 | + return confirmationPromptWithTimeout( |
| 75 | + "telemetryConsent", |
| 76 | + "Help us improve Hardhat with anonymous crash reports & basic usage data?", |
| 77 | + ); |
| 78 | +} |
0 commit comments