fixing insufficient coverage of error_callback
This commit is contained in:
		| @@ -30,8 +30,7 @@ const reader = new InteractiveOptions([ | |||||||
|     description: 'the switch foo', // description in the help page (optional) |     description: 'the switch foo', // description in the help page (optional) | ||||||
|     message: 'should foo be true?', // message when asking interactively (optional) |     message: 'should foo be true?', // message when asking interactively (optional) | ||||||
|     preset: [], // preset choices for string and path types (optional) |     preset: [], // preset choices for string and path types (optional) | ||||||
|     error: 'wrong input', // message to display when the user gives invalid input |     error: 'wrong input' // message to display when the user gives invalid input | ||||||
|     error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read |  | ||||||
|   }, |   }, | ||||||
| ]); | ]); | ||||||
|  |  | ||||||
| @@ -78,7 +77,8 @@ const reader = new InteractiveOptions([], { | |||||||
|   env: true, |   env: true, | ||||||
|   interactive: true, |   interactive: true, | ||||||
|   configs: ['json files to search for options'], |   configs: ['json files to search for options'], | ||||||
|   exit_on_interrupt: true, |   exit_on_interrupt: true,  // exit when user cancels prompt | ||||||
|  |   error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read | ||||||
| }); | }); | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|   | |||||||
| @@ -15,6 +15,7 @@ import { ArgSource } from './Sources/ArgSource'; | |||||||
| import { ConfigSource } from './Sources/ConfigSource'; | import { ConfigSource } from './Sources/ConfigSource'; | ||||||
| import { InteractiveSource } from './Sources/InteractiveSource'; | import { InteractiveSource } from './Sources/InteractiveSource'; | ||||||
| import { Option, OptionProcess } from './Option'; | import { Option, OptionProcess } from './Option'; | ||||||
|  | import { ErrorCallback } from './Types/ErrorCallback'; | ||||||
|  |  | ||||||
| const types: Record<OptionType, TypeValidation> = { | const types: Record<OptionType, TypeValidation> = { | ||||||
|   string:  new TypeValidation ('string'), |   string:  new TypeValidation ('string'), | ||||||
| @@ -32,6 +33,7 @@ interface SourceConfig { | |||||||
|   interactive?: boolean; |   interactive?: boolean; | ||||||
|   configs?: string[]; |   configs?: string[]; | ||||||
|   exit_on_interrupt?: boolean; |   exit_on_interrupt?: boolean; | ||||||
|  |   error_callback?: ErrorCallback; | ||||||
| } | } | ||||||
|  |  | ||||||
| export class InteractiveOptions extends Persistent { | export class InteractiveOptions extends Persistent { | ||||||
| @@ -71,14 +73,22 @@ export class InteractiveOptions extends Persistent { | |||||||
|     if ( |     if ( | ||||||
|       typeof source_config.configs !== 'undefined' |       typeof source_config.configs !== 'undefined' | ||||||
|       && Array.isArray (source_config.configs) |       && Array.isArray (source_config.configs) | ||||||
|     ) |     ) { | ||||||
|       this.sources.push (new ConfigSource (source_config.configs)); |       this.sources.push (new ConfigSource ( | ||||||
|  |         source_config.error_callback, | ||||||
|  |         source_config.configs | ||||||
|  |       )); | ||||||
|  |     } | ||||||
|     if (source_config.env !== false) |     if (source_config.env !== false) | ||||||
|       this.sources.push (new EnvSource); |       this.sources.push (new EnvSource (source_config.error_callback)); | ||||||
|     if (source_config.args !== false) |     if (source_config.args !== false) | ||||||
|       this.sources.push (new ArgSource); |       this.sources.push (new ArgSource (source_config.error_callback)); | ||||||
|     if (source_config.interactive !== false) |     if (source_config.interactive !== false) { | ||||||
|       this.sources.push (new InteractiveSource (exit_on_interrupt)); |       this.sources.push (new InteractiveSource ( | ||||||
|  |         source_config.error_callback, | ||||||
|  |         exit_on_interrupt | ||||||
|  |       )); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public async parse (): Promise<Record<string, unknown>> { |   public async parse (): Promise<Record<string, unknown>> { | ||||||
|   | |||||||
| @@ -19,11 +19,6 @@ interface Option { | |||||||
|   message?: string; |   message?: string; | ||||||
|   preset?: unknown[]; |   preset?: unknown[]; | ||||||
|   error?: string; |   error?: string; | ||||||
|   error_callback?: ( |  | ||||||
|     option: string, |  | ||||||
|     value: unknown, |  | ||||||
|     e: Error |  | ||||||
|   ) => unknown; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| interface OptionProcess extends Option { | interface OptionProcess extends Option { | ||||||
|   | |||||||
| @@ -11,13 +11,14 @@ import fs from 'fs-extra'; | |||||||
| import { run_regex } from '@sapphirecode/utilities'; | import { run_regex } from '@sapphirecode/utilities'; | ||||||
| import hjson from 'hjson'; | import hjson from 'hjson'; | ||||||
| import { OptionProcess } from '../Option'; | import { OptionProcess } from '../Option'; | ||||||
|  | import { ErrorCallback } from '../Types/ErrorCallback'; | ||||||
| import { OptionSource } from './OptionSource'; | import { OptionSource } from './OptionSource'; | ||||||
|  |  | ||||||
| export class ConfigSource extends OptionSource { | export class ConfigSource extends OptionSource { | ||||||
|   private _config_files: string[]; |   private _config_files: string[]; | ||||||
|  |  | ||||||
|   public constructor (config_files: string[]) { |   public constructor (error_callback?:ErrorCallback, config_files: string[]) { | ||||||
|     super (); |     super (error_callback); | ||||||
|     this._config_files = config_files; |     this._config_files = config_files; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -53,7 +54,8 @@ export class ConfigSource extends OptionSource { | |||||||
|         for (const key of Object.keys (json)) |         for (const key of Object.keys (json)) | ||||||
|           data[key] = json[key]; |           data[key] = json[key]; | ||||||
|       } |       } | ||||||
|       catch { |       catch (e) { | ||||||
|  |         this.error_callback ('*', `config file: ${f}`, e); | ||||||
|         continue; |         continue; | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -14,8 +14,11 @@ import { OptionSource } from './OptionSource'; | |||||||
| export class InteractiveSource extends OptionSource { | export class InteractiveSource extends OptionSource { | ||||||
|   private _exit_on_interrupt: boolean; |   private _exit_on_interrupt: boolean; | ||||||
|  |  | ||||||
|   public constructor (exit_on_interrupt: boolean) { |   public constructor ( | ||||||
|     super (); |     error_callback?:ErrorCallback, | ||||||
|  |     exit_on_interrupt: boolean | ||||||
|  |   ) { | ||||||
|  |     super (error_callback); | ||||||
|     this._exit_on_interrupt = exit_on_interrupt; |     this._exit_on_interrupt = exit_on_interrupt; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -6,10 +6,17 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| import { OptionProcess } from '../Option'; | import { OptionProcess } from '../Option'; | ||||||
|  | import { ErrorCallback } from '../Types/ErrorCallback'; | ||||||
|  |  | ||||||
| export abstract class OptionSource { | export abstract class OptionSource { | ||||||
|   public abstract async parse(opt: OptionProcess[]): Promise<void>; |   public abstract async parse(opt: OptionProcess[]): Promise<void>; | ||||||
|  |  | ||||||
|  |   protected error_callback?: ErrorCallback; | ||||||
|  |  | ||||||
|  |   public constructor (error_callback?: ErrorCallback) { | ||||||
|  |     this.error_callback = error_callback; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   protected async assign_arg ( |   protected async assign_arg ( | ||||||
|     opt: OptionProcess, |     opt: OptionProcess, | ||||||
|     value: unknown |     value: unknown | ||||||
| @@ -19,8 +26,8 @@ export abstract class OptionSource { | |||||||
|       opt.filled = true; |       opt.filled = true; | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|       if (typeof opt.error_callback !== 'undefined') |       if (typeof this.error_callback !== 'undefined') | ||||||
|         opt.error_callback (opt.name, value, e); |         this.error_callback (opt.name, value, e); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								lib/Types/ErrorCallback.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								lib/Types/ErrorCallback.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | export type ErrorCallback = ( | ||||||
|  |   option: string, | ||||||
|  |   value: unknown, | ||||||
|  |   e: Error | ||||||
|  | ) => unknown; | ||||||
		Reference in New Issue
	
	Block a user