Skip to content

Commit

Permalink
Merge 0c60aa1 into 87cf3b2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblack authored Dec 27, 2024
2 parents 87cf3b2 + 0c60aa1 commit f500633
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-mugs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

test: add check for story ids in generated docs
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,12 @@ jobs:
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build storybook to generate story IDs
run: npm run build:storybook
working-directory: packages/react
- name: Build components.json
run: npm run build:components.json
run: npx tsx script/components-json/build.ts --storybook-data 'storybook-static/index.json'
working-directory: packages/react

sizes:
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"clean": "rimraf dist lib lib-esm css",
"start": "concurrently npm:start:*",
"start:storybook": "STORYBOOK=true storybook dev -p 6006",
"build:storybook": "script/build-storybook",
"build:storybook": "storybook build",
"build:docs": "NODE_OPTIONS=--openssl-legacy-provider script/build-docs",
"build:docs:preview": "NODE_OPTIONS=--openssl-legacy-provider script/build-docs preview",
"build:components.json": "tsx script/components-json/build.ts",
Expand Down
48 changes: 48 additions & 0 deletions packages/react/script/components-json/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import generate from '@babel/generator'
import {parse} from '@babel/parser'
import traverse from '@babel/traverse'
import type {ArrowFunctionExpression, Identifier, FunctionDeclaration} from '@babel/types'
import path from 'node:path'
import {parseArgs} from 'node:util'
import Ajv from 'ajv'
import {pascalCase, kebabCase} from 'change-case'
import glob from 'fast-glob'
Expand All @@ -11,6 +13,14 @@ import prettier from '@prettier/sync'
import componentSchema from './component.schema.json'
import outputSchema from './output.schema.json'

const args = parseArgs({
options: {
'storybook-data': {
type: 'string',
},
},
})

// Only includes fields we use in this script
type Component = {
name: string
Expand Down Expand Up @@ -40,6 +50,23 @@ const storyPrefix = {
stable: '',
}

let _storybookData: StorybookData | null = null

function getStorybookData(): StorybookData {
const input = args.values['storybook-data']
if (!input) {
throw new Error('Unable to get value for --storybook-data')
}

if (_storybookData === null) {
const filepath = path.resolve(process.cwd(), args.values['storybook-data']!)
const contents = fs.readFileSync(filepath, 'utf-8')
_storybookData = JSON.parse(contents)
}

return _storybookData as StorybookData
}

const components = docsFiles.map(docsFilepath => {
const docs = JSON.parse(fs.readFileSync(docsFilepath, 'utf-8'))

Expand Down Expand Up @@ -114,6 +141,18 @@ const components = docsFiles.map(docsFilepath => {
}
}

if (args.values['storybook-data']) {
const storybookData = getStorybookData()
for (const story of docs.stories) {
const match = Object.values(storybookData.entries).find(entry => {
return entry.id === story.id
})
if (!match) {
throw new Error(`Story "${story.id}" not found in storybook-data`)
}
}
}

// TODO: Provide default type and description for sx and ref props
return {
source: `https://github.com/primer/react/tree/main/packages/react/${docsFilepath.substring(0, docsFilepath.lastIndexOf('/'))}`,
Expand Down Expand Up @@ -236,3 +275,12 @@ function getStoryIds(docs: Component, storyNames: string[]) {

return ids.map(id => ({id}))
}

interface StorybookData {
v: number
entries: {
[key: string]: {
id: string
}
}
}
2 changes: 1 addition & 1 deletion packages/react/src/Button/IconButton.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"id": "components-iconbutton-features--external-tooltip"
},
{
"id": "components-iconbutton-features--external-tooltip-version1"
"id": "components-iconbutton-features--external-tooltip-version-1"
},
{
"id": "components-iconbutton-features--as-a-menu-anchor"
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/Button/IconButton.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const ExternalTooltip = () => (
</Tooltip>
)

export const ExternalTooltipVersion1 = () => (
/* eslint-disable-next-line camelcase */
export const ExternalTooltipVersion_1 = () => (
<TooltipV1 text="this is a supportive description for icon button" direction="se">
<IconButton icon={HeartIcon} aria-label="HeartIcon" />
</TooltipV1>
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/Hidden/Hidden.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"a11yReviewed": false,
"stories": [
{
"id": "drafts-components-hidden--default"
"id": "experimental-components-hidden--default"
},
{
"id": "drafts-components-hidden-features--hide-content"
"id": "experimental-components-hidden-features--hide-content"
},
{
"id": "drafts-components-hidden-features--render-content-responsively"
"id": "experimental-components-hidden-features--render-content-responsively"
}
],
"importPath": "@primer/react/experimental",
Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/InlineMessage/InlineMessage.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
"importPath": "@primer/react/experimental",
"stories": [
{
"id": "components-inlinemessage--default"
"id": "experimental-components-inlinemessage--default"
},
{
"id": "components-inlinemessage-features--critical"
"id": "experimental-components-inlinemessage-features--critical"
},
{
"id": "components-inlinemessage-features--success"
"id": "experimental-components-inlinemessage-features--success"
},
{
"id": "components-inlinemessage-features--unavailable"
"id": "experimental-components-inlinemessage-features--unavailable"
},
{
"id": "components-inlinemessage-features--warning"
"id": "experimental-components-inlinemessage-features--warning"
},
{
"id": "components-inlinemessage-features--multiline"
"id": "experimental-components-inlinemessage-features--multiline"
}
],
"props": [
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/Portal/Portal.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"a11yReviewed": false,
"stories": [
{
"id": "components-portal--default"
"id": "behaviors-portal--default"
},
{
"id": "components-portal-features--custom-portal-root-by-id"
"id": "behaviors-portal-features--custom-portal-root-by-id"
},
{
"id": "components-portal-features--custom-portal-root-by-registration"
"id": "behaviors-portal-features--custom-portal-root-by-registration"
},
{
"id": "components-portal-features--multiple-portal-roots"
"id": "behaviors-portal-features--multiple-portal-roots"
}
],
"importPath": "@primer/react",
Expand Down

0 comments on commit f500633

Please sign in to comment.