11import fetch from 'node-fetch' ;
2- import { existsSync , mkdirSync } from 'node:fs' ;
2+ import { existsSync , mkdirSync } from 'node:fs' ;
33import {
44 appendFile ,
55 readFile ,
@@ -15,9 +15,9 @@ import {
1515 Project ,
1616 PropertyAssignmentStructure ,
1717} from 'ts-morph' ;
18- import { BaseGenerator } from '../../base-generator' ;
19- import { IacList } from '../../enum' ;
20- import { CdkOptions } from '../../types' ;
18+ import { BaseGenerator } from '../../base-generator' ;
19+ import { IacList } from '../../enum' ;
20+ import { CdkOptions } from '../../types' ;
2121const chalk = require ( 'chalk' ) ; //NOSONAR
2222
2323/**
@@ -101,12 +101,8 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
101101 ) ;
102102 }
103103
104- if ( ! this . options . dir ) {
105- this . options . dir = 'cdk' ;
106- }
107- if ( ! this . options . packageJsonName ) {
108- this . options . packageJsonName = `arc-cdktf` ;
109- }
104+ this . options . dir ??= 'cdk' ;
105+ this . options . packageJsonName ??= `arc-cdktf` ;
110106 if ( ! this . options . iac ) {
111107 throw new Error (
112108 'Error: The "iac" option is missing. Please provide an iac name.' ,
@@ -131,7 +127,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
131127 return filesToKeep . some ( reqPath => filePath . startsWith ( reqPath ) ) ;
132128 } ;
133129
134- const { owner, repo, tag, templateDir : dir } = this . remoteConfig ;
130+ const { owner, repo, tag, templateDir : dir } = this . remoteConfig ;
135131 /**
136132 * When the tar file is downloaded and extracted it creates a dir structure like
137133 * ${repo}-${tag}
@@ -146,7 +142,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
146142
147143 try {
148144 if ( ! existsSync ( outputDir ) ) {
149- mkdirSync ( outputDir , { recursive : true } ) ;
145+ mkdirSync ( outputDir , { recursive : true } ) ;
150146 }
151147
152148 const response = await fetch ( url ) ;
@@ -215,7 +211,8 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
215211
216212 await this . _updateFile (
217213 this . destinationPath (
218- `${ this . options . dir } /${ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
214+ `${ this . options . dir } /${
215+ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
219216 } `,
220217 ) ,
221218 '{{app_class_name_placeholder}}' ,
@@ -224,18 +221,19 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
224221
225222 await this . _updateFile (
226223 this . destinationPath (
227- `${ this . options . dir } /${ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
224+ `${ this . options . dir } /${
225+ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
228226 } `,
229227 ) ,
230228 '{{app_import_placeholder}}' ,
231229 appImport ,
232230 ) ;
233231 } else {
234232 // Handle the case where applicationClassName or relativePathToApp is undefined
235- this . log . error ( "Application class name or relative path to app is undefined." ) ;
233+ this . log . error (
234+ 'Application class name or relative path to app is undefined.' ,
235+ ) ;
236236 }
237-
238-
239237 }
240238
241239 async updatePackageJsonName ( ) {
@@ -247,9 +245,8 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
247245 ) ;
248246 } else {
249247 // Handle the case where applicationClassName or relativePathToApp is undefined
250- this . log . error ( " packageJsonName is undefined." ) ;
248+ this . log . error ( ' packageJsonName is undefined.' ) ;
251249 }
252-
253250 }
254251
255252 async configureEnvs ( ) {
@@ -258,17 +255,19 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
258255 const keysToCreate = await this . _getEnvKeys ( envFile ) ;
259256 await this . _appendEmptyKeysToEnv (
260257 this . destinationPath (
261- `${ this . options . dir } /${ ( this [ this . options . iac ! ] as IacConfig ) . envSchemaFile
258+ `${ this . options . dir } /${
259+ ( this [ this . options . iac ! ] as IacConfig ) . envSchemaFile
262260 } `,
263261 ) ,
264262 keysToCreate ,
265263 ) ;
266264
267265 // Create entries for env variables in stack
268266 try {
269- const { project, sourcefile } = this . _parseTsFile (
267+ const { project, sourcefile} = this . _parseTsFile (
270268 this . destinationPath (
271- `${ this . options . dir ! } /${ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
269+ `${ this . options . dir ! } /${
270+ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
272271 } `,
273272 ) ,
274273 ) ;
@@ -300,7 +299,8 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
300299 this . log . ok ( 'Your files are ready for action! 🎉' ) ;
301300 } catch ( error ) {
302301 this . log . error (
303- `Failed to update env vars of lambda stack in ${ this . options . dir } /${ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
302+ `Failed to update env vars of lambda stack in ${ this . options . dir } /${
303+ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
304304 } .`,
305305 error ,
306306 ) ;
@@ -374,19 +374,18 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
374374 } ) ;
375375 } else {
376376 // Handle the case where applicationClassName or relativePathToApp is undefined
377- this . log . error ( " dir is undefined." ) ;
377+ this . log . error ( ' dir is undefined.' ) ;
378378 }
379-
380379 }
381380
382381 async end ( ) {
383- const { owner, repo, templateDir : dir } = this . remoteConfig ;
382+ const { owner, repo, templateDir : dir } = this . remoteConfig ;
384383 this . log ( `
385384${ chalk . green ( "🚀 Hooray! You're all set to launch your app." ) }
386385Next steps:
387386 1. Fill up the environment variables in your ${ chalk . yellow (
388- this . options . dir ,
389- ) } directory.
387+ this . options . dir ,
388+ ) } directory.
390389 2. Build your app.
391390 3. Run ${ chalk . blue ( `cdktf deploy ${ this . options . iac } ` ) } to deploy the iac.
392391
@@ -499,7 +498,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
499498 _parseTsFile ( filePath : string ) {
500499 const project = new Project ( ) ;
501500 const sourcefile = project . addSourceFileAtPathIfExists ( filePath ) ;
502- return { project, sourcefile } ;
501+ return { project, sourcefile} ;
503502 }
504503
505504 /**
@@ -539,9 +538,9 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
539538 encoding : BufferEncoding = 'utf-8' ,
540539 ) {
541540 try {
542- let data = await readFile ( filePath , { encoding } ) ;
541+ let data = await readFile ( filePath , { encoding} ) ;
543542 data = data . replace ( new RegExp ( placeholder , 'g' ) , replaceWith ) ;
544- await writeFile ( filePath , data , { encoding } ) ;
543+ await writeFile ( filePath , data , { encoding} ) ;
545544 } catch ( error ) {
546545 if ( error instanceof Error ) {
547546 this . log . error ( error . message ) ;
@@ -595,6 +594,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
595594 JSON_INDENTATION ,
596595 ) } `,
597596 ) ;
597+ throw error ;
598598 }
599599 }
600600
@@ -631,7 +631,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
631631 return pattern . test ( fileContent ) ;
632632 } catch ( error ) {
633633 this . log . error ( `Error reading ${ filePath } ` ) ;
634- return false ;
634+ throw error ;
635635 }
636636 }
637637
@@ -648,6 +648,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
648648 this . log . error (
649649 `Error moving file from ${ sourcePath } to ${ destinationPath } ` ,
650650 ) ;
651+ throw error ;
651652 }
652653 }
653654}
0 commit comments