Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Parse pnp paths in storybook metadata #23199

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'), '/');
yannbf marked this conversation as resolved.
Show resolved Hide resolved

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