From 58e321620bfbdd833f48aa7f4fbf5057b540133b Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Thu, 2 May 2024 15:16:39 +0200 Subject: [PATCH] feat(scripts-storybook): make getMetadata throwable via config to be able to process stories glob for packages no longer present in the repo (#31226) --- scripts/storybook/src/utils.js | 61 ++++++++++++++++++++--------- scripts/storybook/src/utils.spec.js | 5 +++ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/scripts/storybook/src/utils.js b/scripts/storybook/src/utils.js index 4f6547470d6dc3..eaa9344c4e9abc 100644 --- a/scripts/storybook/src/utils.js +++ b/scripts/storybook/src/utils.js @@ -217,7 +217,9 @@ function getPackageStoriesGlob(options) { const projects = getAllProjects(); const excludeStoriesInsertionFromPackages = options.excludeStoriesInsertionFromPackages ?? []; - const projectMetadata = getMetadata(options.packageName, projects); + const projectMetadata = /** @type {NonNullable>} */ ( + getMetadata(options.packageName, projects) + ); /** @type {{name:string;version:string;dependencies?:Record}} */ const packageJson = JSON.parse( @@ -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; @@ -258,11 +274,20 @@ function getPackageStoriesGlob(options) { return getProjects(tree); } - function getMetadata(/** @type {string}*/ packageName, /** @type {ReturnType}*/ allProjects) { + function getMetadata( + /** @type {string}*/ packageName, + /** @type {ReturnType}*/ 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; diff --git a/scripts/storybook/src/utils.spec.js b/scripts/storybook/src/utils.spec.js index 75c6503ca7a16c..4c20d2809d9c7b 100644 --- a/scripts/storybook/src/utils.spec.js +++ b/scripts/storybook/src/utils.spec.js @@ -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`, () => {