diff --git a/code/lib/telemetry/src/get-chromatic-version.test.ts b/code/lib/telemetry/src/get-chromatic-version.test.ts new file mode 100644 index 000000000000..9ee07f3d45e0 --- /dev/null +++ b/code/lib/telemetry/src/get-chromatic-version.test.ts @@ -0,0 +1,15 @@ +import { getChromaticVersionSpecifier } from './get-chromatic-version'; + +it('works for dependencies', () => { + expect(getChromaticVersionSpecifier({ dependencies: { chromatic: '^6.11.4' } })).toBe('^6.11.4'); +}); + +it('works for scripts', () => { + expect(getChromaticVersionSpecifier({ scripts: { chromatic: 'npx chromatic -t abc123' } })).toBe( + 'latest' + ); +}); + +it('fails otherwise', () => { + expect(getChromaticVersionSpecifier({})).toBeUndefined(); +}); diff --git a/code/lib/telemetry/src/get-chromatic-version.ts b/code/lib/telemetry/src/get-chromatic-version.ts new file mode 100644 index 000000000000..c9f8db06cfeb --- /dev/null +++ b/code/lib/telemetry/src/get-chromatic-version.ts @@ -0,0 +1,15 @@ +import type { PackageJson } from '@storybook/types'; + +export function getChromaticVersionSpecifier(packageJson: PackageJson) { + const dependency = + packageJson.dependencies?.chromatic || + packageJson.devDependencies?.chromatic || + packageJson.peerDependencies?.chromatic; + if (dependency) return dependency; + + // Chromatic isn't necessarily installed in dependencies, it can be run from npx + return packageJson.scripts && + Object.values(packageJson.scripts).find((s) => s?.match(/chromatic/)) + ? 'latest' + : undefined; +} diff --git a/code/lib/telemetry/src/storybook-metadata.ts b/code/lib/telemetry/src/storybook-metadata.ts index 54a98ce8917b..6973345880e6 100644 --- a/code/lib/telemetry/src/storybook-metadata.ts +++ b/code/lib/telemetry/src/storybook-metadata.ts @@ -13,6 +13,7 @@ import { getActualPackageVersion, getActualPackageVersions } from './package-jso import { getMonorepoType } from './get-monorepo-type'; import { cleanPaths } from './sanitize'; import { getFrameworkInfo } from './get-framework-info'; +import { getChromaticVersionSpecifier } from './get-chromatic-version'; export const metaFrameworks = { next: 'Next', @@ -123,6 +124,15 @@ export const computeStorybookMetadata = async ({ }); } + const chromaticVersionSpecifier = getChromaticVersionSpecifier(packageJson); + if (chromaticVersionSpecifier) { + addons.chromatic = { + version: undefined, + versionSpecifier: chromaticVersionSpecifier, + options: undefined, + }; + } + const addonVersions = await getActualPackageVersions(addons); addonVersions.forEach(({ name, version }) => { addons[name].version = version; diff --git a/code/lib/telemetry/src/types.ts b/code/lib/telemetry/src/types.ts index 6011282a8c53..d33b89677343 100644 --- a/code/lib/telemetry/src/types.ts +++ b/code/lib/telemetry/src/types.ts @@ -15,6 +15,7 @@ export type EventType = export interface Dependency { version: string | undefined; + versionSpecifier?: string; } export interface StorybookAddon extends Dependency {