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/**
@@ -131,7 +131,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
131131 return filesToKeep . some ( reqPath => filePath . startsWith ( reqPath ) ) ;
132132 } ;
133133
134- const { owner, repo, tag, templateDir : dir } = this . remoteConfig ;
134+ const { owner, repo, tag, templateDir : dir } = this . remoteConfig ;
135135 /**
136136 * When the tar file is downloaded and extracted it creates a dir structure like
137137 * ${repo}-${tag}
@@ -146,7 +146,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
146146
147147 try {
148148 if ( ! existsSync ( outputDir ) ) {
149- mkdirSync ( outputDir , { recursive : true } ) ;
149+ mkdirSync ( outputDir , { recursive : true } ) ;
150150 }
151151
152152 const response = await fetch ( url ) ;
@@ -195,51 +195,61 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
195195
196196 async setupFiles ( ) {
197197 this . log ( '🛠️ Configuring files to meet your requirements...' ) ;
198+ if ( this . options . applicationClassName && this . options . relativePathToApp ) {
199+ let appImport = '' ;
198200
199- let appImport = '' ;
201+ const isDefaultExport = await this . _isDefaultExport (
202+ this . options . applicationClassName ,
203+ this . options . relativePathToApp ,
204+ ) ;
200205
201- const isDefaultExport = await this . _isDefaultExport (
202- this . options . applicationClassName ! ,
203- this . options . relativePathToApp ! ,
204- ) ;
206+ const appImportPath = this . _getAppImportPath (
207+ this . options . relativePathToApp ,
208+ ) ;
205209
206- const appImportPath = this . _getAppImportPath (
207- this . options . relativePathToApp ! ,
208- ) ;
210+ if ( ! isDefaultExport ) {
211+ appImport = `import {${ this . options . applicationClassName } } from '${ appImportPath } '` ;
212+ } else {
213+ appImport = `import ${ this . options . applicationClassName } from '${ appImportPath } ` ;
214+ }
209215
210- if ( ! isDefaultExport ) {
211- appImport = `import {${ this . options . applicationClassName } } from '${ appImportPath } '` ;
216+ await this . _updateFile (
217+ this . destinationPath (
218+ `${ this . options . dir } /${ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
219+ } `,
220+ ) ,
221+ '{{app_class_name_placeholder}}' ,
222+ this . options . applicationClassName ,
223+ ) ;
224+
225+ await this . _updateFile (
226+ this . destinationPath (
227+ `${ this . options . dir } /${ ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
228+ } `,
229+ ) ,
230+ '{{app_import_placeholder}}' ,
231+ appImport ,
232+ ) ;
212233 } else {
213- appImport = `import ${ this . options . applicationClassName } from '${ appImportPath } ` ;
234+ // Handle the case where applicationClassName or relativePathToApp is undefined
235+ this . log . error ( "Application class name or relative path to app is undefined." ) ;
214236 }
215237
216- await this . _updateFile (
217- this . destinationPath (
218- `${ this . options . dir } /${
219- ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
220- } `,
221- ) ,
222- '{{app_class_name_placeholder}}' ,
223- this . options . applicationClassName ! ,
224- ) ;
225238
226- await this . _updateFile (
227- this . destinationPath (
228- `${ this . options . dir } /${
229- ( this [ this . options . iac ! ] as LambdaConfig ) . handlerFile
230- } `,
231- ) ,
232- '{{app_import_placeholder}}' ,
233- appImport ,
234- ) ;
235239 }
236240
237241 async updatePackageJsonName ( ) {
238- await this . _updateFile (
239- this . destinationPath ( `${ this . options . dir } /package.json` ) ,
240- '{{package_json_name_placeholder}}' ,
241- this . options . packageJsonName ! ,
242- ) ;
242+ if ( this . options . packageJsonName ) {
243+ await this . _updateFile (
244+ this . destinationPath ( `${ this . options . dir } /package.json` ) ,
245+ '{{package_json_name_placeholder}}' ,
246+ this . options . packageJsonName ,
247+ ) ;
248+ } else {
249+ // Handle the case where applicationClassName or relativePathToApp is undefined
250+ this . log . error ( "packageJsonName is undefined." ) ;
251+ }
252+
243253 }
244254
245255 async configureEnvs ( ) {
@@ -248,19 +258,17 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
248258 const keysToCreate = await this . _getEnvKeys ( envFile ) ;
249259 await this . _appendEmptyKeysToEnv (
250260 this . destinationPath (
251- `${ this . options . dir } /${
252- ( this [ this . options . iac ! ] as IacConfig ) . envSchemaFile
261+ `${ this . options . dir } /${ ( this [ this . options . iac ! ] as IacConfig ) . envSchemaFile
253262 } `,
254263 ) ,
255264 keysToCreate ,
256265 ) ;
257266
258267 // Create entries for env variables in stack
259268 try {
260- const { project, sourcefile} = this . _parseTsFile (
269+ const { project, sourcefile } = this . _parseTsFile (
261270 this . destinationPath (
262- `${ this . options . dir ! } /${
263- ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
271+ `${ this . options . dir ! } /${ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
264272 } `,
265273 ) ,
266274 ) ;
@@ -292,8 +300,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
292300 this . log . ok ( 'Your files are ready for action! 🎉' ) ;
293301 } catch ( error ) {
294302 this . log . error (
295- `Failed to update env vars of lambda stack in ${ this . options . dir } /${
296- ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
303+ `Failed to update env vars of lambda stack in ${ this . options . dir } /${ ( this [ this . options . iac ! ] as IacConfig ) . mainStackFile
297304 } .`,
298305 error ,
299306 ) ;
@@ -361,19 +368,25 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
361368 cwd : this . destinationRoot ( ) ,
362369 } ) ;
363370
364- this . spawnCommandSync ( 'npm' , [ 'install' ] , {
365- cwd : this . destinationPath ( this . options . dir ! ) ,
366- } ) ;
371+ if ( this . options . dir ) {
372+ this . spawnCommandSync ( 'npm' , [ 'install' ] , {
373+ cwd : this . destinationPath ( this . options . dir ) ,
374+ } ) ;
375+ } else {
376+ // Handle the case where applicationClassName or relativePathToApp is undefined
377+ this . log . error ( "dir is undefined." ) ;
378+ }
379+
367380 }
368381
369382 async end ( ) {
370- const { owner, repo, templateDir : dir } = this . remoteConfig ;
383+ const { owner, repo, templateDir : dir } = this . remoteConfig ;
371384 this . log ( `
372385${ chalk . green ( "🚀 Hooray! You're all set to launch your app." ) }
373386Next steps:
374387 1. Fill up the environment variables in your ${ chalk . yellow (
375- this . options . dir ,
376- ) } directory.
388+ this . options . dir ,
389+ ) } directory.
377390 2. Build your app.
378391 3. Run ${ chalk . blue ( `cdktf deploy ${ this . options . iac } ` ) } to deploy the iac.
379392
@@ -486,7 +499,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
486499 _parseTsFile ( filePath : string ) {
487500 const project = new Project ( ) ;
488501 const sourcefile = project . addSourceFileAtPathIfExists ( filePath ) ;
489- return { project, sourcefile} ;
502+ return { project, sourcefile } ;
490503 }
491504
492505 /**
@@ -526,9 +539,9 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
526539 encoding : BufferEncoding = 'utf-8' ,
527540 ) {
528541 try {
529- let data = await readFile ( filePath , { encoding} ) ;
542+ let data = await readFile ( filePath , { encoding } ) ;
530543 data = data . replace ( new RegExp ( placeholder , 'g' ) , replaceWith ) ;
531- await writeFile ( filePath , data , { encoding} ) ;
544+ await writeFile ( filePath , data , { encoding } ) ;
532545 } catch ( error ) {
533546 if ( error instanceof Error ) {
534547 this . log . error ( error . message ) ;
0 commit comments