@@ -44,7 +44,10 @@ const volumeValidation = input => {
44
44
45
45
const writeConfig = ( configPath , newConfig ) => {
46
46
// init config object
47
- const config = { name : newConfig . name , restart : newConfig . restart } ;
47
+ const config = { name : newConfig . name } ;
48
+ if ( newConfig . restart && newConfig . restart . length ) {
49
+ config . restart = newConfig . restart ;
50
+ }
48
51
if ( newConfig . domain && newConfig . domain . length ) {
49
52
config . domain = newConfig . domain ;
50
53
}
@@ -116,59 +119,7 @@ const writeConfig = (configPath, newConfig) => {
116
119
console . log ( chalk . green ( 'Config created!' ) ) ;
117
120
} ;
118
121
119
- exports . command = [ 'config' , 'init' ] ;
120
- exports . describe = 'generate new config file for current project' ;
121
- exports . builder = {
122
- func : {
123
- alias : 'f' ,
124
- description : 'generate a new config for function deployment' ,
125
- } ,
126
- } ;
127
- exports . handler = async ( { func} = { } ) => {
128
- const workdir = process . cwd ( ) ;
129
- const folderName = path . basename ( workdir ) ;
130
- const configPath = path . join ( workdir , 'exoframe.json' ) ;
131
- let defaultConfig = {
132
- name : folderName ,
133
- domain : '' ,
134
- project : '' ,
135
- restart : 'on-failure:2' ,
136
- env : undefined ,
137
- labels : undefined ,
138
- hostname : '' ,
139
- template : '' ,
140
- rateLimit : {
141
- period : '1s' ,
142
- average : 1 ,
143
- burst : 5 ,
144
- } ,
145
- basicAuth : false ,
146
- function : false ,
147
- } ;
148
- try {
149
- fs . statSync ( configPath ) ;
150
- console . log ( chalk . green ( 'Config already exists! Editing..' ) ) ;
151
- defaultConfig = JSON . parse ( fs . readFileSync ( configPath ) . toString ( ) ) ;
152
- } catch ( e ) {
153
- // check if config didn't exist
154
- if ( e . message . includes ( 'ENOENT' ) ) {
155
- console . log ( 'Creating new config..' ) ;
156
- } else {
157
- // if there was any parsing error - show message and die
158
- console . log ( chalk . red ( 'Error parsing existing config! Please make sure it is valid and try again.' ) ) ;
159
- return ;
160
- }
161
- }
162
-
163
- if ( func ) {
164
- console . log ( 'Creating new config for function deployment..' ) ;
165
- // set function flag to true
166
- defaultConfig . function = true ;
167
- // write config to file
168
- writeConfig ( configPath , defaultConfig ) ;
169
- return ;
170
- }
171
-
122
+ const generatePrompts = defaultConfig => {
172
123
// ask user for values
173
124
// generate and show choices
174
125
const prompts = [ ] ;
@@ -180,19 +131,47 @@ exports.handler = async ({func} = {}) => {
180
131
validate,
181
132
filter,
182
133
} ) ;
134
+
135
+ // function deployment part
136
+ prompts . push ( {
137
+ type : 'confirm' ,
138
+ name : 'function' ,
139
+ message : 'Deploy as function? [optional]:' ,
140
+ default : Boolean ( defaultConfig . function ) ,
141
+ } ) ;
142
+ prompts . push ( {
143
+ type : 'input' ,
144
+ name : 'functionType' ,
145
+ message : 'Function type [http, worker, trigger, custom]:' ,
146
+ default : defaultConfig . function && defaultConfig . function . type ? defaultConfig . function . type : 'http' ,
147
+ filter,
148
+ when : answers => answers . function ,
149
+ } ) ;
150
+ prompts . push ( {
151
+ type : 'input' ,
152
+ name : 'functionRoute' ,
153
+ message : 'Function route [optional]:' ,
154
+ default : defaultConfig . function && defaultConfig . function . route ? defaultConfig . function . route : '' ,
155
+ filter,
156
+ when : answers => answers . function ,
157
+ } ) ;
158
+
159
+ // default stuff
183
160
prompts . push ( {
184
161
type : 'input' ,
185
162
name : 'domain' ,
186
163
message : 'Domain [optional]:' ,
187
164
default : defaultConfig . domain ,
188
165
filter,
166
+ when : answers => ! answers . function ,
189
167
} ) ;
190
168
prompts . push ( {
191
169
type : 'input' ,
192
170
name : 'project' ,
193
171
message : 'Project [optional]:' ,
194
172
default : defaultConfig . project ,
195
173
filter,
174
+ when : answers => ! answers . function ,
196
175
} ) ;
197
176
prompts . push ( {
198
177
type : 'input' ,
@@ -205,6 +184,7 @@ exports.handler = async ({func} = {}) => {
205
184
: '' ,
206
185
filter,
207
186
validate : pairValidation ,
187
+ when : answers => ! answers . function ,
208
188
} ) ;
209
189
prompts . push ( {
210
190
type : 'input' ,
@@ -217,6 +197,7 @@ exports.handler = async ({func} = {}) => {
217
197
: '' ,
218
198
filter,
219
199
validate : pairValidation ,
200
+ when : answers => ! answers . function ,
220
201
} ) ;
221
202
prompts . push ( {
222
203
type : 'input' ,
@@ -225,12 +206,14 @@ exports.handler = async ({func} = {}) => {
225
206
default : defaultConfig . volumes ? defaultConfig . volumes . join ( ', ' ) : '' ,
226
207
filter,
227
208
validate : volumeValidation ,
209
+ when : answers => ! answers . function ,
228
210
} ) ;
229
211
prompts . push ( {
230
212
type : 'confirm' ,
231
213
name : 'enableRatelimit' ,
232
214
message : 'Enable rate-limit? [optional]' ,
233
- default : ! ! defaultConfig . rateLimit ,
215
+ default : defaultConfig . rateLimit && defaultConfig . rateLimit . enabled ,
216
+ when : answers => ! answers . function ,
234
217
} ) ;
235
218
prompts . push ( {
236
219
type : 'input' ,
@@ -262,27 +245,31 @@ exports.handler = async ({func} = {}) => {
262
245
message : 'Hostname [optional]:' ,
263
246
default : defaultConfig . hostname ,
264
247
filter,
248
+ when : answers => ! answers . function ,
265
249
} ) ;
266
250
prompts . push ( {
267
251
type : 'list' ,
268
252
name : 'restart' ,
269
253
message : 'Restart policy [optional]:' ,
270
254
default : defaultConfig . restart ,
271
- choices : [ 'no' , 'on-failure:2' , 'always' ] ,
255
+ choices : [ '' , 'no' , 'on-failure:2' , 'always' ] ,
256
+ when : answers => ! answers . function ,
272
257
} ) ;
273
258
prompts . push ( {
274
259
type : 'input' ,
275
260
name : 'template' ,
276
261
message : 'Template [optional]:' ,
277
262
default : defaultConfig . template ,
278
263
filter,
264
+ when : answers => ! answers . function ,
279
265
} ) ;
280
266
// docker image deployment part
281
267
prompts . push ( {
282
268
type : 'confirm' ,
283
269
name : 'deployWithImage' ,
284
270
message : 'Deploy using docker image? [optional]:' ,
285
271
default : Boolean ( defaultConfig . image ) ,
272
+ when : answers => ! answers . function ,
286
273
} ) ;
287
274
prompts . push ( {
288
275
type : 'input' ,
@@ -301,36 +288,13 @@ exports.handler = async ({func} = {}) => {
301
288
when : ( { deployWithImage} ) => deployWithImage ,
302
289
} ) ;
303
290
304
- // function deployment part
305
- prompts . push ( {
306
- type : 'confirm' ,
307
- name : 'function' ,
308
- message : 'Deploy as function? [optional]:' ,
309
- default : Boolean ( defaultConfig . function ) ,
310
- } ) ;
311
- prompts . push ( {
312
- type : 'input' ,
313
- name : 'functionType' ,
314
- message : 'Function type [http, worker, trigger, custom]:' ,
315
- default : defaultConfig . function && defaultConfig . function . type ? defaultConfig . function . type : 'http' ,
316
- filter,
317
- when : answers => answers . function ,
318
- } ) ;
319
- prompts . push ( {
320
- type : 'input' ,
321
- name : 'functionRoute' ,
322
- message : 'Function route [optional]:' ,
323
- default : defaultConfig . function && defaultConfig . function . route ? defaultConfig . function . route : '' ,
324
- filter,
325
- when : answers => answers . function ,
326
- } ) ;
327
-
328
291
// basic auth part
329
292
prompts . push ( {
330
293
type : 'confirm' ,
331
294
name : 'basicAuth' ,
332
295
message : 'Add a basic auth user? [optional]:' ,
333
296
default : Boolean ( defaultConfig . basicAuth ) ,
297
+ when : answers => ! answers . function ,
334
298
} ) ;
335
299
// prompts for recursive questions
336
300
const recursivePrompts = [ ] ;
@@ -365,12 +329,110 @@ exports.handler = async ({func} = {}) => {
365
329
}
366
330
} ;
367
331
368
- // get values from user
369
- const newConfig = await inquirer . prompt ( prompts ) ;
332
+ return { prompts , askForUsers } ;
333
+ } ;
370
334
371
- // update users for auth if needed
372
- if ( newConfig . basicAuth ) {
373
- newConfig . users = await askForUsers ( ) ;
335
+ exports . command = [ 'config' , 'init' ] ;
336
+ exports . describe = 'generate new config file for current project' ;
337
+ exports . builder = {
338
+ func : {
339
+ alias : 'f' ,
340
+ description : 'generate a new config for function deployment' ,
341
+ } ,
342
+ domain : {
343
+ alias : 'd' ,
344
+ description : 'sets the domain (enables non-interactive mode)' ,
345
+ } ,
346
+ project : {
347
+ alias : 'p' ,
348
+ description : 'sets the project name (enables non-interactive mode)' ,
349
+ } ,
350
+ name : {
351
+ alias : 'n' ,
352
+ description : 'sets the name (enables non-interactive mode)' ,
353
+ } ,
354
+ restart : {
355
+ alias : 'r' ,
356
+ description : 'sets the restart option (enables non-interactive mode)' ,
357
+ } ,
358
+ hostname : {
359
+ description : 'sets the hostname (enables non-interactive mode)' ,
360
+ } ,
361
+ } ;
362
+ exports . handler = async ( { _, $0, func, ...args } = { } ) => {
363
+ const workdir = process . cwd ( ) ;
364
+ const folderName = path . basename ( workdir ) ;
365
+ const nonInteractive = Object . keys ( args ) . some ( key => args [ key ] . length > 0 ) ;
366
+ const configPath = path . join ( workdir , 'exoframe.json' ) ;
367
+ let defaultConfig = {
368
+ name : folderName ,
369
+ domain : '' ,
370
+ project : '' ,
371
+ restart : '' ,
372
+ env : undefined ,
373
+ labels : undefined ,
374
+ hostname : '' ,
375
+ template : '' ,
376
+ rateLimit : {
377
+ enabled : false ,
378
+ period : '1s' ,
379
+ average : 1 ,
380
+ burst : 5 ,
381
+ } ,
382
+ basicAuth : false ,
383
+ function : false ,
384
+ } ;
385
+ try {
386
+ fs . statSync ( configPath ) ;
387
+ console . log ( chalk . green ( 'Config already exists! Editing..' ) ) ;
388
+ defaultConfig = JSON . parse ( fs . readFileSync ( configPath ) . toString ( ) ) ;
389
+ } catch ( e ) {
390
+ // check if config didn't exist
391
+ if ( e . message . includes ( 'ENOENT' ) ) {
392
+ console . log ( 'Creating new config..' ) ;
393
+ } else {
394
+ // if there was any parsing error - show message and die
395
+ console . log ( chalk . red ( 'Error parsing existing config! Please make sure it is valid and try again.' ) ) ;
396
+ return ;
397
+ }
398
+ }
399
+
400
+ if ( func ) {
401
+ console . log ( 'Creating new config for function deployment..' ) ;
402
+ // set function flag to true
403
+ defaultConfig . function = true ;
404
+ // write config to file
405
+ writeConfig ( configPath , defaultConfig ) ;
406
+ return ;
407
+ }
408
+
409
+ let newConfig = defaultConfig ;
410
+
411
+ if ( nonInteractive ) {
412
+ console . log ( chalk . yellow ( 'Mode changed to' ) , 'non-interactive' ) ;
413
+ }
414
+
415
+ const overrideFromArgument = ( key , value ) => {
416
+ if ( ! value ) return ;
417
+ console . log ( 'Setting' , chalk . red ( key ) , 'to' , chalk . yellow ( value ) ) ;
418
+ newConfig [ key ] = value ;
419
+ } ;
420
+
421
+ overrideFromArgument ( 'domain' , args . domain ) ;
422
+ overrideFromArgument ( 'name' , args . name ) ;
423
+ overrideFromArgument ( 'project' , args . project ) ;
424
+ overrideFromArgument ( 'restart' , args . restart ) ;
425
+ overrideFromArgument ( 'hostname' , args . hostname ) ;
426
+
427
+ if ( ! nonInteractive ) {
428
+ const { prompts, askForUsers} = generatePrompts ( defaultConfig ) ;
429
+ // get values from user
430
+ newConfig = await inquirer . prompt ( prompts ) ;
431
+
432
+ // update users for auth if needed
433
+ if ( newConfig . basicAuth ) {
434
+ newConfig . users = await askForUsers ( ) ;
435
+ }
374
436
}
375
437
376
438
writeConfig ( configPath , { ...defaultConfig , ...newConfig } ) ;
0 commit comments