Skip to content

Commit

Permalink
feat(scripts-storybook): make getMetadata throwable via config to be …
Browse files Browse the repository at this point in the history
…able to process stories glob for packages no longer present in the repo (microsoft#31226)
  • Loading branch information
Hotell authored and miroslavstastny committed Jun 14, 2024
1 parent 130d90c commit 58e3216
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
61 changes: 43 additions & 18 deletions scripts/storybook/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ function getPackageStoriesGlob(options) {
const projects = getAllProjects();

const excludeStoriesInsertionFromPackages = options.excludeStoriesInsertionFromPackages ?? [];
const projectMetadata = getMetadata(options.packageName, projects);
const projectMetadata = /** @type {NonNullable<ReturnType<typeof getMetadata>>} */ (
getMetadata(options.packageName, projects)
);

/** @type {{name:string;version:string;dependencies?:Record<string,string>}} */
const packageJson = JSON.parse(
Expand All @@ -228,24 +230,38 @@ function getPackageStoriesGlob(options) {
const rootOffset = offsetFromRoot(options.callerPath.replace(workspaceRoot, ''));
const packages = Object.keys(dependencies);

const result = packages
.filter(pkgName => projects.has(pkgName) && !excludeStoriesInsertionFromPackages.includes(pkgName))
.map(pkgName => {
const storiesGlob = '**/@(index.stories.@(ts|tsx)|*.stories.mdx)';
const pkgMetadata = getMetadata(pkgName, projects);
const result = packages.reduce((acc, pkgName) => {
if (!(pkgName.startsWith('@fluentui/') && !excludeStoriesInsertionFromPackages.includes(pkgName))) {
return acc;
}

if (fs.existsSync(path.resolve(workspaceRoot, pkgMetadata.root, 'stories'))) {
return `${rootOffset}${pkgMetadata.root}/stories/${storiesGlob}`;
}
const pkgMetadata = getMetadata(pkgName, projects, { throwIfNotFound: false });

// if defined package has stories project use that
const pkgMetadataStories = projects.get(`${pkgName}-stories`);
if (pkgMetadataStories) {
return `${rootOffset}${pkgMetadataStories.root}/src/${storiesGlob}`;
}
if (!pkgMetadata) {
return acc;
}

return `${rootOffset}${pkgMetadata.root}/src/${storiesGlob}`;
});
const storiesGlob = '**/@(index.stories.@(ts|tsx)|*.stories.mdx)';

// if defined package(project) has stories sibling project, that means we need to look for stories in sibling project as the original project doesn't have stories anymore
// @see https://github.com/microsoft/fluentui/issues/30516
const pkgMetadataStories = projects.get(`${pkgName}-stories`);
if (pkgMetadataStories) {
acc.push(`${rootOffset}${pkgMetadataStories.root}/src/${storiesGlob}`);
return acc;
}

const hasStoriesFolder = fs.existsSync(path.resolve(workspaceRoot, pkgMetadata.root, 'stories'));

if (hasStoriesFolder) {
acc.push(`${rootOffset}${pkgMetadata.root}/stories/${storiesGlob}`);
return acc;
}

acc.push(`${rootOffset}${pkgMetadata.root}/src/${storiesGlob}`);

return acc;
}, /** @type {string[]}*/ ([]));

return result;

Expand All @@ -258,11 +274,20 @@ function getPackageStoriesGlob(options) {
return getProjects(tree);
}

function getMetadata(/** @type {string}*/ packageName, /** @type {ReturnType<typeof getAllProjects>}*/ allProjects) {
function getMetadata(
/** @type {string}*/ packageName,
/** @type {ReturnType<typeof getAllProjects>}*/ allProjects,
/** @type {Partial<{throwIfNotFound:boolean}>}*/ _options,
) {
const { throwIfNotFound = true } = { ..._options };
const metadata = allProjects.get(packageName);

if (!metadata) {
throw new Error(`Project ${options.packageName} not found in workspace`);
if (throwIfNotFound) {
throw new Error(`Project "${packageName}" not found in workspace`);
}

return null;
}

return metadata;
Expand Down
5 changes: 5 additions & 0 deletions scripts/storybook/src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ describe(`utils`, () => {

expect(actual).not.toContain(expect.stringContaining('/react-text/stories/'));
});

// @TODO: Once we will have at least 1 project migrated to the new structure we can enable/implement this test
it.todo(
`should generate storybook stories string array of glob based on package.json#dependencies field pointing to sibling /stories project if it exists`,
);
});

describe(`#processBabelLoaderOptions`, () => {
Expand Down

0 comments on commit 58e3216

Please sign in to comment.