From cc81dadb143e90303d7c945a2f8dd809a2fd44a6 Mon Sep 17 00:00:00 2001 From: Shaun Lloyd Date: Fri, 8 Dec 2023 11:20:13 -0500 Subject: [PATCH] Add telemetry for successful scaffolding --- code/lib/cli/src/initiate.ts | 2 +- code/lib/cli/src/scaffold-new-project.ts | 26 ++++++++++++++++++------ code/lib/telemetry/src/types.ts | 1 + 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/code/lib/cli/src/initiate.ts b/code/lib/cli/src/initiate.ts index d5acccbfd36a..5217bca197aa 100644 --- a/code/lib/cli/src/initiate.ts +++ b/code/lib/cli/src/initiate.ts @@ -259,7 +259,7 @@ async function doInitiate( // Check if the current directory is empty. if (options.force !== true && currentDirectoryIsEmpty(packageManager.type)) { // Prompt the user to create a new project from our list. - await scaffoldNewProject(packageManager.type); + await scaffoldNewProject(packageManager.type, options); if (process.env.IN_STORYBOOK_SANDBOX === 'true' || process.env.CI === 'true') { packageManager.addPackageResolutions({ diff --git a/code/lib/cli/src/scaffold-new-project.ts b/code/lib/cli/src/scaffold-new-project.ts index eabd944373b8..d33d82a32d7a 100644 --- a/code/lib/cli/src/scaffold-new-project.ts +++ b/code/lib/cli/src/scaffold-new-project.ts @@ -5,10 +5,13 @@ import { readdirSync, remove } from 'fs-extra'; import prompts from 'prompts'; import dedent from 'ts-dedent'; +import { telemetry } from '@storybook/telemetry'; + import { GenerateNewProjectOnInitError } from '@storybook/core-events/server-errors'; import { logger } from '@storybook/node-logger'; import type { PackageManagerName } from './js-package-manager'; +import type { CommandOptions } from './generators/types'; type CoercedPackageManagerName = 'npm' | 'yarn' | 'pnpm'; @@ -108,7 +111,10 @@ const buildProjectDisplayNameForPrint = ({ displayName }: SupportedProject) => { * * @param packageManager The package manager to use. */ -export const scaffoldNewProject = async (packageManager: PackageManagerName) => { +export const scaffoldNewProject = async ( + packageManager: PackageManagerName, + { disableTelemetry }: CommandOptions +) => { const packageManagerName = packageManagerToCoercedName(packageManager); logger.plain( @@ -135,7 +141,7 @@ export const scaffoldNewProject = async (packageManager: PackageManagerName) => let projectStrategy; if (process.env.STORYBOOK_INIT_EMPTY_TYPE) { - projectStrategy = SUPPORTED_PROJECTS[process.env.STORYBOOK_INIT_EMPTY_TYPE]; + projectStrategy = process.env.STORYBOOK_INIT_EMPTY_TYPE; } if (!projectStrategy) { @@ -152,11 +158,12 @@ export const scaffoldNewProject = async (packageManager: PackageManagerName) => { onCancel: () => process.exit(0) } ); - projectStrategy = SUPPORTED_PROJECTS[project]; + projectStrategy = project; } - const projectDisplayName = buildProjectDisplayNameForPrint(projectStrategy); - const createScript = projectStrategy.createScript[packageManagerName]; + const projectStrategyConfig = SUPPORTED_PROJECTS[projectStrategy]; + const projectDisplayName = buildProjectDisplayNameForPrint(projectStrategyConfig); + const createScript = projectStrategyConfig.createScript[packageManagerName]; logger.line(1); logger.plain( @@ -182,7 +189,14 @@ export const scaffoldNewProject = async (packageManager: PackageManagerName) => throw new GenerateNewProjectOnInitError({ error: e, packageManager: packageManagerName, - projectType: projectDisplayName, + projectType: projectStrategy, + }); + } + + if (!disableTelemetry) { + telemetry('scaffolded-empty', { + packageManager: packageManagerName, + projectType: projectStrategy, }); } diff --git a/code/lib/telemetry/src/types.ts b/code/lib/telemetry/src/types.ts index 35266814dff7..8f091703bcae 100644 --- a/code/lib/telemetry/src/types.ts +++ b/code/lib/telemetry/src/types.ts @@ -9,6 +9,7 @@ export type EventType = | 'build' | 'upgrade' | 'init' + | 'scaffolded-empty' | 'browser' | 'canceled' | 'error'