Skip to content

Commit d124631

Browse files
committed
Send project type analytics hit
1 parent 2ba3fcb commit d124631

File tree

2 files changed

+28
-12
lines changed
  • v-next/hardhat

2 files changed

+28
-12
lines changed

v-next/hardhat/src/internal/cli/init/init.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as semver from "semver";
2424
import { findClosestHardhatConfig } from "../../config-loading.js";
2525
import { HARDHAT_NAME } from "../../constants.js";
2626
import { getHardhatVersion } from "../../utils/package.js";
27+
import { sendProjectTypeAnalytics } from "../telemetry/analytics/analytics.js";
2728

2829
import {
2930
getDevDependenciesInstallationCommand,
@@ -96,7 +97,10 @@ export async function initHardhat(options?: InitHardhatOptions): Promise<void> {
9697

9798
// Ask the user for the template to use for the project initialization
9899
// if it was not provided, and validate that it exists
99-
const template = await getTemplate(hardhatVersion, options?.template);
100+
const [template, projectTypeAnalyticsPromise] = await getTemplate(
101+
hardhatVersion,
102+
options?.template,
103+
);
100104

101105
// Create the package.json file if it does not exist
102106
// and validate that it is an esm package
@@ -112,7 +116,11 @@ export async function initHardhat(options?: InitHardhatOptions): Promise<void> {
112116

113117
// Print the commands to install the project dependencies
114118
// Run them only if the user opts-in to it
115-
await installProjectDependencies(workspace, template, options?.install);
119+
// Concurrently, await the analytics hit
120+
await Promise.all([
121+
installProjectDependencies(workspace, template, options?.install),
122+
projectTypeAnalyticsPromise,
123+
]);
116124

117125
showStarOnGitHubMessage();
118126
} catch (e) {
@@ -241,26 +249,34 @@ export async function getWorkspace(workspace?: string): Promise<string> {
241249
* NOTE: This function is exported for testing purposes
242250
*
243251
* @param template The name of the template to use for the project initialization.
244-
* @returns
252+
* @returns A tuple with two elements: the template and a promise with the analytics hit.
245253
*/
246254
export async function getTemplate(
247255
hardhatVersion: "hardhat-2" | "hardhat-3",
248256
template?: string,
249-
): Promise<Template> {
257+
): Promise<[Template, Promise<boolean>]> {
250258
const templates = await getTemplates(hardhatVersion);
251259

252260
// Ask the user for the template to use for the project initialization if it was not provided
253261
if (template === undefined) {
254262
template = await promptForTemplate(templates);
255263
}
256264

265+
const projectTypeAnalyticsPromise = sendProjectTypeAnalytics(
266+
hardhatVersion,
267+
template,
268+
);
269+
257270
// Validate that the template exists
258271
for (const t of templates) {
259272
if (t.name === template) {
260-
return t;
273+
return [t, projectTypeAnalyticsPromise];
261274
}
262275
}
263276

277+
// we wait for the GA hit before throwing
278+
await projectTypeAnalyticsPromise;
279+
264280
throw new HardhatError(HardhatError.ERRORS.CORE.GENERAL.TEMPLATE_NOT_FOUND, {
265281
template,
266282
});

v-next/hardhat/test/internal/cli/init/init.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe("getTemplate", () => {
9090
);
9191
});
9292
it("should return the provided template", async () => {
93-
const template = await getTemplate("hardhat-3", "mocha-ethers");
93+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
9494
assert.equal(template.name, "mocha-ethers");
9595
});
9696
});
@@ -284,7 +284,7 @@ describe("copyProjectFiles", () => {
284284

285285
describe("when force is true", () => {
286286
it("should copy the template files to the workspace and overwrite existing files", async () => {
287-
const template = await getTemplate("hardhat-3", "mocha-ethers");
287+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
288288
// Create template files with "some content" in the workspace
289289
const workspaceFiles = template.files.map(
290290
relativeTemplateToWorkspacePath,
@@ -303,7 +303,7 @@ describe("copyProjectFiles", () => {
303303
}
304304
});
305305
it("should copy the .gitignore file correctly", async () => {
306-
const template = await getTemplate("hardhat-3", "mocha-ethers");
306+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
307307
// Copy the template files to the workspace
308308
await copyProjectFiles(process.cwd(), template, true);
309309
// Check that the .gitignore exists but gitignore does not
@@ -319,7 +319,7 @@ describe("copyProjectFiles", () => {
319319
});
320320
describe("when force is false", () => {
321321
it("should copy the template files to the workspace and NOT overwrite existing files", async () => {
322-
const template = await getTemplate("hardhat-3", "mocha-ethers");
322+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
323323
// Create template files with "some content" in the workspace
324324
const workspaceFiles = template.files.map(
325325
relativeTemplateToWorkspacePath,
@@ -338,7 +338,7 @@ describe("copyProjectFiles", () => {
338338
}
339339
});
340340
it("should copy the .gitignore file correctly", async () => {
341-
const template = await getTemplate("hardhat-3", "mocha-ethers");
341+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
342342
// Copy the template files to the workspace
343343
await copyProjectFiles(process.cwd(), template, false);
344344
// Check that the .gitignore exists but gitignore does not
@@ -396,7 +396,7 @@ describe("installProjectDependencies", async () => {
396396
}
397397

398398
it("should not install any template dependencies if the user opts-out of the installation", async () => {
399-
const template = await getTemplate("hardhat-3", "mocha-ethers");
399+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
400400
await writeUtf8File("package.json", JSON.stringify({ type: "module" }));
401401
await installProjectDependencies(process.cwd(), template, false, false);
402402
assert.ok(!(await exists("node_modules")), "node_modules should not exist");
@@ -412,7 +412,7 @@ describe("installProjectDependencies", async () => {
412412
process.env.GITHUB_HEAD_REF?.startsWith("changeset-release/"),
413413
},
414414
async () => {
415-
const template = await getTemplate("hardhat-3", "mocha-ethers");
415+
const [template] = await getTemplate("hardhat-3", "mocha-ethers");
416416
await writeUtf8File(
417417
"package.json",
418418
JSON.stringify({

0 commit comments

Comments
 (0)