mirror of https://github.com/acid-info/lsd.git
ci: extract metadata.json from storybook-static
This commit is contained in:
parent
f25b47522f
commit
6e23934b2f
|
@ -10,6 +10,8 @@ const ROOT_DIR = path.resolve(DIRNAME, '../../../')
|
|||
const BUILD_DIR = path.resolve(ROOT_DIR, 'storybook-static')
|
||||
const STORIES_JSON = path.resolve(BUILD_DIR, 'stories.json')
|
||||
|
||||
;(global as any).__STORYBOOK_MODULE_ADDONS__ = {}
|
||||
|
||||
type StoryInfo = {
|
||||
id: string
|
||||
name: string
|
||||
|
@ -30,7 +32,10 @@ type StoriesJson = {
|
|||
stories: StoryInfo[]
|
||||
}
|
||||
|
||||
export const fromStories = async (storiesJson: StoriesJson) => {
|
||||
export const fromStories = async (
|
||||
storiesJson: StoriesJson,
|
||||
assetsDir: string,
|
||||
) => {
|
||||
const { stories: storiesMap } = storiesJson
|
||||
const stories = Object.values(storiesMap).filter(
|
||||
(story) => !story.parameters.docsOnly,
|
||||
|
@ -45,9 +50,7 @@ export const fromStories = async (storiesJson: StoriesJson) => {
|
|||
}
|
||||
> = {}
|
||||
|
||||
const storyFiles = await glob(
|
||||
path.join(BUILD_DIR, 'assets') + `/*.stories-*.js`,
|
||||
)
|
||||
const storyFiles = await glob(assetsDir + `/*.stories-*.js`)
|
||||
|
||||
for (const file of storyFiles) {
|
||||
const mod = await import(file)
|
||||
|
@ -68,6 +71,48 @@ export const fromStories = async (storiesJson: StoriesJson) => {
|
|||
.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 () => {
|
||||
if (!fs.existsSync(BUILD_DIR)) {
|
||||
console.error('The storybook-static dir not found!')
|
||||
|
@ -78,25 +123,12 @@ export const run = async () => {
|
|||
process.exit(1)
|
||||
}
|
||||
|
||||
const stories = await import(STORIES_JSON, { assert: { type: 'json' } })
|
||||
const metadata = await extractMetadata(BUILD_DIR)
|
||||
|
||||
await fsp.writeFile(
|
||||
path.join(BUILD_DIR, 'package.json'),
|
||||
Buffer.from(
|
||||
JSON.stringify({
|
||||
type: 'module',
|
||||
}),
|
||||
),
|
||||
path.join(BUILD_DIR, '_metadata.json'),
|
||||
Buffer.from(JSON.stringify(metadata)),
|
||||
)
|
||||
|
||||
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()
|
|
@ -9,8 +9,8 @@
|
|||
"build": "tsc && vite build",
|
||||
"watch": "tsc && vite build --watch --emptyOutDir false",
|
||||
"storybook": "storybook dev -p 6006 -s .storybook/public",
|
||||
"build-storybook": "storybook build -s .storybook/public && yarn storybook:export-components",
|
||||
"storybook:export-components": "ts-node -P tsconfig.node.json .storybook/scripts/export-components.ts",
|
||||
"build-storybook": "storybook build -s .storybook/public && yarn storybook:extract-metadata",
|
||||
"storybook:extract-metadata": "ts-node -P tsconfig.node.json .storybook/scripts/extract-metadata.ts",
|
||||
"preview": "vite preview",
|
||||
"prepublish": "yarn build"
|
||||
},
|
||||
|
@ -33,6 +33,7 @@
|
|||
"@storybook/addon-essentials": "^7.4.6",
|
||||
"@storybook/addon-interactions": "^7.4.6",
|
||||
"@storybook/addon-links": "^7.4.6",
|
||||
"@storybook/addons": "^7.4.6",
|
||||
"@storybook/node-logger": "^7.4.6",
|
||||
"@storybook/react": "^7.4.6",
|
||||
"@storybook/react-vite": "^7.4.6",
|
||||
|
|
|
@ -4307,6 +4307,15 @@
|
|||
memoizerific "^1.11.3"
|
||||
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":
|
||||
version "7.4.6"
|
||||
resolved "https://registry.yarnpkg.com/@storybook/blocks/-/blocks-7.4.6.tgz#03134130fa20d6c36c6985008bc2c38892c5b8f5"
|
||||
|
|
Loading…
Reference in New Issue