@@ -5,7 +5,7 @@ const { Command } = require('commander');
55const { createPromptModule } = require ( 'inquirer' ) ;
66const { execSync } = require ( 'node:child_process' )
77const actionHandlers = require ( './scripts' )
8- const { logInfo, getExistingServices , getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways } = require ( './scripts/scripts.module' ) ;
8+ const { logInfo, getExistingComponent , getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways } = require ( './scripts/scripts.module' ) ;
99const { cwd } = require ( 'node:process' ) ;
1010const program = new Command ( )
1111const prompt = createPromptModule ( )
@@ -123,6 +123,7 @@ program
123123 }
124124 ] )
125125 . then ( answers => {
126+ const existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
126127 switch ( answers . resource ) {
127128 case 'monorepo' :
128129 // Additional prompts specific to 'monorepo' resource
@@ -185,28 +186,38 @@ program
185186 } ) ;
186187 break ;
187188 case 'service' :
188- const existing_services = getExistingServices ( { currentDir : cwd ( ) } )
189189 prompt ( [
190190 {
191191 type : 'input' ,
192192 name : 'service_name' ,
193193 message : 'Enter service name:' ,
194194 // TODO: validate workspace compliant name using regex
195195 validate : input => input ? true : 'Name cannot be empty' ,
196- } , {
197- type : 'input' ,
198- name : 'port' ,
199- message : 'Enter port (optional):' ,
200- default : getNextAvailablePort ( { services : existing_services } ) ,
201- validate : input => input === '' || ! isNaN ( input ) ? true : 'Port must be a number.'
202- }
203- ] ) . then ( ( answers ) => actionHandlers . scaffoldNewService ( {
204- answers : {
205- ...answers ,
206- private : true ,
207- port : parseFloat ( answers . port )
208- }
209- } ) )
196+ } ,
197+ ] ) . then ( ( ans ) => {
198+ const { service_name } = ans ;
199+ const service_idx = existing_services . findIndex ( ( s ) => s . name === service_name ) ;
200+ // if service exists return the port otherwise generate next available port
201+ const next_port = existing_services [ service_idx ] ?. port || getNextAvailablePort ( { key : existing_services , port : 'port' } ) ;
202+ prompt ( [
203+ {
204+ type : 'input' ,
205+ name : 'port' ,
206+ message : 'Enter port (optional):' ,
207+ default : next_port ,
208+ validate : input => input === '' || ! isNaN ( input ) ? true : 'Port must be a number.'
209+ }
210+ ] ) . then ( ( answers ) => {
211+ actionHandlers . scaffoldNewService ( {
212+ answers : {
213+ ...answers ,
214+ service_name,
215+ private : true ,
216+ port : parseFloat ( answers . port )
217+ }
218+ } )
219+ } ) ;
220+ } ) ;
210221 break ;
211222 case 'library' :
212223 prompt ( [
@@ -220,9 +231,8 @@ program
220231 ] ) . then ( ( answers ) => actionHandlers . scaffoldNewLibrary ( { answers : { ...answers , private : false } } ) )
221232 break
222233 case 'app' :
223- const all_services = getExistingServices ( { currentDir : cwd ( ) } )
234+ const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } )
224235 const formatServiceName = ( service ) => `${ service . name } : ${ service . port } ` ;
225-
226236 prompt ( [
227237 {
228238 type : 'input' ,
@@ -233,42 +243,55 @@ program
233243 type : 'checkbox' ,
234244 name : 'services' ,
235245 message : 'Select services' ,
236- choices : all_services . map ( service => ( {
246+ choices : existing_services . map ( service => ( {
237247 name : formatServiceName ( service ) ,
238248 value : service ,
239249 checked : true , // Default all services to be selected
240250 } ) ) ,
241- } ,
242- {
243- type : 'input' ,
244- name : 'gateway_port' ,
245- message : 'Enter port (optional):' ,
246- default : 8080 ,
247- validate : input => ! isNaN ( input ) ? true : 'Port must be a number.'
248- } ,
249- {
250- type : 'input' ,
251- name : 'api_version' ,
252- message : 'Whats the api version? (optional):' ,
253- default : 'v1' ,
254- } ,
255- {
256- type : 'input' ,
257- name : 'gateway_cache_period' ,
258- message : 'How long do you want the gateway to cache data (optional):' ,
259- default : 3600 ,
260- validate : input => ! isNaN ( input ) ? true : 'Caching period must be a number.'
261- } ,
262- {
263- type : 'input' ,
264- name : 'gateway_timeout' ,
265- message : 'How long should a request take before timing out (optional):' ,
266- default : 300 ,
267- validate : input => input === '' || ! isNaN ( input ) ? true : 'Timeout must be a number.'
268- }
269- ] ) . then ( answers => {
270- scaffoldApp ( { answers } )
271- } )
251+ } ] ) . then ( ( ans ) => {
252+ const { app_name } = ans ;
253+ const app_idx = existing_apps . findIndex ( ( a ) => a . name === app_name ) ;
254+ // if app exists return the port otherwise generate next available port
255+ const next_port = existing_apps [ app_idx ] ?. GATEWAY_PORT || getNextAvailablePort ( { key : existing_apps , port : 'GATEWAY_PORT' } ) ;
256+ prompt ( [
257+ {
258+ type : 'input' ,
259+ name : 'gateway_port' ,
260+ message : 'Enter gateway port (optional):' ,
261+ default : next_port ,
262+ validate : input => ! isNaN ( input ) ? true : 'Port must be a number.'
263+ } ,
264+ {
265+ type : 'input' ,
266+ name : 'api_version' ,
267+ message : 'Whats the api version? (optional):' ,
268+ default : 'v1' ,
269+ } ,
270+ {
271+ type : 'input' ,
272+ name : 'gateway_cache_period' ,
273+ message : 'How long do you want the gateway to cache data (optional):' ,
274+ default : 3600 ,
275+ validate : input => ! isNaN ( input ) ? true : 'Caching period must be a number.'
276+ } ,
277+ {
278+ type : 'input' ,
279+ name : 'gateway_timeout' ,
280+ message : 'How long should a request take before timing out (optional):' ,
281+ default : 300 ,
282+ validate : input => input === '' || ! isNaN ( input ) ? true : 'Timeout must be a number.'
283+ }
284+ ] ) . then ( ( answers ) => {
285+ scaffoldApp ( {
286+ answers : {
287+ ...answers ,
288+ ...ans ,
289+ port : parseFloat ( answers . gateway_port )
290+
291+ }
292+ } )
293+ } ) ;
294+ } )
272295 break ;
273296 case 'gateway' :
274297 const all_apps = getExistingApps ( { currentDir : cwd ( ) } ) ;
0 commit comments