-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Core: Built-in static stories.json
support
#14945
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4d78fc8
CsfTools: Read/parse/write
shilman d1d61be
Build stories.json in production
shilman 937b48c
CsfFile: Extract basic parameters
shilman 4a59ea7
Built-in stories.json extraction in dev server (on startup)
shilman 06851d2
CsfFile: Respond to code review (WIP)
shilman 343f7fd
Core: Add fileName to built-in stories.json extraction
shilman 7bce1ae
Merge branch 'next' into feat/built-in-extract
shilman 2e95b7b
Fix deepscan
shilman 9250ca7
Add sbmodern
shilman 793b101
Merge branch 'next' into feat/built-in-extract
shilman 76e9714
Build: Increase CI `build` step to XL
shilman e06bf38
Merge branch 'build/increase-build-step-to-xl' into feat/built-in-ext…
shilman cc4e739
Add option to skip stories.json generation
shilman 1911420
CsfFile: Fix parsing for angular-cli files
shilman 8911a59
Core: Fix skipping files for stories.json extraction, better error lo…
shilman 02e7491
CsfFile: Fix case with no meta export, no title
shilman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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
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,81 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import glob from 'globby'; | ||
import { logger } from '@storybook/node-logger'; | ||
import { resolvePathInStorybookCache, Options } from '@storybook/core-common'; | ||
import { readCsf } from '@storybook/csf-tools'; | ||
|
||
interface ExtractedStory { | ||
id: string; | ||
kind: string; | ||
name: string; | ||
parameters: Record<string, any>; | ||
} | ||
|
||
type ExtractedStories = Record<string, ExtractedStory>; | ||
|
||
export async function extractStoriesJson( | ||
ouputFile: string, | ||
storiesGlobs: string[], | ||
configDir: string | ||
) { | ||
if (!storiesGlobs) { | ||
throw new Error('No stories glob'); | ||
} | ||
const storyFiles: string[] = []; | ||
await Promise.all( | ||
storiesGlobs.map(async (storiesGlob) => { | ||
const files = await glob(path.join(configDir, storiesGlob)); | ||
storyFiles.push(...files); | ||
}) | ||
); | ||
logger.info(`⚙️ Processing ${storyFiles.length} story files from ${storiesGlobs}`); | ||
|
||
const stories: ExtractedStories = {}; | ||
await Promise.all( | ||
storyFiles.map(async (fileName) => { | ||
const ext = path.extname(fileName); | ||
if (!['.js', '.jsx', '.ts', '.tsx'].includes(ext)) { | ||
logger.info(`skipping ${fileName}`); | ||
return; | ||
} | ||
try { | ||
const csf = (await readCsf(fileName)).parse(); | ||
csf.stories.forEach((story) => { | ||
stories[story.id] = { | ||
...story, | ||
kind: csf.meta.title, | ||
parameters: { ...story.parameters, fileName }, | ||
}; | ||
}); | ||
} catch (err) { | ||
logger.error(`🚨 Extraction error on ${fileName}`); | ||
throw err; | ||
} | ||
}) | ||
); | ||
await fs.writeJson(ouputFile, { v: 3, stories }); | ||
} | ||
|
||
const timeout = 30000; // 30s | ||
const step = 100; // .1s | ||
|
||
export async function useStoriesJson(router: any, options: Options) { | ||
const storiesJson = resolvePathInStorybookCache('stories.json'); | ||
await fs.remove(storiesJson); | ||
const storiesGlobs = (await options.presets.apply('stories')) as string[]; | ||
extractStoriesJson(storiesJson, storiesGlobs, options.configDir); | ||
router.use('/stories.json', async (_req: any, res: any) => { | ||
for (let i = 0; i < timeout / step; i += 1) { | ||
if (fs.existsSync(storiesJson)) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const json = await fs.readFile(storiesJson, 'utf-8'); | ||
res.header('Content-Type', 'application/json'); | ||
return res.send(json); | ||
} | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise((r: any) => setTimeout(r, step)); | ||
} | ||
return res.status(408).send('stories.json timeout'); | ||
}); | ||
} |
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,11 @@ | ||
# Storybook CSF Tools | ||
|
||
A library to read, analyze, transform, and write CSF programmatically. | ||
|
||
- Read - Parse a CSF file with Babel | ||
- Analyze - Extract its metadata & stories based on the Babel AST | ||
- Write - Write the AST back to a file | ||
|
||
Coming soon: | ||
|
||
- Transform - Update the AST to add/remove/modify stories & metadata (TODO) |
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,60 @@ | ||
{ | ||
"name": "@storybook/csf-tools", | ||
"version": "6.3.0-alpha.29", | ||
"description": "", | ||
"keywords": [ | ||
"storybook" | ||
], | ||
"homepage": "https://github.com/storybookjs/storybook/tree/master/lib/csf-tools", | ||
"bugs": { | ||
"url": "https://github.com/storybookjs/storybook/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/storybookjs/storybook.git", | ||
"directory": "lib/csf-tools" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/storybook" | ||
}, | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/ts3.9/index.d.ts", | ||
"typesVersions": { | ||
"<3.8": { | ||
"*": [ | ||
"dist/ts3.4/*" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist/**/*", | ||
"README.md", | ||
"*.js", | ||
"*.d.ts" | ||
], | ||
"scripts": { | ||
"prepare": "node ../../scripts/prepare.js" | ||
}, | ||
"dependencies": { | ||
"@babel/generator": "^7.12.11", | ||
"@babel/parser": "^7.12.11", | ||
"@babel/traverse": "^7.12.11", | ||
"@babel/types": "^7.12.11", | ||
"@storybook/csf": "^0.0.1", | ||
"core-js": "^3.8.2", | ||
"fs-extra": "^9.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "^9.0.6", | ||
"globby": "^11.0.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "a8870128bc9684f9b54083decbd10664bf26bcc4", | ||
"sbmodern": "dist/modern/index.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,24 @@ | ||
import { addSerializer } from 'jest-specific-snapshot'; | ||
import globby from 'globby'; | ||
import path from 'path'; | ||
|
||
import { readCsf } from './CsfFile'; | ||
|
||
addSerializer({ | ||
print: (val: any) => JSON.stringify(val, null, 2), | ||
test: (val) => typeof val !== 'string', | ||
}); | ||
|
||
describe('csf extract', () => { | ||
const fixturesDir = path.join(__dirname, '__testfixtures__'); | ||
const testFiles = globby | ||
.sync(path.join(fixturesDir, '*.stories.*')) | ||
.map((testFile) => [path.basename(testFile).split('.')[0], testFile]); | ||
|
||
it.each(testFiles)('%s', async (testName, testFile) => { | ||
const csf = (await readCsf(testFile)).parse(); | ||
expect({ meta: csf.meta, stories: csf.stories }).toMatchSpecificSnapshot( | ||
path.join(fixturesDir, `${testName}.snapshot`) | ||
); | ||
}); | ||
}); |
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.
Don't want to use our existing types for this? Last thing we need is yet-another-story type definition.