@@ -4,11 +4,20 @@ const ora = require('ora')
44const { Command } = require ( 'commander' ) ;
55const { createPromptModule } = require ( 'inquirer' ) ;
66const { execSync } = require ( 'node:child_process' )
7- const actionHandlers = require ( './scripts' )
8- const { logInfo, getExistingComponent, getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways } = require ( './scripts/scripts.module' ) ;
97const { 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 ( ) ;
1221program
1322 . command ( 'add' )
1423 . description ( 'Adds dependencies at given workspace and updates package.json' )
@@ -125,9 +134,9 @@ program
125134 . then ( answers => {
126135 let existing_services = [ ]
127136 try {
128- existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
137+ existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
129138 } catch ( error ) {
130-
139+
131140 }
132141 switch ( answers . resource ) {
133142 case 'monorepo' :
@@ -236,7 +245,7 @@ program
236245 ] ) . then ( ( answers ) => actionHandlers . scaffoldNewLibrary ( { answers : { ...answers , private : false } } ) )
237246 break
238247 case 'app' :
239- const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } ) || [ ]
248+ const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } ) || [ ]
240249 const formatServiceName = ( service ) => `${ service . name } : ${ service . port } ` ;
241250 prompt ( [
242251 {
@@ -300,7 +309,7 @@ program
300309 break ;
301310 case 'gateway' :
302311 const apps = getExistingApps ( { currentDir : cwd ( ) } ) ;
303- if ( ! apps ) {
312+ if ( ! apps ) {
304313 logInfo ( { message : `No apps found in this project. You need to have atleast one app to generate a gateway` } )
305314 ora ( ) . info ( `Run 'suite generate' to create one...` )
306315 process . exit ( 0 ) ;
@@ -356,16 +365,45 @@ program
356365 } ) ;
357366 } ) ;
358367 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+
370408program . parse ( process . argv ) ;
371409module . exports = program
0 commit comments