@@ -3,16 +3,16 @@ import { exec } from "node:child_process";
33import { promisify } from "node:util" ;
44import { readdirSync , existsSync , readFileSync } from "fs" ;
55import { micromark } from "micromark" ;
6- import { featureSpecs } from "../scripts/unmonorepo/feature-spec.mjs" ;
76
87const execAsync = promisify ( exec ) ;
98const rootDir = process . cwd ( ) ;
109const rootConfig = JSON . parse ( readFileSync ( path . join ( rootDir , "rescript.json" ) , "utf8" ) ) ;
11- const publicModulesBySourceDir = new Map (
12- rootConfig . sources
13- . filter ( ( source ) => typeof source === "object" )
14- . filter ( ( source ) => source . dir ?. startsWith ( "src/" ) && Array . isArray ( source . public ) )
15- . map ( ( source ) => [ source . dir , new Set ( source . public ) ] ) ,
10+ const publicSourceEntries = rootConfig . sources . filter (
11+ ( source ) =>
12+ source !== null &&
13+ typeof source === "object" &&
14+ source . dir ?. startsWith ( "src/" ) &&
15+ Array . isArray ( source . public ) ,
1616) ;
1717
1818function toKebabCase ( input ) {
@@ -32,64 +32,16 @@ export function createTypeModuleLink(parentModuleLink, typeName) {
3232 return `${ parentModuleLink } /${ toKebabCase ( typeName ) } ` ;
3333}
3434
35- function mapTypeModules ( parentModuleLink , file , spec ) {
36- const folder = path . dirname ( file ) ;
37-
38- if ( ! existsSync ( folder ) ) {
39- return [ ] ;
40- }
41-
42- const publicModules = publicModulesBySourceDir . get ( spec . sourceDir ) ?? new Set ( ) ;
43- const typesFileName = `${ spec . internalPrefix } Types.res` ;
44- const files = readdirSync ( folder ) ;
45- return files
46- . filter ( ( f ) => f . endsWith ( ".res" ) && f !== typesFileName )
47- . filter ( ( file ) => publicModules . has ( file . replace ( "$" , "" ) . replace ( ".res" , "" ) ) )
48- . map ( ( file ) => {
49- const filePath = path . join ( folder , file ) ;
50-
51- const leafName = file . replace ( "$" , "" ) . replace ( ".res" , "" ) ;
52- const moduleName = leafName ;
53- const apiRouteParameter = toKebabCase ( moduleName ) ;
54- const link = createTypeModuleLink ( parentModuleLink , moduleName ) ;
55- const typeName = moduleName [ 0 ] . toLocaleLowerCase ( ) + moduleName . slice ( 1 ) ;
56-
57- return [
58- typeName ,
59- {
60- filePath,
61- moduleName,
62- link,
63- apiRouteParameter,
64- } ,
65- ] ;
66- } ) ;
67- }
68-
69- function mapRescriptFile ( srcDir , file , spec ) {
70- const filePath = path . join ( srcDir , file ) ;
71- const moduleName = spec . publicModule ;
72- const link = createAPIModuleLink ( moduleName ) ;
73- const items = Object . fromEntries ( mapTypeModules ( link , filePath , spec ) ) ;
74-
75- return {
76- filePath,
77- moduleName,
78- link,
79- apiRouteParameter : toKebabCase ( moduleName ) ,
80- items,
81- } ;
82- }
83-
84- const srcRoot = path . resolve ( process . cwd ( ) , "src" ) ;
85- export const apiModules = featureSpecs
86- . map ( ( spec ) => ( {
87- spec,
88- srcDir : path . join ( srcRoot , spec . dirName ) ,
89- typesFileName : `${ spec . internalPrefix } Types.res` ,
90- } ) )
91- . filter ( ( { srcDir, typesFileName } ) => existsSync ( path . join ( srcDir , typesFileName ) ) )
92- . map ( ( { spec, srcDir, typesFileName } ) => mapRescriptFile ( srcDir , typesFileName , spec ) )
35+ export const apiModules = publicSourceEntries
36+ . flatMap ( ( source ) =>
37+ source . public . map ( ( moduleName ) => ( {
38+ filePath : path . join ( rootDir , source . dir , `${ moduleName } .res` ) ,
39+ moduleName,
40+ link : createAPIModuleLink ( moduleName ) ,
41+ apiRouteParameter : toKebabCase ( moduleName ) ,
42+ } ) ) ,
43+ )
44+ . filter ( ( { filePath } ) => existsSync ( filePath ) )
9345 . sort ( ( a , b ) => a . moduleName . localeCompare ( b . moduleName ) ) ;
9446
9547async function getRescriptDoc ( absoluteFilePath ) {
0 commit comments