@@ -4,11 +4,20 @@ const ora = require('ora')
4
4
const { Command } = require ( 'commander' ) ;
5
5
const { createPromptModule } = require ( 'inquirer' ) ;
6
6
const { execSync } = require ( 'node:child_process' )
7
- const actionHandlers = require ( './scripts' )
8
- const { logInfo, getExistingComponent, getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways } = require ( './scripts/scripts.module' ) ;
9
7
const { cwd } = require ( 'node:process' ) ;
10
- const program = new Command ( )
11
- const prompt = createPromptModule ( )
8
+ const { readFileSync } = require ( 'node:fs' ) ;
9
+ const path = require ( 'node:path' ) ;
10
+ const { cwd } = require ( 'node:process' ) ;
11
+ const { readFileSync } = require ( 'node:fs' ) ;
12
+ const path = require ( 'node:path' ) ;
13
+ const actionHandlers = require ( './scripts' )
14
+ const { logInfo, getExistingComponent, getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways, readFileContent } = require ( './scripts/scripts.module' ) ;
15
+
16
+ const program = new Command ( ) ;
17
+ const packageJsonPath = path . join ( __dirname , 'package.json' ) ;
18
+ const packageJSON = JSON . parse ( readFileSync ( packageJsonPath , { 'encoding' : 'utf8' } ) ) ;
19
+ program . version ( packageJSON . version ) ; //get library version set by release script
20
+ const prompt = createPromptModule ( ) ;
12
21
program
13
22
. command ( 'add' )
14
23
. description ( 'Adds dependencies at given workspace and updates package.json' )
@@ -125,9 +134,9 @@ program
125
134
. then ( answers => {
126
135
let existing_services = [ ]
127
136
try {
128
- existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
137
+ existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
129
138
} catch ( error ) {
130
-
139
+
131
140
}
132
141
switch ( answers . resource ) {
133
142
case 'monorepo' :
@@ -236,7 +245,7 @@ program
236
245
] ) . then ( ( answers ) => actionHandlers . scaffoldNewLibrary ( { answers : { ...answers , private : false } } ) )
237
246
break
238
247
case 'app' :
239
- const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } ) || [ ]
248
+ const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } ) || [ ]
240
249
const formatServiceName = ( service ) => `${ service . name } : ${ service . port } ` ;
241
250
prompt ( [
242
251
{
@@ -300,7 +309,7 @@ program
300
309
break ;
301
310
case 'gateway' :
302
311
const apps = getExistingApps ( { currentDir : cwd ( ) } ) ;
303
- if ( ! apps ) {
312
+ if ( ! apps ) {
304
313
logInfo ( { message : `No apps found in this project. You need to have atleast one app to generate a gateway` } )
305
314
ora ( ) . info ( `Run 'suite generate' to create one...` )
306
315
process . exit ( 0 ) ;
@@ -356,16 +365,45 @@ program
356
365
} ) ;
357
366
} ) ;
358
367
program
359
- . command ( 'remove' )
360
- . description ( 'Clean remove a monorepo resource plus all the associated files. No residual files remain behind' )
361
- . option ( 'service' , 'remove service and associated files' )
362
- . option ( 'app' , 'remove app and associated files' )
363
- . option ( 'library' , 'remove library and associated files' )
364
- . option ( 'microservice' , 'remove microservice and associated files' )
365
- . option ( 'gateway' , 'remove gateway and associated files' )
366
- . action ( async ( options ) => {
367
- console . log ( { options} )
368
- await actionHandlers . dockerPrune ( { options } )
369
- } ) ;
368
+ . command ( 'remove <resource> [resource_name]' )
369
+ . description ( 'Clean remove a monorepo resource or all resources of a specific type with the --all flag.' )
370
+ . option ( '-f, --force' , 'Force removal without confirmation' )
371
+ . option ( '--all' , 'Remove all resources of the specified type' ) // Add --all flag
372
+ . action ( async ( resource , resource_name , options ) => {
373
+ const spinner = ora ( ) ;
374
+
375
+ // Validate --all flag usage
376
+ if ( ! resource_name && ! options . all ) {
377
+ spinner . fail ( 'You must provide either a resource name or the --all flag to remove all resources.' ) ;
378
+ return ;
379
+ }
380
+
381
+ try {
382
+ // Confirm the deletion if --force is not provided
383
+ if ( ! options . force ) {
384
+ const { confirmRemoval } = await prompt ( [
385
+ {
386
+ type : 'confirm' ,
387
+ name : 'confirmRemoval' ,
388
+ message : `Are you sure you want to remove ${ options . all ? `all ${ resource } s` : `${ resource } "${ resource_name } "` } ?` ,
389
+ default : false
390
+ }
391
+ ] ) ;
392
+
393
+ if ( ! confirmRemoval ) {
394
+ spinner . info ( 'Operation cancelled.' ) ;
395
+ return ;
396
+ }
397
+ }
398
+
399
+ // Call the resource removal handler
400
+ await actionHandlers . removeResource ( { answers : { resource, resource_name, options } } ) ;
401
+
402
+ } catch ( error ) {
403
+ spinner . fail ( `Failed to remove ${ resource } ${ resource_name || 'resources' } : ${ error . message } ` ) ;
404
+ }
405
+ } ) ;
406
+
407
+
370
408
program . parse ( process . argv ) ;
371
409
module . exports = program
0 commit comments