mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-24 13:44:54 +00:00
Generate JSON UI component types
This commit is contained in:
50
scripts/generate-json-ui-component-types.ts
Normal file
50
scripts/generate-json-ui-component-types.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
interface RegistryComponent {
|
||||
type?: string
|
||||
name?: string
|
||||
export?: string
|
||||
}
|
||||
|
||||
interface RegistryData {
|
||||
components?: RegistryComponent[]
|
||||
}
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const rootDir = path.resolve(__dirname, '..')
|
||||
const registryPath = path.join(rootDir, 'json-components-registry.json')
|
||||
const outputPath = path.join(rootDir, 'src/types/json-ui-component-types.ts')
|
||||
|
||||
const registryData = JSON.parse(fs.readFileSync(registryPath, 'utf8')) as RegistryData
|
||||
const components = registryData.components ?? []
|
||||
|
||||
const seen = new Set<string>()
|
||||
const componentTypes = components.flatMap((component) => {
|
||||
const typeName = component.type ?? component.name ?? component.export
|
||||
if (!typeName || typeof typeName !== 'string') {
|
||||
throw new Error('Registry component is missing a valid type/name/export entry.')
|
||||
}
|
||||
if (seen.has(typeName)) {
|
||||
return []
|
||||
}
|
||||
seen.add(typeName)
|
||||
return [typeName]
|
||||
})
|
||||
|
||||
const lines = [
|
||||
'// This file is auto-generated by scripts/generate-json-ui-component-types.ts.',
|
||||
'// Do not edit this file directly.',
|
||||
'',
|
||||
'export const jsonUIComponentTypes = [',
|
||||
...componentTypes.map((typeName) => ` ${JSON.stringify(typeName)},`),
|
||||
'] as const',
|
||||
'',
|
||||
'export type JSONUIComponentType = typeof jsonUIComponentTypes[number]',
|
||||
'',
|
||||
]
|
||||
|
||||
fs.writeFileSync(outputPath, `${lines.join('\n')}`)
|
||||
|
||||
console.log(`✅ Wrote ${componentTypes.length} component types to ${outputPath}`)
|
||||
@@ -4,7 +4,7 @@ const path = require('path')
|
||||
const rootDir = path.resolve(__dirname, '..')
|
||||
const registryPath = path.join(rootDir, 'json-components-registry.json')
|
||||
const definitionsPath = path.join(rootDir, 'src/lib/component-definitions.json')
|
||||
const componentTypesPath = path.join(rootDir, 'src/types/json-ui.ts')
|
||||
const componentTypesPath = path.join(rootDir, 'src/types/json-ui-component-types.ts')
|
||||
const uiRegistryPath = path.join(rootDir, 'src/lib/json-ui/component-registry.ts')
|
||||
const atomIndexPath = path.join(rootDir, 'src/components/atoms/index.ts')
|
||||
const moleculeIndexPath = path.join(rootDir, 'src/components/molecules/index.ts')
|
||||
@@ -21,16 +21,10 @@ const componentDefinitions = readJson(definitionsPath)
|
||||
const definitionTypes = new Set(componentDefinitions.map((def) => def.type))
|
||||
|
||||
const componentTypesContent = readText(componentTypesPath)
|
||||
const componentTypesStart = componentTypesContent.indexOf('export type ComponentType')
|
||||
const componentTypesEnd = componentTypesContent.indexOf('export type ActionType')
|
||||
if (componentTypesStart === -1 || componentTypesEnd === -1) {
|
||||
throw new Error('Unable to locate ComponentType union in src/types/json-ui.ts')
|
||||
}
|
||||
const componentTypesBlock = componentTypesContent.slice(componentTypesStart, componentTypesEnd)
|
||||
const componentTypeSet = new Set()
|
||||
const componentTypeRegex = /'([^']+)'/g
|
||||
const componentTypeRegex = /"([^"]+)"/g
|
||||
let match
|
||||
while ((match = componentTypeRegex.exec(componentTypesBlock)) !== null) {
|
||||
while ((match = componentTypeRegex.exec(componentTypesContent)) !== null) {
|
||||
componentTypeSet.add(match[1])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user