ci: extract metadata.json from storybook-static

This commit is contained in:
Hossein Mehrabi 2023-10-06 18:56:36 +03:30
parent f25b47522f
commit 6e23934b2f
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
3 changed files with 64 additions and 22 deletions

View File

@ -10,6 +10,8 @@ const ROOT_DIR = path.resolve(DIRNAME, '../../../')
const BUILD_DIR = path.resolve(ROOT_DIR, 'storybook-static') const BUILD_DIR = path.resolve(ROOT_DIR, 'storybook-static')
const STORIES_JSON = path.resolve(BUILD_DIR, 'stories.json') const STORIES_JSON = path.resolve(BUILD_DIR, 'stories.json')
;(global as any).__STORYBOOK_MODULE_ADDONS__ = {}
type StoryInfo = { type StoryInfo = {
id: string id: string
name: string name: string
@ -30,7 +32,10 @@ type StoriesJson = {
stories: StoryInfo[] stories: StoryInfo[]
} }
export const fromStories = async (storiesJson: StoriesJson) => { export const fromStories = async (
storiesJson: StoriesJson,
assetsDir: string,
) => {
const { stories: storiesMap } = storiesJson const { stories: storiesMap } = storiesJson
const stories = Object.values(storiesMap).filter( const stories = Object.values(storiesMap).filter(
(story) => !story.parameters.docsOnly, (story) => !story.parameters.docsOnly,
@ -45,9 +50,7 @@ export const fromStories = async (storiesJson: StoriesJson) => {
} }
> = {} > = {}
const storyFiles = await glob( const storyFiles = await glob(assetsDir + `/*.stories-*.js`)
path.join(BUILD_DIR, 'assets') + `/*.stories-*.js`,
)
for (const file of storyFiles) { for (const file of storyFiles) {
const mod = await import(file) const mod = await import(file)
@ -68,6 +71,48 @@ export const fromStories = async (storiesJson: StoriesJson) => {
.sort((a, b) => a.name.localeCompare(b.name)) .sort((a, b) => a.name.localeCompare(b.name))
} }
export const extractGlobalTypes = async (assetsDir: string) => {
const filenames = await glob(assetsDir + `/preview-*.js`)
for (const filename of filenames) {
const file = await fsp.readFile(filename, 'utf-8')
if (!file.includes('as globalTypes')) continue
const mod = await import(filename)
return mod.globalTypes
}
return []
}
export const extractMetadata = async (dir: string) => {
const assetsDir = path.join(dir, 'assets')
await fsp.writeFile(
path.join(assetsDir, 'package.json'),
Buffer.from(
JSON.stringify({
type: 'module',
}),
),
)
const stories = await import(path.join(dir, 'stories.json'), {
assert: { type: 'json' },
})
const components = await fromStories(stories.default, assetsDir)
const globalTypes = await extractGlobalTypes(assetsDir)
fs.unlinkSync(path.join(assetsDir, 'package.json'))
return {
components,
globalTypes,
}
}
export const run = async () => { export const run = async () => {
if (!fs.existsSync(BUILD_DIR)) { if (!fs.existsSync(BUILD_DIR)) {
console.error('The storybook-static dir not found!') console.error('The storybook-static dir not found!')
@ -78,25 +123,12 @@ export const run = async () => {
process.exit(1) process.exit(1)
} }
const stories = await import(STORIES_JSON, { assert: { type: 'json' } }) const metadata = await extractMetadata(BUILD_DIR)
await fsp.writeFile( await fsp.writeFile(
path.join(BUILD_DIR, 'package.json'), path.join(BUILD_DIR, '_metadata.json'),
Buffer.from( Buffer.from(JSON.stringify(metadata)),
JSON.stringify({
type: 'module',
}),
),
) )
const result = await fromStories(stories.default)
await fsp.writeFile(
path.join(BUILD_DIR, 'components.json'),
Buffer.from(JSON.stringify(result)),
)
fs.unlinkSync(path.join(BUILD_DIR, 'package.json'))
} }
run() run()

View File

@ -9,8 +9,8 @@
"build": "tsc && vite build", "build": "tsc && vite build",
"watch": "tsc && vite build --watch --emptyOutDir false", "watch": "tsc && vite build --watch --emptyOutDir false",
"storybook": "storybook dev -p 6006 -s .storybook/public", "storybook": "storybook dev -p 6006 -s .storybook/public",
"build-storybook": "storybook build -s .storybook/public && yarn storybook:export-components", "build-storybook": "storybook build -s .storybook/public && yarn storybook:extract-metadata",
"storybook:export-components": "ts-node -P tsconfig.node.json .storybook/scripts/export-components.ts", "storybook:extract-metadata": "ts-node -P tsconfig.node.json .storybook/scripts/extract-metadata.ts",
"preview": "vite preview", "preview": "vite preview",
"prepublish": "yarn build" "prepublish": "yarn build"
}, },
@ -33,6 +33,7 @@
"@storybook/addon-essentials": "^7.4.6", "@storybook/addon-essentials": "^7.4.6",
"@storybook/addon-interactions": "^7.4.6", "@storybook/addon-interactions": "^7.4.6",
"@storybook/addon-links": "^7.4.6", "@storybook/addon-links": "^7.4.6",
"@storybook/addons": "^7.4.6",
"@storybook/node-logger": "^7.4.6", "@storybook/node-logger": "^7.4.6",
"@storybook/react": "^7.4.6", "@storybook/react": "^7.4.6",
"@storybook/react-vite": "^7.4.6", "@storybook/react-vite": "^7.4.6",

View File

@ -4307,6 +4307,15 @@
memoizerific "^1.11.3" memoizerific "^1.11.3"
prop-types "^15.7.2" prop-types "^15.7.2"
"@storybook/addons@^7.4.6":
version "7.4.6"
resolved "https://registry.yarnpkg.com/@storybook/addons/-/addons-7.4.6.tgz#cca556ca30aa34652f9bbab30467a538df5b10fe"
integrity sha512-c+4awrtwNlJayFdgLkEXa5H2Gj+KNlxuN+Z5oDAdZBLqXI8g0gn7eYO2F/eCSIDWdd/+zcU2uq57XPFKc8veHQ==
dependencies:
"@storybook/manager-api" "7.4.6"
"@storybook/preview-api" "7.4.6"
"@storybook/types" "7.4.6"
"@storybook/blocks@7.4.6": "@storybook/blocks@7.4.6":
version "7.4.6" version "7.4.6"
resolved "https://registry.yarnpkg.com/@storybook/blocks/-/blocks-7.4.6.tgz#03134130fa20d6c36c6985008bc2c38892c5b8f5" resolved "https://registry.yarnpkg.com/@storybook/blocks/-/blocks-7.4.6.tgz#03134130fa20d6c36c6985008bc2c38892c5b8f5"