config source
This commit is contained in:
		
							
								
								
									
										2
									
								
								Jenkinsfile
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								Jenkinsfile
									
									
									
									
										vendored
									
									
								
							| @@ -5,7 +5,7 @@ pipeline { | ||||
|       VERSION = VersionNumber([ | ||||
|           versionNumberString: | ||||
|               '${BUILDS_ALL_TIME}', | ||||
|           versionPrefix: '1.2.', | ||||
|           versionPrefix: '1.3.', | ||||
|           worstResultForIncrement: 'SUCCESS' | ||||
|       ]) | ||||
|   } | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| # @sapphirecode/console-app | ||||
|  | ||||
| version: 1.2.x | ||||
| version: 1.3.x | ||||
|  | ||||
| read parameters from env, console args or interactively | ||||
|  | ||||
| @@ -54,13 +54,14 @@ the console reader automatically adds the options --help (-h) and --quiet (-q) | ||||
|   parameters are specified | ||||
|  | ||||
| the reader can also be constructed with additional options that specify which | ||||
| sources should be used. It reads from all by default | ||||
| sources should be used. It reads from all, except config files by default | ||||
|  | ||||
| ```js | ||||
| const reader = new InteractiveOptions([], { | ||||
|   args: true, | ||||
|   env: true, | ||||
|   interactive: true, | ||||
|   configs: ['json files to search for options'] | ||||
| }); | ||||
| ``` | ||||
|  | ||||
|   | ||||
| @@ -12,6 +12,7 @@ import { OptionType } from './OptionType'; | ||||
| import { OptionSource } from './Sources/OptionSource'; | ||||
| import { EnvSource } from './Sources/EnvSource'; | ||||
| import { ArgSource } from './Sources/ArgSource'; | ||||
| import { ConfigSource } from './Sources/ConfigSource'; | ||||
| import { InteractiveSource } from './Sources/InteractiveSource'; | ||||
| import { Option, OptionProcess } from './Option'; | ||||
|  | ||||
| @@ -26,9 +27,10 @@ const types: Record<OptionType, TypeValidation> = { | ||||
| }; | ||||
|  | ||||
| interface SourceConfig { | ||||
|   env: boolean; | ||||
|   args: boolean; | ||||
|   interactive: boolean; | ||||
|   env?: boolean; | ||||
|   args?: boolean; | ||||
|   interactive?: boolean; | ||||
|   configs?: string[]; | ||||
| } | ||||
|  | ||||
| export class InteractiveOptions extends Persistent { | ||||
| @@ -38,7 +40,7 @@ export class InteractiveOptions extends Persistent { | ||||
|  | ||||
|   public constructor ( | ||||
|     options: Array<Option>, | ||||
|     source_config: SourceConfig = { args: true, env: true, interactive: true } | ||||
|     source_config: SourceConfig = {} | ||||
|   ) { | ||||
|     super (); | ||||
|     this.options = options | ||||
| @@ -60,11 +62,16 @@ export class InteractiveOptions extends Persistent { | ||||
|       this.properties[option.name] = option.type_validation.persistent_type; | ||||
|     } | ||||
|  | ||||
|     if (source_config.env) | ||||
|     if ( | ||||
|       typeof source_config.configs !== 'undefined' | ||||
|       && Array.isArray (source_config.configs) | ||||
|     ) | ||||
|       this.sources.push (new ConfigSource (source_config.configs)); | ||||
|     if (source_config.env !== false) | ||||
|       this.sources.push (new EnvSource); | ||||
|     if (source_config.args) | ||||
|     if (source_config.args !== false) | ||||
|       this.sources.push (new ArgSource); | ||||
|     if (source_config.interactive) | ||||
|     if (source_config.interactive !== false) | ||||
|       this.sources.push (new InteractiveSource); | ||||
|   } | ||||
|  | ||||
|   | ||||
							
								
								
									
										43
									
								
								lib/Sources/ConfigSource.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								lib/Sources/ConfigSource.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| /* | ||||
|  * Copyright (C) Sapphirecode - All Rights Reserved | ||||
|  * This file is part of console-app which is released under MIT. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| /* eslint-disable no-await-in-loop */ | ||||
| import fs from 'fs-extra'; | ||||
| import { OptionProcess } from '../Option'; | ||||
| import { OptionSource } from './OptionSource'; | ||||
|  | ||||
| export class ConfigSource extends OptionSource { | ||||
|   private _config_files: string[]; | ||||
|  | ||||
|   public constructor (config_files: string[]) { | ||||
|     super (); | ||||
|     this._config_files = config_files; | ||||
|   } | ||||
|  | ||||
|   public async parse (options: OptionProcess[]): Promise<void> { | ||||
|     const data: Record<string, unknown> = {}; | ||||
|     for (const f of this._config_files) { | ||||
|       if (await fs.pathExists (f)) { | ||||
|         try { | ||||
|           const json = JSON.parse (await fs.readFile (f, 'utf-8')); | ||||
|           for (const key of Object.keys (json)) | ||||
|             data[key] = json[key]; | ||||
|         } | ||||
|         catch { | ||||
|           continue; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     const keys = Object.keys (data); | ||||
|  | ||||
|     await Promise.all (options.map (async (opt) => { | ||||
|       if (keys.includes (opt.name)) | ||||
|         await this.assign_arg (opt, data[opt.name]); | ||||
|     })); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user