Skip to content

Commit

Permalink
Add a timeout to the telemetry consent prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado committed Oct 26, 2020
1 parent 73ed19f commit 80ee444
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/hardhat-core/src/internal/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ async function main() {
process.stdout.isTTY === true
) {
telemetryConsent = await confirmTelemetryConsent();
writeTelemetryConsent(telemetryConsent);

if (telemetryConsent !== undefined) {
writeTelemetryConsent(telemetryConsent);
}
}

const analytics = await Analytics.getInstance(telemetryConsent);
Expand Down
42 changes: 27 additions & 15 deletions packages/hardhat-core/src/internal/cli/project-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const SAMPLE_PROJECT_DEPENDENCIES: Dependencies = {
ethers: "^5.0.0",
};

const TELEMETRY_CONSENT_TIMEOUT = 10000;

async function removeProjectDirIfPresent(projectRoot: string, dirName: string) {
const dirPath = path.join(projectRoot, dirName);
if (await fsExtra.pathExists(dirPath)) {
Expand Down Expand Up @@ -270,7 +272,9 @@ export async function createProject() {
if (hasConsentedTelemetry() === undefined) {
const telemetryConsent = await confirmTelemetryConsent();

writeTelemetryConsent(telemetryConsent);
if (telemetryConsent !== undefined) {
writeTelemetryConsent(telemetryConsent);
}
}

let shouldShowInstallationInstructions = true;
Expand Down Expand Up @@ -428,22 +432,30 @@ async function confirmRecommendedDepsInstallation(
return responses.shouldInstallPlugin;
}

export async function confirmTelemetryConsent(): Promise<boolean> {
const { default: enquirer } = await import("enquirer");
export async function confirmTelemetryConsent(): Promise<boolean | undefined> {
const enquirer = require("enquirer");

const { telemetryConsent } = await enquirer.prompt<{
telemetryConsent: boolean;
}>([
{
name: "telemetryConsent",
type: "confirm",
initial: true,
message:
"Help us improve Hardhat with anonymous crash reports & basic usage data?",
},
]);
const prompt = new enquirer.prompts.Confirm({
name: "telemetryConsent",
type: "confirm",
initial: true,
message:
"Help us improve Hardhat with anonymous crash reports & basic usage data?",
});

let timeout;
const timeoutPromise = new Promise((resolve) => {
timeout = setTimeout(resolve, TELEMETRY_CONSENT_TIMEOUT);
});

const result = await Promise.race([prompt.run(), timeoutPromise]);

clearTimeout(timeout);
if (result === undefined) {
await prompt.cancel();
}

return telemetryConsent;
return result;
}

async function installDependencies(
Expand Down

0 comments on commit 80ee444

Please sign in to comment.