11import { promises as fs } from 'fs'
22import process from 'process'
3- import { fileURLToPath } from 'url'
43
5- import glob from 'fast-glob '
4+ import { FRAMEWORK_NAMES } from './frameworks.js '
65
6+ const FRAMEWORKS_SOURCE = new URL ( './frameworks.js' , import . meta. url )
77const FRAMEWORKS_DIR = new URL ( '../src/frameworks/' , import . meta. url )
88const BUILD_DIR = new URL ( '../src/generated/' , import . meta. url )
99const FRAMEWORKS_BUILD = new URL ( 'frameworks.ts' , BUILD_DIR )
10+ const FRAMEWORK_NAMES_BUILD = new URL ( 'frameworkNames.ts' , BUILD_DIR )
1011
1112// We enforce frameworks to be written with JSON to ensure they remain logicless
1213// which is simpler for contributors and avoid adding unnecessary logic.
@@ -15,8 +16,9 @@ const FRAMEWORKS_BUILD = new URL('frameworks.ts', BUILD_DIR)
1516// Therefore, we transform JSON to JavaScript files using at build time.
1617const transformFrameworks = async function ( ) {
1718 await fs . mkdir ( BUILD_DIR , { recursive : true } )
18- const frameworkFiles = await glob ( '*.json' , { cwd : fileURLToPath ( FRAMEWORKS_DIR ) , absolute : true } )
19- const frameworks = await Promise . all ( frameworkFiles . map ( transformFramework ) )
19+ // We use the framework names here to generate the frameworks infos, so that
20+ // we keep the exact same order as in FRAMEWORK_NAMES
21+ const frameworks = await Promise . all ( FRAMEWORK_NAMES . map ( transformFramework ) )
2022 const fileContents = `${ FRAMEWORKS_HEADER } ${ JSON . stringify ( frameworks , null , 2 ) } `
2123 await fs . writeFile ( FRAMEWORKS_BUILD , fileContents )
2224}
@@ -33,8 +35,9 @@ const updateLogoUrls = function (contents) {
3335 return updatedContents
3436}
3537
36- const transformFramework = async function ( frameworkFile ) {
37- const jsonContents = await fs . readFile ( frameworkFile )
38+ const transformFramework = async function ( frameworkName ) {
39+ const frameworkUrl = new URL ( `${ frameworkName } .json` , FRAMEWORKS_DIR )
40+ const jsonContents = await fs . readFile ( frameworkUrl )
3841 const contents = JSON . parse ( jsonContents )
3942
4043 const updatedContents = updateLogoUrls ( contents )
@@ -45,4 +48,17 @@ const FRAMEWORKS_HEADER = `import type { FrameworkDefinition } from "../types.js
4548// This file is autogenerated at build time
4649export const FRAMEWORKS: FrameworkDefinition[] = `
4750
51+ const transformFrameworkNames = async function ( ) {
52+ const frameworkNamesContent = await fs . readFile ( FRAMEWORKS_SOURCE , 'utf-8' )
53+ const fileContents = `${ FRAMEWORK_NAMES_HEADER } ${ frameworkNamesContent . trim ( ) } ${ FRAMEWORK_NAMES_FOOTER } `
54+ await fs . writeFile ( FRAMEWORK_NAMES_BUILD , fileContents )
55+ }
56+
57+ const FRAMEWORK_NAMES_HEADER = '// This file is autogenerated at build time\n\n'
58+
59+ const FRAMEWORK_NAMES_FOOTER = ` as const
60+
61+ export type FrameworkName = typeof FRAMEWORK_NAMES[number]`
62+
4863transformFrameworks ( )
64+ transformFrameworkNames ( )
0 commit comments