@@ -175,6 +175,29 @@ function removeFile(filePath) {
175175 } ) ;
176176}
177177
178+ /**
179+ * Copy file
180+ * @param {string } filePath
181+ * @param {string } [backupFileExtension=BACKUPFILE_EXTENSION]
182+ * @returns {Promise<*> }
183+ */
184+ async function backupFile (
185+ filePath ,
186+ backupFileExtension = BACKUPFILE_EXTENSION ,
187+ ) {
188+ return new Promise ( ( resolve , reject ) => {
189+ const targetFile = filePath . concat ( `.${ backupFileExtension } ` )
190+ try {
191+ fs . copyFile ( filePath , targetFile , err => {
192+ if ( err ) throw err ;
193+ } ) ;
194+ resolve ( targetFile ) ;
195+ } catch ( err ) {
196+ reject ( err ) ;
197+ }
198+ } ) ;
199+ }
200+
178201/**
179202 * Overwrite file from copy
180203 * @param {string } filePath
@@ -230,6 +253,7 @@ async function generateComponent({ name, memo }) {
230253 return component ;
231254}
232255
256+
233257/**
234258 * Test the container generator and rollback when successful
235259 * @param {string } name - Container name
@@ -271,6 +295,8 @@ async function generateContainer({ name, memo }) {
271295 * @returns {Promise<[string]> }
272296 */
273297async function generateComponents ( components ) {
298+ const typesPath = '../../app/types/index.d.ts'
299+
274300 const promises = components . map ( async component => {
275301 let result ;
276302
@@ -283,8 +309,20 @@ async function generateComponents(components) {
283309 return result ;
284310 } ) ;
285311
312+ const backupTypes = await backupFile ( typesPath )
313+ . then ( feedbackToUser ( "Generated 'types/index.ds.ts.rbgen'" ) )
314+ . catch ( reason => reportErrors ( reason ) ) ;
315+
286316 const results = await Promise . all ( promises ) ;
287317
318+ await restoreModifiedFile ( backupTypes )
319+ . then ( feedbackToUser ( `Restored: ${ typesPath } ` ) )
320+ . catch ( reason => reportErrors ( reason ) ) ;
321+
322+ await removeFile ( backupTypes )
323+ . then ( feedbackToUser ( `Removed: ${ backupTypes } ` ) )
324+ . catch ( reason => reportErrors ( reason ) ) ;
325+
288326 return results ;
289327}
290328
0 commit comments