From 3ca1ef7194b944f94578ab4dc01572fd732c5fdf Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 16 Jun 2023 01:42:09 +0800 Subject: [PATCH] Merge pull request #23092 from storybookjs/shilman/count-onboarding-stories Telemetry: Count onboarding stories (cherry picked from commit 9f151fc64b7194a627cf450f3bc5e1b473c37fb5) --- .../src/utils/summarizeIndex.test.ts | 70 +++++++++++++++++++ .../core-server/src/utils/summarizeIndex.ts | 26 +++++-- 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/code/lib/core-server/src/utils/summarizeIndex.test.ts b/code/lib/core-server/src/utils/summarizeIndex.test.ts index de2a7d64eb8b..b58516b912ca 100644 --- a/code/lib/core-server/src/utils/summarizeIndex.test.ts +++ b/code/lib/core-server/src/utils/summarizeIndex.test.ts @@ -139,6 +139,66 @@ describe('summarizeIndex', () => { "exampleDocsCount": 3, "exampleStoryCount": 8, "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, + "pageStoryCount": 0, + "playStoryCount": 0, + "storiesMdxCount": 0, + "storyCount": 0, + "version": 4, + } + `); + }); + it('onboarding stories', () => { + expect( + summarizeIndex({ + v: 4, + entries: { + 'example-introduction--docs': { + id: 'example-introduction--docs', + title: 'Example/Introduction', + name: 'Docs', + importPath: './src/stories/Introduction.mdx', + storiesImports: [], + type: 'docs', + tags: ['docs'], + }, + 'example-button--docs': { + id: 'example-button--docs', + title: 'Example/Button', + name: 'Docs', + importPath: './src/stories/Button.stories.ts', + type: 'docs', + tags: ['autodocs', 'docs'], + storiesImports: [], + }, + 'example-button--primary': { + id: 'example-button--primary', + title: 'Example/Button', + name: 'Primary', + importPath: './src/stories/Button.stories.ts', + tags: ['autodocs', 'story'], + type: 'story', + }, + 'example-button--warning': { + id: 'example-button--warning', + title: 'Example/Button', + name: 'Warning', + importPath: './src/stories/Button.stories.ts', + tags: ['autodocs', 'story'], + type: 'story', + }, + }, + }) + ).toMatchInlineSnapshot(` + Object { + "autodocsCount": 0, + "componentCount": 0, + "exampleDocsCount": 2, + "exampleStoryCount": 1, + "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 1, "pageStoryCount": 0, "playStoryCount": 0, "storiesMdxCount": 0, @@ -193,6 +253,8 @@ describe('summarizeIndex', () => { "exampleDocsCount": 0, "exampleStoryCount": 0, "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, "storiesMdxCount": 0, @@ -248,6 +310,8 @@ describe('summarizeIndex', () => { "exampleDocsCount": 0, "exampleStoryCount": 2, "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, "pageStoryCount": 1, "playStoryCount": 1, "storiesMdxCount": 0, @@ -303,6 +367,8 @@ describe('summarizeIndex', () => { "exampleDocsCount": 0, "exampleStoryCount": 0, "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, "storiesMdxCount": 1, @@ -359,6 +425,8 @@ describe('summarizeIndex', () => { "exampleDocsCount": 1, "exampleStoryCount": 2, "mdxCount": 0, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, "storiesMdxCount": 0, @@ -408,6 +476,8 @@ describe('summarizeIndex', () => { "exampleDocsCount": 1, "exampleStoryCount": 0, "mdxCount": 2, + "onboardingDocsCount": 0, + "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, "storiesMdxCount": 0, diff --git a/code/lib/core-server/src/utils/summarizeIndex.ts b/code/lib/core-server/src/utils/summarizeIndex.ts index 601455bcfcba..091038cdaccf 100644 --- a/code/lib/core-server/src/utils/summarizeIndex.ts +++ b/code/lib/core-server/src/utils/summarizeIndex.ts @@ -7,11 +7,12 @@ const PAGE_REGEX = /(page|screen)/i; export const isPageStory = (storyId: string) => PAGE_REGEX.test(storyId); /** - * Filter out example stories that are generated by the CLI + * Is this story generated by the CLI */ -const isExampleEntry = (entry: IndexEntry) => { - return [ +const isCLIExampleEntry = (entry: IndexEntry) => + [ 'example-introduction--docs', + 'configure-your-project--docs', 'example-button--docs', 'example-button--primary', 'example-button--secondary', @@ -23,12 +24,22 @@ const isExampleEntry = (entry: IndexEntry) => { 'example-page--logged-in', 'example-page--logged-out', ].includes(entry.id); -}; + +/** + * Is this story part of the CLI generated examples, + * including user-created stories in those files + */ +const isAnyExampleEntry = (entry: IndexEntry) => + entry.id.startsWith('example-button--') || + entry.id.startsWith('example-header--') || + entry.id.startsWith('example-page--'); export function summarizeIndex(storyIndex: StoryIndex) { let storyCount = 0; const componentTitles = new Set(); let exampleStoryCount = 0; + let onboardingStoryCount = 0; + let onboardingDocsCount = 0; let exampleDocsCount = 0; let pageStoryCount = 0; let playStoryCount = 0; @@ -36,9 +47,12 @@ export function summarizeIndex(storyIndex: StoryIndex) { let storiesMdxCount = 0; let mdxCount = 0; Object.values(storyIndex.entries).forEach((entry) => { - if (isExampleEntry(entry)) { + if (isCLIExampleEntry(entry)) { if (entry.type === 'story') exampleStoryCount += 1; if (entry.type === 'docs') exampleDocsCount += 1; + } else if (isAnyExampleEntry(entry)) { + if (entry.type === 'story') onboardingStoryCount += 1; + if (entry.type === 'docs') onboardingDocsCount += 1; } else if (entry.type === 'story') { storyCount += 1; componentTitles.add(entry.title); @@ -69,6 +83,8 @@ export function summarizeIndex(storyIndex: StoryIndex) { mdxCount, exampleStoryCount, exampleDocsCount, + onboardingStoryCount, + onboardingDocsCount, version: storyIndex.v, }; }