fixes
This commit is contained in:
		| @@ -1,3 +1,5 @@ | ||||
| /* eslint-disable no-process-exit */ | ||||
| /* eslint-disable no-console */ | ||||
| /* | ||||
|  * Copyright (C) Sapphirecode - All Rights Reserved | ||||
|  * This file is part of console-app which is released under MIT. | ||||
| @@ -38,24 +40,40 @@ interface OptionProcess extends Option { | ||||
|   value?: unknown; | ||||
| } | ||||
|  | ||||
| function get_string_type (type: OptionType): 'string'|'number'|'boolean' { | ||||
|   if ([ | ||||
|     'string', | ||||
|     'number', | ||||
|     'boolean' | ||||
|   ].includes (type)) | ||||
|     return type as ('string'|'number'|'boolean'); | ||||
|   if ([ | ||||
|     'file', | ||||
|     'folder', | ||||
|     'path' | ||||
|   ].includes (type)) | ||||
|     return 'string'; | ||||
|   throw new Error (`unknown option type ${type}`); | ||||
| } | ||||
|  | ||||
| export class InteractiveOptions extends Persistent { | ||||
|   protected options: Array<OptionProcess>; | ||||
|   protected quiet = false; | ||||
|  | ||||
|   public constructor (options: Array<Option>) { | ||||
|     super (); | ||||
|     this.options = options | ||||
|       .map ((v) => ({ filled: false, ...v } as OptionProcess)); | ||||
|     for (const option of this.options) { | ||||
|       /* | ||||
|        *      if ( | ||||
|        *      typeof option.default !== 'undefined' | ||||
|        *      //TODO stringtype    typeof option.default !== option.type | ||||
|        *      ) { | ||||
|        *      throw new Error ( | ||||
|        *        `default does not match option type on ${option.name}` | ||||
|        *      ); | ||||
|        *      } | ||||
|        */ | ||||
|       if ( | ||||
|         typeof option.default !== 'undefined' | ||||
|         && typeof option.default !== get_string_type (option.type) | ||||
|       ) { | ||||
|         throw new Error ( | ||||
|           `default does not match option type on ${option.name}` | ||||
|         ); | ||||
|       } | ||||
|  | ||||
|       this.properties[option.name] = option.type; | ||||
|     } | ||||
|   } | ||||
| @@ -145,18 +163,31 @@ export class InteractiveOptions extends Persistent { | ||||
|         default:  false, | ||||
|         type:     'boolean', | ||||
|         describe: 'do not ask for options interactively' | ||||
|       }, | ||||
|       help: { | ||||
|         alias:    'h', | ||||
|         default:  false, | ||||
|         type:     'boolean', | ||||
|         describe: '' | ||||
|       } | ||||
|     }; | ||||
|     for (const opt of this.options) { | ||||
|       yargs_config[opt.name] = { | ||||
|         alias:    opt.alias, | ||||
|         default:  opt.default, | ||||
|         type:     opt.type === 'boolean' ? 'boolean' : 'string', | ||||
|         type:     get_string_type (opt.type), | ||||
|         describe: opt.description | ||||
|       }; | ||||
|     } | ||||
|     const argv = yargs.options (yargs_config) | ||||
|       .parse (); | ||||
|     if (argv.help) { | ||||
|       yargs.options (yargs_config) | ||||
|         .showHelp (); | ||||
|       process.exit (0); | ||||
|     } | ||||
|     this.quiet = argv.quiet as boolean; | ||||
|  | ||||
|     await Promise.all (this.options.map ((opt) => { | ||||
|       if (typeof argv[opt.name] !== 'undefined') | ||||
|         return this.assign_arg (opt, argv[opt.name]); | ||||
| @@ -195,8 +226,23 @@ export class InteractiveOptions extends Persistent { | ||||
|   } | ||||
|  | ||||
|   private async get_interactive_options (): Promise<void> { | ||||
|     for (const opt of this.options) | ||||
|     if (this.quiet) { | ||||
|       const missing = this.options.filter ((o) => !o.filled && o.required) | ||||
|         .map ((o) => o.name); | ||||
|       if (missing.length > 0) { | ||||
|         console.error ('missing arguments:'); | ||||
|         console.error (missing.join (', ')); | ||||
|         process.exit (0); | ||||
|       } | ||||
|     } | ||||
|     let invalid = false; | ||||
|     for (const opt of this.options) { | ||||
|       // eslint-disable-next-line no-await-in-loop | ||||
|       await this.prompt (opt); | ||||
|       if (opt.filled === false) | ||||
|         invalid = true; | ||||
|     } | ||||
|     if (invalid) | ||||
|       await this.get_interactive_options (); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user