|
7 | 7 | * file that was distributed with this source code. |
8 | 8 | */ |
9 | 9 |
|
10 | | -import { sep, parse } from 'node:path' |
| 10 | +import { |
| 11 | + File, |
| 12 | + Json, |
| 13 | + Path, |
| 14 | + ObjectBuilder, |
| 15 | + Exec, |
| 16 | + Module, |
| 17 | + Is, |
| 18 | +} from '@athenna/common' |
11 | 19 | import { loadFile, writeFile } from 'magicast' |
12 | | -import { File, Json, Path, ObjectBuilder, Exec, Module } from '@athenna/common' |
| 20 | +import { sep, parse, extname } from 'node:path' |
13 | 21 | import { RecursiveConfigException } from '#src/Exceptions/RecursiveConfigException' |
14 | 22 | import { NotSupportedKeyException } from '#src/Exceptions/NotSupportedKeyException' |
| 23 | +import { NotValidArrayConfigException } from '#src/Exceptions/NotValidArrayConfigException' |
15 | 24 |
|
16 | 25 | export class Config { |
17 | 26 | /** |
@@ -111,6 +120,28 @@ export class Config { |
111 | 120 | return this |
112 | 121 | } |
113 | 122 |
|
| 123 | + /** |
| 124 | + * Push a value to a configuration key that is a valid array. |
| 125 | + * If configuration is not an array, an exception will be thrown. |
| 126 | + */ |
| 127 | + public static push(key: string, value: any | any[]): typeof Config { |
| 128 | + const config = this.configs.get(key, []) |
| 129 | + |
| 130 | + if (!Is.Array(config)) { |
| 131 | + throw new NotValidArrayConfigException(key) |
| 132 | + } |
| 133 | + |
| 134 | + if (Is.Array(value)) { |
| 135 | + config.push(...value) |
| 136 | + } else { |
| 137 | + config.push(value) |
| 138 | + } |
| 139 | + |
| 140 | + this.configs.set(key, config) |
| 141 | + |
| 142 | + return this |
| 143 | + } |
| 144 | + |
114 | 145 | /** |
115 | 146 | * Delete the configuration key. |
116 | 147 | */ |
@@ -168,6 +199,12 @@ export class Config { |
168 | 199 | path = Path.config(), |
169 | 200 | safe = false, |
170 | 201 | ): Promise<void> { |
| 202 | + if (extname(path)) { |
| 203 | + safe ? this.safeLoad(path) : this.load(path) |
| 204 | + |
| 205 | + return |
| 206 | + } |
| 207 | + |
171 | 208 | const files = await Module.getAllJSFilesFrom(path) |
172 | 209 |
|
173 | 210 | this.fatherConfigPath = path |
|
0 commit comments