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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Portable stories: Add telemetry #26764
Open
shilman
wants to merge
1
commit into
next
Choose a base branch
from
shilman/portable-stories-stats
base: next
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// composeStory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// composeStory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// composeStories; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
composeStory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// composeStory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { join } from 'path'; | ||
|
||
import { getPortableStoriesFiles } from './get-portable-stories-usage'; | ||
|
||
describe('getPortableStoriesFiles', () => { | ||
it('should ignores node_modules, non-source files', async () => { | ||
const base = join(__dirname, '__fixtures__'); | ||
const usage = (await getPortableStoriesFiles(base)).map((f) => f.replace(base, '')); | ||
expect(usage).toMatchInlineSnapshot(` | ||
[ | ||
"/b.js", | ||
"/a.js", | ||
"/foo/a.js", | ||
"/.storybook/a.js", | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { readFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
import { glob } from 'glob'; | ||
|
||
import { createFileSystemCache, resolvePathInStorybookCache } from '@storybook/core-common'; | ||
|
||
const cache = createFileSystemCache({ | ||
basePath: resolvePathInStorybookCache('portable-stories'), | ||
ns: 'storybook', | ||
ttl: 24 * 60 * 60 * 1000, | ||
}); | ||
|
||
export const containsPortableStories = async (filename: string) => { | ||
if (/sb\-preview\/runtime.m?js$/i.test(filename)) return null; | ||
|
||
const fileContent = await readFile(filename, 'utf-8'); | ||
const contains = /composeStor[y|ies]/g.test(fileContent); | ||
return contains ? filename : null; | ||
}; | ||
|
||
export const getPortableStoriesFiles = async (base: string) => { | ||
const files = await glob('**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}', { | ||
ignore: ['**/node_modules/**', '**/storybook-static/**', '**/dist/**'], | ||
dot: true, | ||
cwd: base, | ||
}); | ||
|
||
const hits = []; | ||
const chunkSize = 10; | ||
for (let i = 0; i < files.length; i += chunkSize) { | ||
const chunk = files.slice(i, i + chunkSize); | ||
const results = ( | ||
await Promise.all(chunk.map((f: string) => containsPortableStories(join(base, f)))) | ||
).filter(Boolean); | ||
if (results.length > 0) { | ||
hits.push(...results); | ||
} | ||
} | ||
return hits as string[]; | ||
}; | ||
|
||
const CACHE_KEY = 'portableStories'; | ||
export const getPortableStoriesFileCount = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to keep in mind that people might have helper functions that wrap composeStory/ies and therefore the count won't be accurate |
||
let cached = await cache.get(CACHE_KEY); | ||
if (!cached) { | ||
const files = await getPortableStoriesFiles(process.cwd()); | ||
cached = { usage: files.length }; | ||
await cache.set(CACHE_KEY, cached); | ||
} | ||
return cached.usage; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to get values from user's gitignore, but I don't know how complex it is as projects might have multiple gitignore files