Skip to content

Commit

Permalink
Merge pull request #23199 from storybookjs/fix/parse-pnp-paths-in-met…
Browse files Browse the repository at this point in the history
…adata

CLI: Parse pnp paths in storybook metadata
  • Loading branch information
shilman committed Jun 23, 2023
2 parents 1d61076 + 536ca99 commit 4f7cd07
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 175 deletions.
34 changes: 28 additions & 6 deletions code/lib/telemetry/src/get-framework-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { PackageJson, StorybookConfig } from '@storybook/types';
import path from 'path';
import { frameworkPackages } from '@storybook/core-common';
import { cleanPaths } from './sanitize';
import { getActualPackageJson } from './package-json';

const knownRenderers = [
Expand Down Expand Up @@ -30,20 +33,39 @@ function findMatchingPackage(packageJson: PackageJson, suffixes: string[]) {
return suffixes.map((suffix) => `@storybook/${suffix}`).find((pkg) => allDependencies[pkg]);
}

export async function getFrameworkInfo(mainConfig: StorybookConfig) {
const { framework: frameworkInput } = mainConfig;
export const getFrameworkPackageName = (mainConfig?: StorybookConfig) => {
const packageNameOrPath =
typeof mainConfig?.framework === 'string' ? mainConfig.framework : mainConfig?.framework?.name;

if (!packageNameOrPath) {
return null;
}

const normalizedPath = path.normalize(packageNameOrPath).replace(new RegExp(/\\/, 'g'), '/');

if (!frameworkInput) return {};
const knownFramework = Object.keys(frameworkPackages).find((pkg) => normalizedPath.endsWith(pkg));

return knownFramework || cleanPaths(packageNameOrPath).replace(/.*node_modules[\\/]/, '');
};

export async function getFrameworkInfo(mainConfig: StorybookConfig) {
if (!mainConfig.framework) return {};

const framework = typeof frameworkInput === 'string' ? { name: frameworkInput } : frameworkInput;
const frameworkName = getFrameworkPackageName(mainConfig);
if (!frameworkName) return {};
const frameworkOptions =
typeof mainConfig.framework === 'object' ? mainConfig.framework.options : {};

const frameworkPackageJson = await getActualPackageJson(framework.name);
const frameworkPackageJson = await getActualPackageJson(frameworkName);

const builder = findMatchingPackage(frameworkPackageJson, knownBuilders);
const renderer = findMatchingPackage(frameworkPackageJson, knownRenderers);

return {
framework,
framework: {
name: frameworkName,
options: frameworkOptions,
},
builder,
renderer,
};
Expand Down
8 changes: 4 additions & 4 deletions code/lib/telemetry/src/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ describe(`Errors Helpers`, () => {

const mockCwd = jest.spyOn(process, `cwd`).mockImplementation(() => cwdMockPath);

const errorMessage = `This path ${fullPath} is a test ${fullPath}`;
const errorMessage = `This path should be sanitized ${fullPath}`;

expect(cleanPaths(errorMessage, `/`)).toBe(
`This path $SNIP/${filePath} is a test $SNIP/${filePath}`
`This path should be sanitized $SNIP/${filePath}`
);
mockCwd.mockRestore();
}
Expand All @@ -90,10 +90,10 @@ describe(`Errors Helpers`, () => {

const mockCwd = jest.spyOn(process, `cwd`).mockImplementation(() => cwdMockPath);

const errorMessage = `This path ${fullPath} is a test ${fullPath}`;
const errorMessage = `This path should be sanitized ${fullPath}`;

expect(cleanPaths(errorMessage, `\\`)).toBe(
`This path $SNIP\\${filePath} is a test $SNIP\\${filePath}`
`This path should be sanitized $SNIP\\${filePath}`
);
mockCwd.mockRestore();
}
Expand Down

0 comments on commit 4f7cd07

Please sign in to comment.