@@ -3,20 +3,8 @@ import path from "node:path";
33
44import type { RouteData , dirData } from "./types" ;
55
6- function modulesPath ( ) {
7- const modulesPath = import . meta. env . SECRET_MODULES_PATH ;
8-
9- if ( ! modulesPath || modulesPath === "" ) {
10- throw new Error (
11- "Cannot generate types, missing SECRET_MODULES_PATH"
12- ) ;
13- }
14-
15- return modulesPath ;
16- }
17-
18- async function readSubdir ( subdir : string ) : Promise < dirData [ ] > {
19- const fullpath = path . join ( modulesPath ( ) , subdir ) ;
6+ async function readSubdir ( basePath : string , subdir : string ) : Promise < dirData [ ] > {
7+ const fullpath = path . join ( basePath , subdir ) ;
208 const filenames = await fs . readdir ( fullpath ) ;
219
2210 const data = await Promise . all (
@@ -42,16 +30,14 @@ async function readSubdir(subdir: string): Promise<dirData[]> {
4230 return data ;
4331}
4432
45- async function generateTypeData ( ) : Promise < RouteData [ ] > {
46- const mainDir = modulesPath ( ) ;
47-
48- const subdirs = await fs . readdir ( mainDir , {
33+ async function generateTypeData ( basePath : string ) : Promise < RouteData [ ] > {
34+ const subdirs = await fs . readdir ( basePath , {
4935 withFileTypes : true ,
5036 } ) ;
5137 const routes : RouteData [ ] = [ ] ;
5238
5339 for ( const subdir of subdirs ) {
54- const data = await readSubdir ( subdir . name ) ;
40+ const data = await readSubdir ( basePath , subdir . name ) ;
5541 const returnValue = data . map ( entry => {
5642 return {
5743 type : entry . category ,
@@ -65,12 +51,40 @@ async function generateTypeData(): Promise<RouteData[]> {
6551 return routes ;
6652}
6753
68- let globalTypeData : Promise < RouteData [ ] > ;
54+ async function generateVersionsData ( ) : Promise < VersionsData > {
55+ const versionsPath = import . meta. env . VERSION_FILE_PATH ;
56+
57+ if ( ! versionsPath || versionsPath === "" ) {
58+ throw new Error (
59+ "Cannot generate types, missing VERSION_FILE_PATH"
60+ ) ;
61+ }
62+
63+ const content = await fs . readFile ( versionsPath , "utf8" ) ;
64+ const data = JSON . parse ( content ) ;
65+
66+ const versions = await Promise . all ( data . versions . map ( async d => ( {
67+ name : d . name ,
68+ modules : await generateTypeData ( d . types ) ,
69+ } ) ) )
70+
71+ return {
72+ versions,
73+ default : data . default ,
74+ }
75+ }
76+
77+ let globalVersionsData : Promise < VersionsData > ;
6978
70- export function getTypeData ( ) : Promise < RouteData [ ] > {
71- if ( ! globalTypeData ) {
72- globalTypeData = generateTypeData ( ) ;
79+ export function getVersionsData ( ) : Promise < VersionsData > {
80+ if ( ! globalVersionsData ) {
81+ globalVersionsData = generateVersionsData ( ) ;
7382 }
7483
75- return globalTypeData ;
84+ return globalVersionsData ;
85+ }
86+
87+ export async function getTypeData ( ) : RouteData [ ] {
88+ const versions = await getVersionsData ( ) ;
89+ return versions . versions . find ( v => v . name == versions . default ) . modules ;
7690}
0 commit comments