Skip to content

Commit

Permalink
Merge pull request #20143 from storybookjs/tom/sb-982-add-using-chrom…
Browse files Browse the repository at this point in the history
…atic-to-telemetry

Telemetry: Add `chromatic` to addons list
  • Loading branch information
shilman authored Dec 9, 2022
2 parents 415d763 + 82ccbd9 commit f56de0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions code/lib/telemetry/src/get-chromatic-version.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
15 changes: 15 additions & 0 deletions code/lib/telemetry/src/get-chromatic-version.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions code/lib/telemetry/src/storybook-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions code/lib/telemetry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type EventType =

export interface Dependency {
version: string | undefined;
versionSpecifier?: string;
}

export interface StorybookAddon extends Dependency {
Expand Down

0 comments on commit f56de0d

Please sign in to comment.