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 CHANGED
@@ -91,7 +91,7 @@ class Bajo extends Plugin {
91
91
  }
92
92
  }
93
93
 
94
- breakNsPathFromFile = ({ file, dir, baseNs, suffix = '', getType } = {}) => {
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 = baseNs
106
+ name = ns
107
107
  }
108
108
  _path = camelCase(_path)
109
109
  const names = map(name.split('.'), n => camelCase(n))
110
- const [ns, subNs] = names
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.opts={}] - Parser setting
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, { ns, pattern, ignoreError = true, defValue = {}, options = {} } = {}) => {
851
+ readConfig = async (file, options = {}) => {
852
852
  const { parseObject } = this.app.lib
853
- options.readFromFile = true
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 parseObject(await readHandler.call(this.app[ns], file, options))
900
+ return await output(await readHandler.call(this.app[ns], file, parserOpts))
862
901
  }
863
- if (ext === '.json') return await this.fromJson(file, options)
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 parseObject(defValue)
907
+ return await output(defValue)
869
908
  }
870
- return parseObject(await item.readHandler.call(this.app[ns], file, options))
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, options.glob ?? {})
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 parseObject(defValue)
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, options)
925
+ config = await item.readHandler.call(this.app[ns], f, parserOpts)
887
926
  if (!isEmpty(config)) break
888
927
  }
889
- return parseObject(config)
928
+ return await output(config)
890
929
  }
891
930
 
892
931
  /**
@@ -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, breakNsPathFromFile } = this.bajo
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 { ns: baseNs } = this
108
- const { ns, subNs, path } = breakNsPathFromFile({ file, dir, baseNs, suffix: '/hook/' })
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: baseNs })
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "2.11.1",
3
+ "version": "2.12.0",
4
4
  "description": "The ultimate framework for whipping up massive apps in no time",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-04-23
4
+
5
+ - [2.12.0] Remove ```breakNsPathFromFile()```
6
+ - [2.12.0] Add feature to read in extended path ```readConfig()```
7
+
3
8
  ## 2026-04-11
4
9
 
5
10
  - [2.11.1] Bug fix in ```join()```