bajo 2.11.1 → 2.12.1
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 +67 -18
- package/class/helper/base.js +5 -4
- package/package.json +1 -1
- package/wiki/CHANGES.md +9 -0
package/class/bajo.js
CHANGED
|
@@ -91,7 +91,7 @@ class Bajo extends Plugin {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
breakNsPathFromFile = ({ file, dir,
|
|
94
|
+
breakNsPathFromFile = ({ file = '', dir = '', ns, suffix = '', getType } = {}) => {
|
|
95
95
|
let item = file.replace(dir + suffix, '')
|
|
96
96
|
let type
|
|
97
97
|
if (getType) {
|
|
@@ -103,12 +103,12 @@ class Bajo extends Plugin {
|
|
|
103
103
|
let [name, _path] = item.split('@')
|
|
104
104
|
if (!_path) {
|
|
105
105
|
_path = name
|
|
106
|
-
name =
|
|
106
|
+
name = ns
|
|
107
107
|
}
|
|
108
108
|
_path = camelCase(_path)
|
|
109
109
|
const names = map(name.split('.'), n => camelCase(n))
|
|
110
|
-
const [
|
|
111
|
-
return { ns, subNs, path: _path, fullNs: names.join('.'), type }
|
|
110
|
+
const [_ns, subNs] = names
|
|
111
|
+
return { ns: _ns, subNs, path: _path, fullNs: names.join('.'), type }
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
/**
|
|
@@ -843,14 +843,63 @@ class Bajo extends Plugin {
|
|
|
843
843
|
* @param {boolean} [options.ignoreError] - Any exception will be silently discarded
|
|
844
844
|
* @param {string} [options.ns] - If given, use this as the scope
|
|
845
845
|
* @param {string} [options.pattern] - If given and auto detection is on (extension is ```.*```), it will be used for instead the default one
|
|
846
|
-
* @param {Object} [options.globOptions={}] - {@link https://github.com/mrmlnc/fast-glob|fast-glob} options
|
|
847
846
|
* @param {Object} [options.defValue={}] - Default value to use if value returned empty
|
|
848
|
-
* @param {Object} [options.
|
|
847
|
+
* @param {Object} [options.parserOpts={}] - Parser setting
|
|
848
|
+
* @param {Object} [options.globOpts={}] - {@link https://github.com/mrmlnc/fast-glob|fast-glob} options
|
|
849
849
|
* @returns {Object}
|
|
850
850
|
*/
|
|
851
|
-
readConfig = async (file,
|
|
851
|
+
readConfig = async (file, options = {}) => {
|
|
852
852
|
const { parseObject } = this.app.lib
|
|
853
|
-
|
|
853
|
+
const { defaultsDeep } = this.app.lib.aneka
|
|
854
|
+
const { uniq, isString, isArray, findIndex, isPlainObject } = this.app.lib._
|
|
855
|
+
let { ns, baseNs, extend, checkOverride, pattern, ignoreError = true, defValue = {}, parserOpts = {}, globOpts = {} } = options
|
|
856
|
+
|
|
857
|
+
const getParseOptsArgs = (opts, orig) => {
|
|
858
|
+
opts.parserOpts = opts.parserOpts ?? {}
|
|
859
|
+
opts.parserOpts.args = opts.parserOpts.args ?? []
|
|
860
|
+
const idx = findIndex(opts.parserOpts.args, item => {
|
|
861
|
+
return isPlainObject(item) && Object.keys(item)[0] === '_orig'
|
|
862
|
+
})
|
|
863
|
+
if (idx > -1) opts.parserOpts.args[idx] = { _orig: orig }
|
|
864
|
+
else opts.parserOpts.args.push({ _orig: orig })
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
const output = async (obj) => {
|
|
868
|
+
let orig = parseObject(obj)
|
|
869
|
+
if (!baseNs || extend === false) return orig
|
|
870
|
+
const { suffix = '', keys = [] } = options
|
|
871
|
+
let bases = this.app.getAllNs()
|
|
872
|
+
if (isString(extend)) extend = extend.split(',').map(i => i.trim)
|
|
873
|
+
if (isArray(extend)) bases = [...extend, 'main']
|
|
874
|
+
bases = uniq(bases)
|
|
875
|
+
let ext = isArray(orig) ? [] : {}
|
|
876
|
+
const dir = this.app[ns].dir.pkg
|
|
877
|
+
let [names, _path] = file.split(':')
|
|
878
|
+
if (file.slice(0, names.length + 1) !== `${ns}:`) _path = file.slice(dir.length + 1)
|
|
879
|
+
if (_path.startsWith('extend/')) _path = _path.slice(7)
|
|
880
|
+
if (_path.startsWith(`${baseNs}/`)) _path = _path.slice(baseNs.length + 1)
|
|
881
|
+
_path = _path.slice(0, -(path.extname(_path).length)) + '.*'
|
|
882
|
+
// check for override? Override only exists in main plugin
|
|
883
|
+
const opts = omit(options, ['suffix', 'keys', 'extend'])
|
|
884
|
+
if (checkOverride) {
|
|
885
|
+
getParseOptsArgs(opts, orig)
|
|
886
|
+
const fileExt = `${this.app.main.dir.pkg}/extend/${baseNs}/override/${ns}${suffix}/${_path}`
|
|
887
|
+
const result = parseObject(await this.readConfig(fileExt, { ...opts, extend: false, checkOverride: false }))
|
|
888
|
+
if (!isEmpty(result)) orig = result
|
|
889
|
+
}
|
|
890
|
+
getParseOptsArgs(opts, orig)
|
|
891
|
+
for (const base of bases) {
|
|
892
|
+
if (!this.app[base]) continue
|
|
893
|
+
const fileExt = `${this.app[base].dir.pkg}/extend/${baseNs}/extend/${ns}${suffix}/${_path}`
|
|
894
|
+
const result = parseObject(await this.readConfig(fileExt, { ...opts, extend: false }))
|
|
895
|
+
if (isEmpty(result)) continue
|
|
896
|
+
if (isArray(result)) ext = [...result, ...ext]
|
|
897
|
+
else ext = defaultsDeep({}, result, ext)
|
|
898
|
+
}
|
|
899
|
+
return isArray(orig) ? [...orig, ...ext] : defaultsDeep({}, keys.length > 0 ? pick(ext, keys) : ext, orig)
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
parserOpts.readFromFile = true
|
|
854
903
|
if (!ns) ns = this.ns
|
|
855
904
|
file = resolvePath(this.getPluginFile(file))
|
|
856
905
|
let ext = path.extname(file)
|
|
@@ -858,22 +907,22 @@ class Bajo extends Plugin {
|
|
|
858
907
|
ext = ext.toLowerCase()
|
|
859
908
|
if (ext === '.js') {
|
|
860
909
|
const { readHandler } = find(this.app.configHandlers, { ext })
|
|
861
|
-
return
|
|
910
|
+
return await output(await readHandler.call(this.app[ns], file, parserOpts))
|
|
862
911
|
}
|
|
863
|
-
if (ext === '.json') return await this.fromJson(file,
|
|
912
|
+
if (ext === '.json') return await output(await this.fromJson(file, parserOpts))
|
|
864
913
|
if (!['', '.*'].includes(ext)) {
|
|
865
914
|
const item = find(this.app.configHandlers, { ext })
|
|
866
915
|
if (!item) {
|
|
867
916
|
if (!ignoreError) throw this.error('cantParse%s', file, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
868
|
-
return
|
|
917
|
+
return await output(defValue)
|
|
869
918
|
}
|
|
870
|
-
return
|
|
919
|
+
return await output(await item.readHandler.call(this.app[ns], file, parserOpts))
|
|
871
920
|
}
|
|
872
921
|
const item = pattern ?? `${fname}.{${map(map(this.app.configHandlers, 'ext'), k => k.slice(1)).join(',')}}`
|
|
873
|
-
const files = await fastGlob(item,
|
|
922
|
+
const files = await fastGlob(item, globOpts ?? {})
|
|
874
923
|
if (files.length === 0) {
|
|
875
924
|
if (!ignoreError) throw this.error('noConfigFileFound', { code: 'BAJO_CONFIG_FILE_NOT_FOUND' })
|
|
876
|
-
return
|
|
925
|
+
return await output(defValue)
|
|
877
926
|
}
|
|
878
927
|
let config = defValue
|
|
879
928
|
for (const f of files) {
|
|
@@ -883,10 +932,10 @@ class Bajo extends Plugin {
|
|
|
883
932
|
if (!ignoreError) throw this.error('cantParse%s', f, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
884
933
|
continue
|
|
885
934
|
}
|
|
886
|
-
config = await item.readHandler.call(this.app[ns], f,
|
|
935
|
+
config = await item.readHandler.call(this.app[ns], f, parserOpts)
|
|
887
936
|
if (!isEmpty(config)) break
|
|
888
937
|
}
|
|
889
|
-
return
|
|
938
|
+
return await output(config)
|
|
890
939
|
}
|
|
891
940
|
|
|
892
941
|
/**
|
|
@@ -944,13 +993,13 @@ class Bajo extends Plugin {
|
|
|
944
993
|
let ext = {}
|
|
945
994
|
// default config file
|
|
946
995
|
try {
|
|
947
|
-
cfg = await this.readConfig(`${path}
|
|
996
|
+
cfg = await this.readConfig(`${path}.*`)
|
|
948
997
|
} catch (err) {
|
|
949
998
|
if (['BAJO_CONFIG_NO_PARSER'].includes(err.code)) throw err
|
|
950
999
|
}
|
|
951
1000
|
// env based config file
|
|
952
1001
|
try {
|
|
953
|
-
ext = await this.readConfig(`${path}-${this.config.env}
|
|
1002
|
+
ext = await this.readConfig(`${path}-${this.config.env}.*`)
|
|
954
1003
|
} catch (err) {
|
|
955
1004
|
if (!['BAJO_CONFIG_FILE_NOT_FOUND'].includes(err.code)) throw err
|
|
956
1005
|
}
|
package/class/helper/base.js
CHANGED
|
@@ -99,16 +99,17 @@ export async function checkDependencies () {
|
|
|
99
99
|
* @fires bajo:afterCollectHooks
|
|
100
100
|
*/
|
|
101
101
|
export async function collectHooks () {
|
|
102
|
-
const { eachPlugins, runHook, isLogInRange, importModule
|
|
102
|
+
const { eachPlugins, runHook, isLogInRange, importModule } = this.bajo
|
|
103
103
|
const me = this
|
|
104
104
|
me.bajo.log.trace('collecting%s', this.t('hooks'))
|
|
105
105
|
// collects
|
|
106
106
|
await eachPlugins(async function ({ dir, file }) {
|
|
107
|
-
const
|
|
108
|
-
const
|
|
107
|
+
const _file = file.replace(dir + '/hook/', '').replace('.js', '')
|
|
108
|
+
const [names, path] = _file.split('@')
|
|
109
|
+
const [ns, subNs] = names.split('.').map(n => camelCase(n))
|
|
109
110
|
const mod = await importModule(file, { asHandler: true })
|
|
110
111
|
if (!mod) return undefined
|
|
111
|
-
merge(mod, { ns, subNs, path, src:
|
|
112
|
+
merge(mod, { ns, subNs, path: camelCase(path), src: this.ns })
|
|
112
113
|
me.bajo.hooks.push(mod)
|
|
113
114
|
}, { glob: 'hook/**/*.js', prefix: me.bajo.ns })
|
|
114
115
|
// for log trace purpose only
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-04-25
|
|
4
|
+
|
|
5
|
+
- [2.12.1] Bug fix in ```readConfig()```
|
|
6
|
+
|
|
7
|
+
## 2026-04-23
|
|
8
|
+
|
|
9
|
+
- [2.12.0] Remove ```breakNsPathFromFile()```
|
|
10
|
+
- [2.12.0] Add feature to read in extended path ```readConfig()```
|
|
11
|
+
|
|
3
12
|
## 2026-04-11
|
|
4
13
|
|
|
5
14
|
- [2.11.1] Bug fix in ```join()```
|