Skip to content

Commit a58fe68

Browse files
committed
Merge pull request #23199 from storybookjs/fix/parse-pnp-paths-in-metadata
CLI: Parse pnp paths in storybook metadata (cherry picked from commit 4f7cd07)
1 parent 05bebd9 commit a58fe68

File tree

3 files changed

+313
-174
lines changed

3 files changed

+313
-174
lines changed

code/lib/telemetry/src/get-framework-info.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import type { PackageJson, StorybookConfig } from '@storybook/types';
2+
import path from 'path';
3+
import { frameworkPackages } from '@storybook/core-common';
4+
import { cleanPaths } from './sanitize';
25
import { getActualPackageJson } from './package-json';
36

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

33-
export async function getFrameworkInfo(mainConfig: StorybookConfig) {
34-
const { framework: frameworkInput } = mainConfig;
36+
export const getFrameworkPackageName = (mainConfig?: StorybookConfig) => {
37+
const packageNameOrPath =
38+
typeof mainConfig?.framework === 'string' ? mainConfig.framework : mainConfig?.framework?.name;
39+
40+
if (!packageNameOrPath) {
41+
return null;
42+
}
43+
44+
const normalizedPath = path.normalize(packageNameOrPath).replace(new RegExp(/\\/, 'g'), '/');
3545

36-
if (!frameworkInput) return {};
46+
const knownFramework = Object.keys(frameworkPackages).find((pkg) => normalizedPath.endsWith(pkg));
47+
48+
return knownFramework || cleanPaths(packageNameOrPath).replace(/.*node_modules[\\/]/, '');
49+
};
50+
51+
export async function getFrameworkInfo(mainConfig: StorybookConfig) {
52+
if (!mainConfig.framework) return {};
3753

38-
const framework = typeof frameworkInput === 'string' ? { name: frameworkInput } : frameworkInput;
54+
const frameworkName = getFrameworkPackageName(mainConfig);
55+
if (!frameworkName) return {};
56+
const frameworkOptions =
57+
typeof mainConfig.framework === 'object' ? mainConfig.framework.options : {};
3958

40-
const frameworkPackageJson = await getActualPackageJson(framework.name);
59+
const frameworkPackageJson = await getActualPackageJson(frameworkName);
4160

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

4564
return {
46-
framework,
65+
framework: {
66+
name: frameworkName,
67+
options: frameworkOptions,
68+
},
4769
builder,
4870
renderer,
4971
};

code/lib/telemetry/src/sanitize.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ describe(`Errors Helpers`, () => {
7373

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

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

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

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

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

9595
expect(cleanPaths(errorMessage, `\\`)).toBe(
96-
`This path $SNIP\\${filePath} is a test $SNIP\\${filePath}`
96+
`This path should be sanitized $SNIP\\${filePath}`
9797
);
9898
mockCwd.mockRestore();
9999
}

0 commit comments

Comments
 (0)