@@ -5,7 +5,7 @@ const { Command } = require('commander');
5
5
const { createPromptModule } = require ( 'inquirer' ) ;
6
6
const { execSync } = require ( 'node:child_process' )
7
7
const 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' ) ;
9
9
const { cwd } = require ( 'node:process' ) ;
10
10
const program = new Command ( )
11
11
const prompt = createPromptModule ( )
@@ -123,6 +123,7 @@ program
123
123
}
124
124
] )
125
125
. then ( answers => {
126
+ const existing_services = getExistingComponent ( { key : 'services' , currentDir : cwd ( ) } )
126
127
switch ( answers . resource ) {
127
128
case 'monorepo' :
128
129
// Additional prompts specific to 'monorepo' resource
@@ -185,28 +186,38 @@ program
185
186
} ) ;
186
187
break ;
187
188
case 'service' :
188
- const existing_services = getExistingServices ( { currentDir : cwd ( ) } )
189
189
prompt ( [
190
190
{
191
191
type : 'input' ,
192
192
name : 'service_name' ,
193
193
message : 'Enter service name:' ,
194
194
// TODO: validate workspace compliant name using regex
195
195
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
+ } ) ;
210
221
break ;
211
222
case 'library' :
212
223
prompt ( [
@@ -220,9 +231,8 @@ program
220
231
] ) . then ( ( answers ) => actionHandlers . scaffoldNewLibrary ( { answers : { ...answers , private : false } } ) )
221
232
break
222
233
case 'app' :
223
- const all_services = getExistingServices ( { currentDir : cwd ( ) } )
234
+ const existing_apps = getExistingComponent ( { key : 'apps' , currentDir : cwd ( ) } )
224
235
const formatServiceName = ( service ) => `${ service . name } : ${ service . port } ` ;
225
-
226
236
prompt ( [
227
237
{
228
238
type : 'input' ,
@@ -233,42 +243,55 @@ program
233
243
type : 'checkbox' ,
234
244
name : 'services' ,
235
245
message : 'Select services' ,
236
- choices : all_services . map ( service => ( {
246
+ choices : existing_services . map ( service => ( {
237
247
name : formatServiceName ( service ) ,
238
248
value : service ,
239
249
checked : true , // Default all services to be selected
240
250
} ) ) ,
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
+ } )
272
295
break ;
273
296
case 'gateway' :
274
297
const all_apps = getExistingApps ( { currentDir : cwd ( ) } ) ;
0 commit comments