9090 . demandCommand ( )
9191 . help ( ) . argv ;
9292
93+ process . on ( 'exit' , cleanup ) ;
94+ process . on ( 'SIGINT' , cleanup ) ;
95+
96+ let authApiReportOriginal : string ;
97+ let authApiConfigOriginal : string ;
98+
99+ function cleanup ( ) {
100+ try {
101+ // Restore original auth api-extractor.json contents.
102+ if ( authApiReportOriginal ) {
103+ console . log ( `Restoring original auth api-extractor.json contents.` ) ;
104+ fs . writeFileSync (
105+ `${ projectRoot } /packages/auth/api-extractor.json` ,
106+ authApiConfigOriginal
107+ ) ;
108+ }
109+ // Restore original auth.api.md
110+ if ( authApiConfigOriginal ) {
111+ console . log ( `Restoring original auth.api.md contents.` ) ;
112+ fs . writeFileSync (
113+ `${ projectRoot } /common/api-review/auth.api.md` ,
114+ authApiReportOriginal
115+ ) ;
116+ }
117+ for ( const excludedPackage of EXCLUDED_PACKAGES ) {
118+ if ( fs . existsSync ( `${ projectRoot } /temp/${ excludedPackage } .skip` ) ) {
119+ console . log (
120+ `Restoring json files for excluded package: ${ excludedPackage } .`
121+ ) ;
122+ fs . renameSync (
123+ `${ projectRoot } /temp/${ excludedPackage } .skip` ,
124+ `${ projectRoot } /temp/${ excludedPackage } .api.json`
125+ ) ;
126+ }
127+ }
128+ } catch ( e ) {
129+ console . error (
130+ 'Error cleaning up files on exit - ' +
131+ 'check for temp modifications to md and json files.'
132+ ) ;
133+ console . error ( e ) ;
134+ }
135+ }
136+
93137async function generateToc ( ) {
94138 console . log ( `Temporarily renaming excluded packages' json files.` ) ;
95139 for ( const excludedPackage of EXCLUDED_PACKAGES ) {
@@ -115,15 +159,7 @@ async function generateToc() {
115159 { stdio : 'inherit' }
116160 ) ;
117161 } finally {
118- console . log ( `Restoring excluded packages' json files.` ) ;
119- for ( const excludedPackage of EXCLUDED_PACKAGES ) {
120- if ( fs . existsSync ( `${ projectRoot } /temp/${ excludedPackage } .skip` ) ) {
121- fs . renameSync (
122- `${ projectRoot } /temp/${ excludedPackage } .skip` ,
123- `${ projectRoot } /temp/${ excludedPackage } .api.json`
124- ) ;
125- }
126- }
162+ cleanup ( ) ;
127163 }
128164}
129165
@@ -137,12 +173,12 @@ async function generateDocs(
137173
138174 console . log ( `Temporarily modifying auth api-extractor.json for docgen.` ) ;
139175 // Use a special d.ts file for auth for doc gen only.
140- const authApiConfigOriginal = fs . readFileSync (
176+ authApiConfigOriginal = fs . readFileSync (
141177 `${ projectRoot } /packages/auth/api-extractor.json` ,
142178 'utf8'
143179 ) ;
144180 // Save original auth.md as well.
145- const authApiReportOriginal = fs . readFileSync (
181+ authApiReportOriginal = fs . readFileSync (
146182 `${ projectRoot } /common/api-review/auth.api.md` ,
147183 'utf8'
148184 ) ;
@@ -193,10 +229,13 @@ async function generateDocs(
193229 ) ;
194230 }
195231
196- if ( ! fs . existsSync ( tmpDir ) ) {
197- fs . mkdirSync ( tmpDir ) ;
232+ if ( fs . existsSync ( tmpDir ) ) {
233+ console . log ( `Removing old json temp dir: ${ tmpDir } .` ) ;
234+ fs . rmSync ( tmpDir , { recursive : true , force : true } ) ;
198235 }
199236
237+ fs . mkdirSync ( tmpDir ) ;
238+
200239 // TODO: Throw error if path doesn't exist once all packages add markdown support.
201240 const apiJsonDirectories = (
202241 await mapWorkspaceToPackages ( [ `${ projectRoot } /packages/*` ] )
0 commit comments