bajo 2.8.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/class/bajo.js +23 -21
- package/class/helper/base.js +1 -1
- package/class/plugin/print.js +1 -1
- package/class/plugin.js +10 -0
- package/package.json +1 -1
- package/wiki/CHANGES.md +11 -0
package/class/bajo.js
CHANGED
|
@@ -57,19 +57,13 @@ class Bajo extends Plugin {
|
|
|
57
57
|
this.config = {}
|
|
58
58
|
|
|
59
59
|
app.configHandlers = [
|
|
60
|
-
{ ext: '.js', readHandler: this.
|
|
60
|
+
{ ext: '.js', readHandler: this.fromJs },
|
|
61
61
|
{ ext: '.json', readHandler: this.fromJson, writeHandler: this.toJson }
|
|
62
62
|
]
|
|
63
63
|
|
|
64
64
|
this.hooks = []
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
async _defConfigHandler (file, opts = {}) {
|
|
68
|
-
let mod = await importModule(file)
|
|
69
|
-
if (isFunction(mod)) mod = await mod.call(this, opts)
|
|
70
|
-
return mod
|
|
71
|
-
}
|
|
72
|
-
|
|
73
67
|
/**
|
|
74
68
|
* Initialization:
|
|
75
69
|
*
|
|
@@ -225,6 +219,7 @@ class Bajo extends Plugin {
|
|
|
225
219
|
* @returns {Object[]} The collection
|
|
226
220
|
*/
|
|
227
221
|
buildCollections = async (options = {}) => {
|
|
222
|
+
const { parseObject } = this.app.lib
|
|
228
223
|
let { ns, handler, dupChecks = [], container, useDefaultName = true, noDefault = true } = options
|
|
229
224
|
if (!ns) ns = this.ns
|
|
230
225
|
const cfg = this.app[ns].getConfig()
|
|
@@ -244,7 +239,7 @@ class Bajo extends Plugin {
|
|
|
244
239
|
await this.runHook(`${ns}:beforeBuildCollection`, container)
|
|
245
240
|
const deleted = []
|
|
246
241
|
for (const index in items) {
|
|
247
|
-
const item = items[index]
|
|
242
|
+
const item = parseObject(items[index])
|
|
248
243
|
if (useDefaultName) {
|
|
249
244
|
if (!has(item, 'name')) {
|
|
250
245
|
if (find(items, { name: 'default' })) throw this.app[ns].error('collExists%s', 'default')
|
|
@@ -851,9 +846,9 @@ class Bajo extends Plugin {
|
|
|
851
846
|
* @param {Object} [options.opts={}] - Parser setting
|
|
852
847
|
* @returns {Object}
|
|
853
848
|
*/
|
|
854
|
-
readConfig = async (file, { ns, pattern,
|
|
849
|
+
readConfig = async (file, { ns, pattern, ignoreError = true, defValue = {}, options = {} } = {}) => {
|
|
855
850
|
const { parseObject } = this.app.lib
|
|
856
|
-
|
|
851
|
+
options.readFromFile = true
|
|
857
852
|
if (!ns) ns = this.ns
|
|
858
853
|
file = resolvePath(this.getPluginFile(file))
|
|
859
854
|
let ext = path.extname(file)
|
|
@@ -861,19 +856,19 @@ class Bajo extends Plugin {
|
|
|
861
856
|
ext = ext.toLowerCase()
|
|
862
857
|
if (ext === '.js') {
|
|
863
858
|
const { readHandler } = find(this.app.configHandlers, { ext })
|
|
864
|
-
return parseObject(await readHandler.call(this.app[ns], file,
|
|
859
|
+
return parseObject(await readHandler.call(this.app[ns], file, options))
|
|
865
860
|
}
|
|
866
|
-
if (ext === '.json') return await this.fromJson(file,
|
|
861
|
+
if (ext === '.json') return await this.fromJson(file, options)
|
|
867
862
|
if (!['', '.*'].includes(ext)) {
|
|
868
863
|
const item = find(this.app.configHandlers, { ext })
|
|
869
864
|
if (!item) {
|
|
870
865
|
if (!ignoreError) throw this.error('cantParse%s', file, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
871
866
|
return parseObject(defValue)
|
|
872
867
|
}
|
|
873
|
-
return parseObject(await item.readHandler.call(this.app[ns], file,
|
|
868
|
+
return parseObject(await item.readHandler.call(this.app[ns], file, options))
|
|
874
869
|
}
|
|
875
870
|
const item = pattern ?? `${fname}.{${map(map(this.app.configHandlers, 'ext'), k => k.slice(1)).join(',')}}`
|
|
876
|
-
const files = await fastGlob(item,
|
|
871
|
+
const files = await fastGlob(item, options.glob ?? {})
|
|
877
872
|
if (files.length === 0) {
|
|
878
873
|
if (!ignoreError) throw this.error('noConfigFileFound', { code: 'BAJO_CONFIG_FILE_NOT_FOUND' })
|
|
879
874
|
return parseObject(defValue)
|
|
@@ -886,7 +881,7 @@ class Bajo extends Plugin {
|
|
|
886
881
|
if (!ignoreError) throw this.error('cantParse%s', f, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
887
882
|
continue
|
|
888
883
|
}
|
|
889
|
-
config = await item.readHandler.call(this.app[ns], f,
|
|
884
|
+
config = await item.readHandler.call(this.app[ns], f, options)
|
|
890
885
|
if (!isEmpty(config)) break
|
|
891
886
|
}
|
|
892
887
|
return parseObject(config)
|
|
@@ -912,15 +907,22 @@ class Bajo extends Plugin {
|
|
|
912
907
|
return parseObject(JSON.parse(resp))
|
|
913
908
|
}
|
|
914
909
|
|
|
915
|
-
|
|
916
|
-
const
|
|
910
|
+
fromJs = async (file, options = {}) => {
|
|
911
|
+
const args = options.args ?? []
|
|
912
|
+
let mod = await importModule(file)
|
|
913
|
+
if (isFunction(mod)) mod = await mod.call(this, ...args)
|
|
914
|
+
return mod
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
fromJson (data, options = {}) {
|
|
918
|
+
const content = options.readFromFile ? fs.readFileSync(data, 'utf8') : data
|
|
917
919
|
return JSON.parse(content)
|
|
918
920
|
}
|
|
919
921
|
|
|
920
|
-
toJson = (data,
|
|
921
|
-
const content = JSON.stringify(data, null, omit(
|
|
922
|
-
if (
|
|
923
|
-
fs.writeFileSync(
|
|
922
|
+
toJson = (data, options = {}) => {
|
|
923
|
+
const content = JSON.stringify(data, null, omit(options, ['writeToFile']))
|
|
924
|
+
if (options.writeToFile) {
|
|
925
|
+
fs.writeFileSync(options.saveAsFile, content, 'utf8')
|
|
924
926
|
return
|
|
925
927
|
}
|
|
926
928
|
return content
|
package/class/helper/base.js
CHANGED
|
@@ -162,7 +162,6 @@ export async function run () {
|
|
|
162
162
|
await runHook(`bajo:${camelCase(`before all ${method}`)}`)
|
|
163
163
|
await eachPlugins(async function () {
|
|
164
164
|
const { ns } = this
|
|
165
|
-
if (method === 'start') freeze(me[ns].config)
|
|
166
165
|
/**
|
|
167
166
|
* Run before ```{method}``` is executed within ```{ns}``` context
|
|
168
167
|
*
|
|
@@ -186,6 +185,7 @@ export async function run () {
|
|
|
186
185
|
* @see module:Helper/Base.run
|
|
187
186
|
*/
|
|
188
187
|
await runHook(`${ns}:${camelCase(`after ${method}`)}`)
|
|
188
|
+
if (method === 'start') freeze(me[ns].config)
|
|
189
189
|
})
|
|
190
190
|
/**
|
|
191
191
|
* Run after all ```{method}``` executed. Accepted ```{method}```: ```Init``` or ```Start```
|
package/class/plugin/print.js
CHANGED
|
@@ -229,12 +229,12 @@ class Print extends Tools {
|
|
|
229
229
|
*/
|
|
230
230
|
fatal = (text, ...args) => {
|
|
231
231
|
if (text instanceof Error) {
|
|
232
|
+
console.error(text)
|
|
232
233
|
text = text.message
|
|
233
234
|
args = []
|
|
234
235
|
}
|
|
235
236
|
this.setText(text, ...args)
|
|
236
237
|
this.ora.fail()
|
|
237
|
-
if (text instanceof Error && this.app.bajo.config.log.level === 'trace') console.error(text)
|
|
238
238
|
this.app.exit()
|
|
239
239
|
}
|
|
240
240
|
|
package/class/plugin.js
CHANGED
|
@@ -176,6 +176,16 @@ class Plugin {
|
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Alias to ```this.app.dump()```
|
|
181
|
+
*
|
|
182
|
+
* @param {...any} args
|
|
183
|
+
* @returns
|
|
184
|
+
*/
|
|
185
|
+
dump = (...args) => {
|
|
186
|
+
this.app.dump(...args)
|
|
187
|
+
}
|
|
188
|
+
|
|
179
189
|
/**
|
|
180
190
|
* Dispose internal references
|
|
181
191
|
*/
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-03-30
|
|
4
|
+
|
|
5
|
+
- [2.10.0] Add ability to pass options of ```configHandlers``` with type ```.js```
|
|
6
|
+
- [2.10.0] Freezing config object now occurs ```{ns}:afterStart()```
|
|
7
|
+
- [2.10.0] Bug fix on ```print.fatal()``` when argument is an Error object
|
|
8
|
+
|
|
9
|
+
## 2026-03-25
|
|
10
|
+
|
|
11
|
+
- [2.9.0] Add ```dump()``` now available through out plugins
|
|
12
|
+
- [2.9.0] Bug fix in ```buildCollections()```, now items to be collected are parsed with ```aneka.parseObject()```
|
|
13
|
+
|
|
3
14
|
## 2026-03-22
|
|
4
15
|
|
|
5
16
|
- [2.8.0] Add ```options.readFromFile``` in ```readConfig()```
|