Add metadata.hasChromatic

This commit is contained in:
Tom Coleman 2022-12-07 17:20:14 +11:00
parent 670f512793
commit 4cf693c66d
4 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,13 @@
import { hasChromatic } from './has-chromatic';
it('works for dependencies', () => {
expect(hasChromatic({ dependencies: { chromatic: '^6.11.4' } })).toBe(true);
});
it('works for scripts', () => {
expect(hasChromatic({ scripts: { chromatic: 'npx chromatic -t abc123' } })).toBe(true);
});
it('fails otherwise', () => {
expect(hasChromatic({})).toBe(false);
});

View File

@ -0,0 +1,16 @@
import type { PackageJson } from '@storybook/types';
export function hasChromatic(packageJson: PackageJson) {
if (
packageJson.dependencies?.chromatic ||
packageJson.devDependencies?.chromatic ||
packageJson.peerDependencies?.chromatic
) {
return true;
}
// 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/))
);
}

View File

@ -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 { hasChromatic } from './has-chromatic';
export const metaFrameworks = {
next: 'Next',
@ -157,6 +158,7 @@ export const computeStorybookMetadata = async ({
...frameworkInfo,
storybookVersion,
storybookVersionSpecifier: storybookInfo.version,
hasChromatic: hasChromatic(packageJson),
language,
storybookPackages,
addons,

View File

@ -49,6 +49,7 @@ export type StorybookMetadata = {
hasStaticDirs?: boolean;
hasCustomWebpack?: boolean;
hasCustomBabel?: boolean;
hasChromatic?: boolean;
features?: StorybookConfig['features'];
refCount?: number;
};