Skip to content

Commit 656b9ef

Browse files
dbtools-antcampoLeonSilva15
authored andcommitted
feat(generator): enable usage of flags for all the options except for passwords
Merge pull request add-options-to-cli-to-all-questions
1 parent 17177f7 commit 656b9ef

File tree

1 file changed

+141
-41
lines changed

1 file changed

+141
-41
lines changed

src/index.ts

Lines changed: 141 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const getWalletFileChoices = async () => {
178178
const execAsync = promisify( exec );
179179
export default class Generate extends Command {
180180
static args = {
181-
file: Args.string( { description: 'file to read' } ),
181+
name: Args.string( { name:'name', required: false, description: 'Name of the project' } ),
182182
};
183183

184184
static description = 'generate template';
@@ -187,19 +187,119 @@ export default class Generate extends Command {
187187
'<%= config.bin %> <%= command.id %>',
188188
];
189189

190-
static flags = {
191-
// flag with a value (-n, --name=VALUE)
192-
name: Flags.string( { char: 'n', description: 'name to print' } ),
193-
// // flag with no value (-f, --force)
194-
// force: Flags.boolean({char: 'f'}),
195-
// // flag with no value (-t, --template)
196-
template: Flags.string( { char: 't', description: 'Template name' } ),
190+
static flags = {
191+
'template': Flags.string({
192+
char: 't',
193+
description: 'Template to use',
194+
options: ['node-vanilla', 'node-react', 'node-vue', 'node-react-todo'],
195+
multiple: false
196+
}),
197+
198+
'connection-type': Flags.string({
199+
char: 'c',
200+
description: 'Connection type',
201+
options:['basic', 'walletPath'],
202+
multiple: false
203+
}),
204+
205+
'db-username': Flags.string({
206+
description: 'Database username',
207+
multiple: false
208+
}),
209+
210+
// basic connection flags
211+
'db-protocol': Flags.string({
212+
description: 'Database protocol',
213+
multiple: false,
214+
relationships:[{
215+
type:'none', flags:[
216+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
217+
]
218+
}
219+
]}),
220+
221+
'db-hostname': Flags.string( {
222+
description: 'Database host name',
223+
multiple: false,
224+
relationships:[{
225+
type:'none', flags:[
226+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
227+
]
228+
}]
229+
}),
230+
'db-port': Flags.integer({
231+
description: 'Database port number',
232+
max: 65535,
233+
min: 0,
234+
multiple: false,
235+
relationships:[{
236+
type:'none', flags:[
237+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
238+
]
239+
}
240+
]
241+
}),
242+
'db-service-type': Flags.string({
243+
description: 'Database service type. Only can be sid or service name',
244+
options: ['sid', 'serviceName'],
245+
multiple: false,
246+
relationships:[{
247+
type:'none', flags:[
248+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
249+
]
250+
}
251+
]
252+
}),
253+
'db-sid': Flags.string({
254+
description: 'Database service ID, only needed if service type was sid',
255+
multiple: false,
256+
relationships:[{
257+
type:'none', flags:[
258+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'},
259+
{name: 'db-service-type', when: async (flags) => flags['db-service-type'] === 'serviceName'}
260+
]
261+
}
262+
]
263+
}),
264+
'db-service-name': Flags.string({
265+
description: 'Database service name, only needed if service type was serviceName',
266+
multiple: false,
267+
relationships:[{
268+
type:'none', flags:[
269+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'},
270+
{name: 'db-service-type', when: async (flags) => flags['db-service-type'] === 'sid'}
271+
]
272+
}
273+
]
274+
}),
275+
'wallet-path': Flags.string({
276+
description: 'Cloud wallet path',
277+
dependsOn: ['connection-type'],
278+
multiple: false,
279+
relationships:[{
280+
type:'none', flags:[
281+
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'basic'}
282+
]
283+
}
284+
]
285+
})
197286
};
198287

199288
public async run (): Promise<void> {
200289
const { args, flags } = await this.parse( Generate );
201-
const name = flags.name ? flags.name : '';
202-
const template = flags.template ? flags.template : '';
290+
const name = args.name ?? '';
291+
const template = flags['template'] ?? '';
292+
const connectionType = flags['connection-type'] ?? '';
293+
const databaseProtocol = flags['db-protocol'] ?? '';
294+
const databaseHostName = flags['db-hostname'] ?? '';
295+
const databasePort = flags['db-port']?.toString() ?? '';
296+
const databaseServiceType = flags['db-service-type'] ?? '';
297+
const databaseSID = flags['db-sid'] ?? '';
298+
const databaseServiceName = flags['db-service-name'] ?? '';
299+
const databaseUsername = flags['db-username'] ?? '';
300+
301+
// TODO: Validate and use wallet path
302+
const walletPathDirectory = flags['wallet-path'] ? flags['wallet-path'] : '';
203303

204304
// Ask the user for the application name.
205305
const appName = name === '' ? await input(
@@ -247,7 +347,7 @@ export default class Generate extends Command {
247347
) : template;
248348

249349
// Ask the user for the database connection type (Either basic connection or a connection using a cloud wallet).
250-
const databaseConnectionType = await select(
350+
const databaseConnectionType = connectionType === '' ? await select(
251351
{
252352
message: 'Which database connection type would you like to choose?',
253353
choices: [
@@ -262,39 +362,39 @@ export default class Generate extends Command {
262362
],
263363
default: 'walletPath'
264364
}
265-
);
365+
) : connectionType;
266366

267367
// This represents the config object that will hold all the information that the user has inputted and selected.
268368
let configObject;
269369

270370
// If the user has chosen the basic connection type, then we ask for the protocol, hostname, port and service name / SID.
271371
if ( databaseConnectionType === 'basic' ) {
272372

273-
const protocol = await input(
373+
const protocol = databaseProtocol === '' ? await input(
274374
{
275375
message: 'What is your database protocol?',
276376
default: 'tcp'
277377
},
278-
);
378+
) : databaseProtocol;
279379

280-
const hostname = await input(
380+
const hostname = databaseHostName === '' ? await input(
281381
{
282382
message: 'What is your database hostname?',
283383
default: 'localhost'
284384
},
285-
);
385+
) : databaseHostName;
286386

287-
const port = await input(
387+
const port = databasePort === '' ? await input(
288388
{
289389
message: 'What is your database port?',
290390
validate ( input ) {
291391
return !isNaN( Number.parseInt( input ) ) && isFinite( Number.parseInt( input ) ) ? true : 'Port can only be numbers!';
292392
},
293393
default: '1521'
294394
},
295-
);
395+
) : databasePort;
296396

297-
const serviceType = await select(
397+
const serviceType = databaseServiceType === '' ? await select(
298398
{
299399
message: 'Which service type would you like to use?',
300400
choices: [
@@ -308,29 +408,29 @@ export default class Generate extends Command {
308408
}
309409
]
310410
}
311-
);
411+
) : databaseServiceType;
312412

313413
let serviceValue;
314414

315-
serviceValue = await (
316-
serviceType === 'sid'
317-
? input(
318-
{
319-
message: 'Please input your database SID: ',
320-
validate ( input ) {
321-
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
322-
}
415+
if (serviceType === 'sid') {
416+
serviceValue = databaseSID === '' ? await input(
417+
{
418+
message: 'Please enter your database SID: ',
419+
validate ( input ) {
420+
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
323421
}
324-
)
325-
: input(
326-
{
327-
message: 'Please input your database service name: ',
328-
validate ( input ) {
329-
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
330-
}
422+
}
423+
) : databaseSID;
424+
} else {
425+
serviceValue = databaseServiceName === '' ? await input(
426+
{
427+
message: 'Please enter your database service name: ',
428+
validate ( input ) {
429+
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
331430
}
332-
) );
333-
431+
}
432+
) : databaseServiceName;
433+
}
334434
// This will be config object for the basic connection type.
335435
configObject = {
336436
appName,
@@ -345,7 +445,7 @@ export default class Generate extends Command {
345445
walletPath = await input(
346446
{
347447
// TODO: Cannot use tab to autocomplete, cannot use ~ for $HOME
348-
message: 'Please input your Cloud Wallet Path:',
448+
message: 'Please enter your Cloud Wallet Path:',
349449
validate ( input ) {
350450
return checkIfDirectoryOrZip( input );
351451
}
@@ -359,7 +459,7 @@ export default class Generate extends Command {
359459
const walletPassword = await password(
360460
{
361461
mask: true,
362-
message: 'Please input your wallet password:',
462+
message: 'Please enter your wallet password:',
363463
validate ( input ) {
364464
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
365465
}
@@ -378,14 +478,14 @@ export default class Generate extends Command {
378478

379479
// Ask the user for the database connection username.
380480
Object.assign( configObject, {
381-
connectionUsername: await input(
481+
connectionUsername: databaseUsername === '' ? await input(
382482
{
383483
message: 'What\'s your database username?',
384484
validate ( input ) {
385485
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
386486
}
387487
},
388-
)
488+
) : databaseUsername
389489
} );
390490

391491
// Ask the user for the database connection password.

0 commit comments

Comments
 (0)