1- import fs from "fs" ;
1+ import { readFileSync } from "fs" ;
2+ import fs from "fs/promises" ;
23import { basename } from "path" ;
34
45const basePath = new URL (
@@ -50,9 +51,11 @@ function extractSummary(markdown: string): string {
5051 return normalizedText . split ( " " ) [ 0 ] || "" ; // Fallback: first word if no sentence found
5152}
5253
53- function getDirectories ( dirPath : URL ) : URL [ ] {
54+ async function getDirectories ( dirPath : URL ) : Promise < URL [ ] > {
5455 try {
55- const entries = fs . readdirSync ( dirPath , { withFileTypes : true } ) ;
56+ const entries = await fs . readdir ( dirPath , {
57+ withFileTypes : true ,
58+ } ) ;
5659 return entries
5760 . filter ( ( entry ) => entry . isDirectory ( ) )
5861 . map ( ( entry ) => new URL ( entry . name + "/" , dirPath ) ) ;
@@ -62,14 +65,16 @@ function getDirectories(dirPath: URL): URL[] {
6265 }
6366}
6467
65- function getIndexMdContents ( folders : URL [ ] ) : { [ key : string ] : string } {
68+ async function getIndexMdContents (
69+ folders : URL [ ] ,
70+ ) : Promise < { [ key : string ] : string } > {
6671 const results : { [ key : string ] : string } = { } ;
6772
6873 for ( const folder of folders ) {
6974 const indexPath = new URL ( "index.md" , folder ) ;
7075
7176 try {
72- const content = fs . readFileSync ( indexPath , "utf-8" ) ;
77+ const content = await fs . readFile ( indexPath , "utf-8" ) ;
7378
7479 // Improved title extraction
7580 const titleMatch = content . match ( / t i t l e : \s * [ " ' ] ? ( [ ^ " ' \n ] + ) [ " ' ] ? / ) ;
@@ -88,26 +93,24 @@ function getIndexMdContents(folders: URL[]): { [key: string]: string } {
8893 return results ;
8994}
9095
91- export function generateDescriptions ( ) : Record < string , string > {
92- const stats = fs . statSync ( basePath ) ;
96+ export async function generateDescriptions ( ) : Promise < Record < string , string > > {
97+ const stats = await fs . stat ( basePath ) ;
9398 if ( ! stats . isDirectory ( ) ) {
9499 throw new Error (
95100 "MDN submodule does not exist; try running `git submodule update --init`" ,
96101 ) ;
97102 }
98-
99103 try {
100- const folders = getDirectories ( basePath ) ;
104+ const folders = await getDirectories ( basePath ) ;
101105 if ( folders . length > 0 ) {
102- return getIndexMdContents ( folders ) ;
106+ return await getIndexMdContents ( folders ) ;
103107 }
104108 } catch ( error ) {
105109 console . error ( "Error generating API descriptions:" , error ) ;
106110 }
107111
108112 return { } ;
109113}
110-
111114export function extractSummaryFromFile ( url : string ) : string {
112115 const relativePath = url
113116 . replace ( "https://developer.mozilla.org/docs/" , "" )
@@ -120,7 +123,7 @@ export function extractSummaryFromFile(url: string): string {
120123 ) ;
121124
122125 try {
123- const content = fs . readFileSync ( filePath , "utf-8" ) ;
126+ const content = readFileSync ( filePath , "utf-8" ) ;
124127 return extractSummary ( content ) ;
125128 } catch ( error ) {
126129 console . error (
0 commit comments