1- /* eslint-disable n/no-sync */
2-
3- import * as fs from 'fs' ;
1+ import * as fs from 'fs/promises' ;
42import * as path from 'path' ;
53
64import { findDtsFiles , findTsFiles } from './discovery' ;
@@ -31,10 +29,25 @@ function deduplicationScore(item: MessengerItemDoc): number {
3129 return jsDocScore + homeScore ;
3230}
3331
32+ /**
33+ * Check whether a path exists.
34+ *
35+ * @param targetPath - The path to check.
36+ * @returns A promise that resolves to true if the path exists.
37+ */
38+ async function pathExists ( targetPath : string ) : Promise < boolean > {
39+ try {
40+ await fs . access ( targetPath ) ;
41+ return true ;
42+ } catch {
43+ return false ;
44+ }
45+ }
46+
3447/**
3548 * Main entry point: scans packages, extracts messenger types, and generates docs.
3649 */
37- export function main ( ) : void {
50+ export async function main ( ) : Promise < void > {
3851 // Parse --client flag
3952 const clientIdx = process . argv . indexOf ( '--client' ) ;
4053 const clientPath = clientIdx === - 1 ? undefined : process . argv [ clientIdx + 1 ] ;
@@ -49,13 +62,13 @@ export function main(): void {
4962 ) ;
5063
5164 const nmDir = path . join ( clientPath as string , 'node_modules' , '@metamask' ) ;
52- if ( ! fs . existsSync ( nmDir ) ) {
65+ if ( ! ( await pathExists ( nmDir ) ) ) {
5366 throw new Error ( `${ nmDir } does not exist.` ) ;
5467 }
5568
5669 // Find @metamask packages that contain "controller" or "service" in name
57- const pkgDirs = fs
58- . readdirSync ( nmDir , { withFileTypes : true } )
70+ const entries = await fs . readdir ( nmDir , { withFileTypes : true } ) ;
71+ const pkgDirs = entries
5972 . filter (
6073 ( dirent ) =>
6174 dirent . isDirectory ( ) &&
@@ -65,14 +78,14 @@ export function main(): void {
6578 . map ( ( dirent ) => path . join ( nmDir , dirent . name , 'dist' ) ) ;
6679
6780 for ( const distDir of pkgDirs ) {
68- if ( ! fs . existsSync ( distDir ) ) {
81+ if ( ! ( await pathExists ( distDir ) ) ) {
6982 continue ;
7083 }
7184
72- const dtsFiles = findDtsFiles ( distDir ) ;
85+ const dtsFiles = await findDtsFiles ( distDir ) ;
7386 for ( const file of dtsFiles ) {
7487 try {
75- const items = extractFromFile ( file , clientPath as string ) ;
88+ const items = await extractFromFile ( file , clientPath as string ) ;
7689 allItems . push ( ...items ) ;
7790 } catch ( error ) {
7891 console . warn (
@@ -85,20 +98,20 @@ export function main(): void {
8598 console . log ( 'Scanning packages for Messenger action/event types...' ) ;
8699
87100 const packagesDir = path . join ( ROOT , 'packages' ) ;
88- const packageDirs = fs
89- . readdirSync ( packagesDir , { withFileTypes : true } )
101+ const entries = await fs . readdir ( packagesDir , { withFileTypes : true } ) ;
102+ const packageDirs = entries
90103 . filter ( ( dirent ) => dirent . isDirectory ( ) )
91104 . map ( ( dirent ) => path . join ( packagesDir , dirent . name , 'src' ) ) ;
92105
93106 for ( const srcDir of packageDirs ) {
94- if ( ! fs . existsSync ( srcDir ) ) {
107+ if ( ! ( await pathExists ( srcDir ) ) ) {
95108 continue ;
96109 }
97110
98- const tsFiles = findTsFiles ( srcDir ) ;
111+ const tsFiles = await findTsFiles ( srcDir ) ;
99112 for ( const file of tsFiles ) {
100113 try {
101- const items = extractFromFile ( file , ROOT ) ;
114+ const items = await extractFromFile ( file , ROOT ) ;
102115 allItems . push ( ...items ) ;
103116 } catch ( error ) {
104117 console . warn (
@@ -169,39 +182,39 @@ export function main(): void {
169182 const docsDir = path . join ( ROOT , 'docs-site' , 'docs' ) ;
170183
171184 // Clean existing generated docs
172- if ( fs . existsSync ( docsDir ) ) {
173- fs . rmSync ( docsDir , { recursive : true } ) ;
185+ if ( await pathExists ( docsDir ) ) {
186+ await fs . rm ( docsDir , { recursive : true } ) ;
174187 }
175- fs . mkdirSync ( docsDir , { recursive : true } ) ;
188+ await fs . mkdir ( docsDir , { recursive : true } ) ;
176189
177190 // Generate namespace pages
178191 for ( const ns of namespaces ) {
179192 const nsDir = path . join ( docsDir , ns . namespace ) ;
180- fs . mkdirSync ( nsDir , { recursive : true } ) ;
193+ await fs . mkdir ( nsDir , { recursive : true } ) ;
181194
182195 if ( ns . actions . length > 0 ) {
183- fs . writeFileSync (
196+ await fs . writeFile (
184197 path . join ( nsDir , 'actions.md' ) ,
185198 generateNamespacePage ( ns , 'action' , clientMode ) ,
186199 ) ;
187200 }
188201
189202 if ( ns . events . length > 0 ) {
190- fs . writeFileSync (
203+ await fs . writeFile (
191204 path . join ( nsDir , 'events.md' ) ,
192205 generateNamespacePage ( ns , 'event' , clientMode ) ,
193206 ) ;
194207 }
195208 }
196209
197210 // Generate index page
198- fs . writeFileSync (
211+ await fs . writeFile (
199212 path . join ( docsDir , 'index.md' ) ,
200213 generateIndexPage ( namespaces , clientName ) ,
201214 ) ;
202215
203216 // Generate sidebars
204- fs . writeFileSync (
217+ await fs . writeFile (
205218 path . join ( ROOT , 'docs-site' , 'sidebars.ts' ) ,
206219 generateSidebars ( namespaces ) ,
207220 ) ;
0 commit comments