forked from patternfly/patternfly-doc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent-index.json.ts
More file actions
119 lines (102 loc) · 3.52 KB
/
component-index.json.ts
File metadata and controls
119 lines (102 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { APIRoute } from 'astro'
import { getApiIndex } from '../../utils/apiIndex/get'
import { removeSubsection } from '../../utils/case'
import { getOutputDir } from '../../utils/getOutputDir'
import { join } from 'path'
import { readFile } from 'fs/promises'
export const prerender = true
interface ComponentEntry {
section: string
page: string
tabs: string[]
hasProps: boolean
hasCss: boolean
exampleCount: number
}
export interface ComponentIndex {
version: string
components: Record<string, ComponentEntry>
}
// "about-modal" → "AboutModal", "forms_checkbox" → "Checkbox"
function pageToPascalCase(page: string): string {
return removeSubsection(page)
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('')
}
export const GET: APIRoute = async () => {
try {
const index = await getApiIndex()
// props.json keys include both component names ("Alert") and their prop
// interfaces ("AlertProps"). Filter out the interface entries to get the
// set of component names that have prop documentation available.
let componentNamesWithProps = new Set<string>()
try {
const outputDir = await getOutputDir()
const propsFile = await readFile(join(outputDir, 'props.json'), 'utf-8')
const propsData = JSON.parse(propsFile)
const propsSuffixPattern = /Props/i
componentNamesWithProps = new Set(
Object.keys(propsData).filter((name) => !propsSuffixPattern.test(name)),
)
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error
}
}
if (index.versions.length === 0) {
throw new Error('API index contains no versions')
}
// Latest version is last in sorted array (e.g., ["v5", "v6"] → "v6")
const version = index.versions[index.versions.length - 1]
const sections = index.sections[version] || []
const components: Record<string, ComponentEntry> = {}
for (const section of sections) {
const pagesKey = `${version}::${section}`
const pages = index.pages[pagesKey] || []
for (const page of pages) {
const tabsKey = `${version}::${section}::${page}`
const tabs = index.tabs[tabsKey] || []
let exampleCount = 0
for (const tab of tabs) {
const examplesKey = `${tabsKey}::${tab}`
const examples = index.examples[examplesKey] || []
exampleCount += examples.length
}
const hasCss = tabsKey in index.css && index.css[tabsKey].length > 0
const pascalName = pageToPascalCase(page)
const hasProps = componentNamesWithProps.has(pascalName)
// Prefer the first occurrence when multiple sections produce the same
// PascalCase key (e.g., components/table vs extensions/data-view_table)
if (!components[pascalName]) {
components[pascalName] = {
section,
page,
tabs,
hasProps,
hasCss,
exampleCount,
}
}
}
}
const componentIndex: ComponentIndex = { version, components }
return new Response(JSON.stringify(componentIndex), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
})
} catch (error) {
return new Response(
JSON.stringify({
error: 'Failed to generate component index',
details: error instanceof Error ? error.message : String(error),
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}
}