11import { promises as fs } from "node:fs" ;
22import path from "node:path" ;
33
4- import type { RouteData , dirData } from "./types" ;
5-
6- async function readSubdir ( basePath : string , subdir : string ) : Promise < dirData [ ] > {
7- const fullpath = path . join ( basePath , subdir ) ;
8- const filenames = await fs . readdir ( fullpath ) ;
9-
10- const data = await Promise . all (
11- filenames . map ( async filename => {
12- const filepath = path . join ( fullpath , filename ) ;
13- const content = await fs . readFile ( filepath , "utf8" ) ;
14- const data = JSON . parse ( content ) ;
15- if ( typeof data . module === "undefined" ) {
16- data . module = "index" ;
17- data . contains = filenames
18- . filter ( filename => filename !== "index.json" )
19- . map ( filename => filename . replace ( ".json" , "" ) ) ;
20- }
21- const returnValue = {
22- fullpath : path . join ( fullpath , filename ) ,
23- filename : filename . replace ( ".json" , "" ) ,
24- category : subdir ,
25- data : data ,
26- } ;
27- return returnValue ;
28- } )
29- ) ;
30- return data ;
31- }
4+ import type { VersionsData , ModuleData } from "./types" ;
5+
6+ async function readModulesData ( basePath : string ) : Promise < ModuleData [ ] > {
7+ const moduleDirs = await fs . readdir ( basePath ) ;
8+
9+ const modules = await Promise . all ( moduleDirs . map ( async moduleDir => {
10+ const modulePath = path . join ( basePath , moduleDir ) ;
11+
12+ const indexPromise = async ( ) => {
13+ const indexPath = path . join ( modulePath , "index.json" ) ;
14+ const indexContent = await fs . readFile ( indexPath , "utf8" ) ;
15+ return JSON . parse ( indexContent ) ;
16+ } ;
3217
33- async function generateTypeData ( basePath : string ) : Promise < RouteData [ ] > {
34- const subdirs = await fs . readdir ( basePath , {
35- withFileTypes : true ,
36- } ) ;
37- const routes : RouteData [ ] = [ ] ;
38-
39- for ( const subdir of subdirs ) {
40- const data = await readSubdir ( basePath , subdir . name ) ;
41- const returnValue = data . map ( entry => {
42- return {
43- type : entry . category ,
44- name : entry . filename ,
45- path : entry . fullpath ,
46- data : entry . data ,
47- } ;
18+ const typeNames = ( await fs . readdir ( modulePath ) ) . filter ( name => name !== "index.json" ) ;
19+ const typePromises = typeNames . map ( async fileName => {
20+ const typePath = path . join ( modulePath , fileName ) ;
21+ const fileContent = await fs . readFile ( typePath , "utf8" ) ;
22+ return JSON . parse ( fileContent ) ;
4823 } ) ;
49- routes . push ( ...returnValue ) ;
50- }
51- return routes ;
24+
25+ const [ index , ...types ] = await Promise . all ( [ indexPromise ( ) , ...typePromises ] ) ;
26+
27+ return {
28+ name : index . name ,
29+ description : index . description ,
30+ details : index . details ,
31+ types,
32+ }
33+ } ) ) ;
34+
35+ return modules ;
5236}
5337
54- async function generateVersionsData ( ) : Promise < VersionsData > {
38+ async function readVersionsData ( ) : Promise < VersionsData > {
5539 const versionsPath = import . meta. env . VERSION_FILE_PATH ;
5640
5741 if ( ! versionsPath || versionsPath === "" ) {
@@ -63,9 +47,9 @@ async function generateVersionsData(): Promise<VersionsData> {
6347 const content = await fs . readFile ( versionsPath , "utf8" ) ;
6448 const data = JSON . parse ( content ) ;
6549
66- const versions = await Promise . all ( data . versions . map ( async d => ( {
50+ const versions = await Promise . all ( data . versions . map ( async ( d : { name : string , types : any } ) => ( {
6751 name : d . name ,
68- modules : await generateTypeData ( d . types ) ,
52+ modules : await readModulesData ( d . types ) ,
6953 } ) ) )
7054
7155 return {
@@ -78,13 +62,13 @@ let globalVersionsData: Promise<VersionsData>;
7862
7963export function getVersionsData ( ) : Promise < VersionsData > {
8064 if ( ! globalVersionsData ) {
81- globalVersionsData = generateVersionsData ( ) ;
65+ globalVersionsData = readVersionsData ( ) ;
8266 }
8367
8468 return globalVersionsData ;
8569}
8670
87- export async function getTypeData ( ) : RouteData [ ] {
71+ export async function getModulesData ( ) : Promise < ModuleData [ ] > {
8872 const versions = await getVersionsData ( ) ;
89- return versions . versions . find ( v => v . name == versions . default ) . modules ;
73+ return versions . versions . find ( v => v . name == versions . default ) ! . modules ;
9074}
0 commit comments