Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix telemetry consent prompt #956

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/hardhat-core/src/internal/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,28 @@ async function main() {
showWarningIfNoSolidityConfig,
});

let telemetryConsent = hasConsentedTelemetry();
let telemetryConsent: boolean | undefined = hasConsentedTelemetry();

const isHelpCommand = hardhatArguments.help || taskName === TASK_HELP;
if (
telemetryConsent === undefined &&
!isHelpCommand &&
!isRunningOnCiServer()
!isRunningOnCiServer() &&
process.stdout.isTTY === true
) {
telemetryConsent = await confirmTelemetryConsent();
writeTelemetryConsent(telemetryConsent);

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

const analytics = await Analytics.getInstance(telemetryConsent);

Reporter.setConfigPath(config.paths.configFile);
Reporter.setEnabled(true);
if (telemetryConsent === true) {
Reporter.setEnabled(true);
}

const envExtenders = ctx.extendersManager.getExtenders();
const taskDefinitions = ctx.tasksDSL.getTaskDefinitions();
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