bajo 2.11.1 → 2.12.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 +55 -16
- package/class/helper/base.js +5 -4
- package/package.json +1 -1
- package/wiki/CHANGES.md +5 -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,53 @@ 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, pattern, ignoreError = true, defValue = {}, parserOpts = {}, globOpts = {} } = options
|
|
856
|
+
|
|
857
|
+
const output = async (obj) => {
|
|
858
|
+
const orig = parseObject(obj)
|
|
859
|
+
if (!baseNs || extend === false) return orig
|
|
860
|
+
const { suffix = '', keys = [] } = options
|
|
861
|
+
let bases = this.app.getAllNs()
|
|
862
|
+
if (isString(extend)) extend = extend.split(',').map(i => i.trim)
|
|
863
|
+
if (isArray(extend)) bases = [...extend, 'main']
|
|
864
|
+
bases = uniq(bases)
|
|
865
|
+
let ext = isArray(obj) ? [] : {}
|
|
866
|
+
const dir = this.app[ns].dir.pkg
|
|
867
|
+
let [names, _path] = file.split(':')
|
|
868
|
+
if (file.slice(0, names.length + 1) !== `${ns}:`) _path = file.slice(dir.length + 1)
|
|
869
|
+
if (_path.startsWith('extend/')) _path = _path.slice(7)
|
|
870
|
+
if (_path.startsWith(`${baseNs}/`)) _path = _path.slice(baseNs.length + 1)
|
|
871
|
+
_path = _path.slice(0, -(path.extname(_path).length)) + '.*'
|
|
872
|
+
const opts = omit(options, ['suffix', 'keys', 'extend'])
|
|
873
|
+
opts.parserOpts = opts.parserOpts ?? {}
|
|
874
|
+
opts.parserOpts.args = opts.parserOpts.args ?? []
|
|
875
|
+
const idx = findIndex(opts.parserOpts.args, item => {
|
|
876
|
+
return isPlainObject(item) && Object.keys(item)[0] === '_orig'
|
|
877
|
+
})
|
|
878
|
+
if (idx > -1) opts.parserOpts.args[idx] = { _orig: orig }
|
|
879
|
+
else opts.parserOpts.args.push({ _orig: orig })
|
|
880
|
+
|
|
881
|
+
for (const base of bases) {
|
|
882
|
+
if (!this.app[base]) continue
|
|
883
|
+
const fileExt = `${this.app[base].dir.pkg}/extend/${baseNs}/extend/${ns}${suffix}/${_path}`
|
|
884
|
+
const result = parseObject(await this.readConfig(fileExt, { ...opts, extend: false }))
|
|
885
|
+
if (isEmpty(result)) continue
|
|
886
|
+
if (isArray(result)) ext = [...result, ...ext]
|
|
887
|
+
else ext = defaultsDeep({}, result, ext)
|
|
888
|
+
}
|
|
889
|
+
return isArray(orig) ? [...orig, ...ext] : defaultsDeep({}, keys.length > 0 ? pick(ext, keys) : ext, orig)
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
parserOpts.readFromFile = true
|
|
854
893
|
if (!ns) ns = this.ns
|
|
855
894
|
file = resolvePath(this.getPluginFile(file))
|
|
856
895
|
let ext = path.extname(file)
|
|
@@ -858,22 +897,22 @@ class Bajo extends Plugin {
|
|
|
858
897
|
ext = ext.toLowerCase()
|
|
859
898
|
if (ext === '.js') {
|
|
860
899
|
const { readHandler } = find(this.app.configHandlers, { ext })
|
|
861
|
-
return
|
|
900
|
+
return await output(await readHandler.call(this.app[ns], file, parserOpts))
|
|
862
901
|
}
|
|
863
|
-
if (ext === '.json') return await this.fromJson(file,
|
|
902
|
+
if (ext === '.json') return await output(await this.fromJson(file, parserOpts))
|
|
864
903
|
if (!['', '.*'].includes(ext)) {
|
|
865
904
|
const item = find(this.app.configHandlers, { ext })
|
|
866
905
|
if (!item) {
|
|
867
906
|
if (!ignoreError) throw this.error('cantParse%s', file, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
868
|
-
return
|
|
907
|
+
return await output(defValue)
|
|
869
908
|
}
|
|
870
|
-
return
|
|
909
|
+
return await output(await item.readHandler.call(this.app[ns], file, parserOpts))
|
|
871
910
|
}
|
|
872
911
|
const item = pattern ?? `${fname}.{${map(map(this.app.configHandlers, 'ext'), k => k.slice(1)).join(',')}}`
|
|
873
|
-
const files = await fastGlob(item,
|
|
912
|
+
const files = await fastGlob(item, globOpts ?? {})
|
|
874
913
|
if (files.length === 0) {
|
|
875
914
|
if (!ignoreError) throw this.error('noConfigFileFound', { code: 'BAJO_CONFIG_FILE_NOT_FOUND' })
|
|
876
|
-
return
|
|
915
|
+
return await output(defValue)
|
|
877
916
|
}
|
|
878
917
|
let config = defValue
|
|
879
918
|
for (const f of files) {
|
|
@@ -883,10 +922,10 @@ class Bajo extends Plugin {
|
|
|
883
922
|
if (!ignoreError) throw this.error('cantParse%s', f, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
884
923
|
continue
|
|
885
924
|
}
|
|
886
|
-
config = await item.readHandler.call(this.app[ns], f,
|
|
925
|
+
config = await item.readHandler.call(this.app[ns], f, parserOpts)
|
|
887
926
|
if (!isEmpty(config)) break
|
|
888
927
|
}
|
|
889
|
-
return
|
|
928
|
+
return await output(config)
|
|
890
929
|
}
|
|
891
930
|
|
|
892
931
|
/**
|
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