changelog, config inheritance
This commit is contained in:
		| @@ -6,7 +6,9 @@ | ||||
|  */ | ||||
|  | ||||
| /* eslint-disable no-await-in-loop */ | ||||
| import { dirname, join } from 'path'; | ||||
| import fs from 'fs-extra'; | ||||
| import { run_regex } from '@sapphirecode/utilities'; | ||||
| import { OptionProcess } from '../Option'; | ||||
| import { OptionSource } from './OptionSource'; | ||||
|  | ||||
| @@ -18,26 +20,48 @@ export class ConfigSource extends OptionSource { | ||||
|     this._config_files = config_files; | ||||
|   } | ||||
|  | ||||
|   private async read_json_file (file: string): | ||||
|    Promise<Record<string, unknown>> { | ||||
|     if (!await fs.pathExists (file)) | ||||
|       return {}; | ||||
|     const dir = dirname (file); | ||||
|     const contents = await fs.readFile (file, 'utf-8'); | ||||
|     const obj: Record<string, unknown> = {}; | ||||
|     const regex = /^#include (?<f>.+)/gmui; | ||||
|     const includes: string[] = []; | ||||
|     run_regex (regex, contents, (res: {groups:{f:string}}) => { | ||||
|       includes.push (join (dir, res.groups.f)); | ||||
|     }); | ||||
|     for (const inc of includes) { | ||||
|       const data = await this.read_json_file (inc); | ||||
|       for (const key of Object.keys (data)) | ||||
|         obj[key] = data[key]; | ||||
|     } | ||||
|     const config = JSON.parse (contents.replace (regex, '')); | ||||
|     for (const key of Object.keys (config)) | ||||
|       obj[key] = config[key]; | ||||
|  | ||||
|     return obj; | ||||
|   } | ||||
|  | ||||
|   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; | ||||
|         } | ||||
|       try { | ||||
|         const json = await this.read_json_file (f); | ||||
|         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) => { | ||||
|     for (const opt of options) { | ||||
|       if (keys.includes (opt.name)) | ||||
|         await this.assign_arg (opt, data[opt.name]); | ||||
|     })); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user