bajo 0.2.16 → 0.2.18

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.
@@ -1,8 +1,9 @@
1
- import { filter, isArray, each, pullAt, camelCase } from 'lodash-es'
1
+ import { filter, isArray, each, pullAt, camelCase, has, find, set } from 'lodash-es'
2
2
 
3
3
  async function buildCollections (options = {}) {
4
- const { getConfig, getPluginName, fatal, runHook } = this.bajo.helper
5
- let { plugin, handler, dupChecks = [], container = 'connections' } = options
4
+ const { getConfig, getPluginName, fatal, runHook, error } = this.bajo.helper
5
+ let { plugin, handler, dupChecks = [], container = 'connections', useDefaultName } = options
6
+ useDefaultName = useDefaultName ?? true
6
7
  if (!plugin) plugin = getPluginName(4)
7
8
  const cfg = getConfig(plugin, { full: true })
8
9
  if (!cfg[container]) return []
@@ -12,6 +13,12 @@ async function buildCollections (options = {}) {
12
13
  const deleted = []
13
14
  for (const index in cfg[container]) {
14
15
  const item = cfg[container][index]
16
+ if (useDefaultName) {
17
+ if (!has(item, 'name')) {
18
+ if (find(cfg[container], { name: 'default' })) throw error('Connection \'default\' already exists')
19
+ else item.name = 'default'
20
+ }
21
+ }
15
22
  const result = await handler.call(this, { item, index, cfg })
16
23
  if (result) cfg[container][index] = result
17
24
  else if (result === false) deleted.push(index)
@@ -20,12 +27,11 @@ async function buildCollections (options = {}) {
20
27
 
21
28
  // check for duplicity
22
29
  each(cfg[container], c => {
23
- const checker = {}
24
30
  each(dupChecks, d => {
25
- checker[d] = c[d]
31
+ const checker = set({}, d, c[d])
32
+ const match = filter(cfg[container], checker)
33
+ if (match.length > 1) fatal('One or more %s shared the same \'%s\'', container, dupChecks.join(', '))
26
34
  })
27
- const match = filter(cfg[container], checker)
28
- if (match.length > 1) fatal('One or more %s shared the same \'%s\'', container, dupChecks.join(', '), { code: 'BAJOMQTT_CONNECTION_NOT_UNIQUE' })
29
35
  })
30
36
  await runHook(`${plugin}:${camelCase(`after build ${container}`)}`)
31
37
  return cfg[container]
@@ -27,6 +27,7 @@ async function _eachPlugins (handler, { key = 'name', glob, ns, useBajo } = {})
27
27
  }
28
28
  const files = await fastGlob(pattern, opts)
29
29
  for (const f of files) {
30
+ if (path.basename(f)[0] === '_') continue
30
31
  const resp = await handler.call(this, { plugin, pkg, cfg, alias, file: f, dir: base, dependencies })
31
32
  if (resp === false) break
32
33
  else if (resp === undefined) continue
@@ -42,7 +42,8 @@ function error (msg = 'Internal server error', ...args) {
42
42
  }
43
43
  if (!ns) ns = getPluginName.call(this, 3)
44
44
  const orgMsg = msg
45
- const message = print.__(msg, ...args, { ns })
45
+ args.push({ ns })
46
+ const message = print.__(msg, ...args)
46
47
  let err
47
48
  if (isPlainObject(payload) && payload.class) err = payload.class(message)
48
49
  else err = Error(message)
@@ -1,10 +1,11 @@
1
- import error from './error.js'
2
1
  import getPluginName from './get-plugin-name.js'
3
2
 
4
3
  function fatal (...args) {
4
+ const { error } = this.bajo.helper
5
5
  const ns = getPluginName.call(this, 3)
6
6
  args.push({ ns })
7
- const err = error(...args)
7
+ const [msg, ...params] = args
8
+ const err = error(msg, ...params)
8
9
  console.error(err)
9
10
  process.exit(1)
10
11
  }
@@ -3,15 +3,17 @@ import { isFunction, isPlainObject } from 'lodash-es'
3
3
  import error from './error.js'
4
4
  import fs from 'fs-extra'
5
5
 
6
- async function load (file, asDefaultImport = true) {
7
- const imported = await import(resolvePath(file, true))
6
+ async function load (file, asDefaultImport = true, noCache = false) {
7
+ file = resolvePath(file, true)
8
+ if (noCache) file += `?_=${Date.now()}`
9
+ const imported = await import(file)
8
10
  if (asDefaultImport) return imported.default
9
11
  return imported
10
12
  }
11
13
 
12
- async function importModule (file, { asDefaultImport, asHandler } = {}) {
14
+ async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
13
15
  if (!fs.existsSync(file)) return
14
- let mod = await load(file, asDefaultImport)
16
+ let mod = await load(file, asDefaultImport, noCache)
15
17
  if (!asHandler) return mod
16
18
  if (isFunction(mod)) mod = { level: 999, handler: mod }
17
19
  if (!isPlainObject(mod)) throw error.call(this, 'File \'%s\' is NOT a handler module', file)
@@ -29,7 +29,7 @@ import fs from 'fs-extra'
29
29
 
30
30
  async function importPkg (...pkg) {
31
31
  const result = {}
32
- let opts = { returnDefault: true, thrownNotFound: false }
32
+ let opts = { returnDefault: true, thrownNotFound: false, noCache: false }
33
33
  if (isPlainObject(last(pkg))) {
34
34
  opts = defaultsDeep(pkg.pop(), opts)
35
35
  }
@@ -52,6 +52,7 @@ async function importPkg (...pkg) {
52
52
  if (fs.existsSync(`${mainFileOrg}/index.js`)) mainFile += '/index.js'
53
53
  else mainFile += '.js'
54
54
  }
55
+ if (opts.noCache) mainFile += `?_=${Date.now()}`
55
56
  let mod = await import(mainFile)
56
57
  if (opts.returnDefault && has(mod, 'default')) {
57
58
  mod = mod.default
@@ -1,4 +1,5 @@
1
- import { isPlainObject, isArray, isNumber } from 'lodash-es'
1
+ import { isPlainObject, isArray, isNumber, set } from 'lodash-es'
2
+ import dotenvParseVariables from 'dotenv-parse-variables'
2
3
  import ms from 'ms'
3
4
  import dayjs from '../lib/dayjs.js'
4
5
  import isSet from './is-set.js'
@@ -13,7 +14,7 @@ function parseDt (val) {
13
14
  return dt.toDate()
14
15
  }
15
16
 
16
- function parseObject (obj, silent = true) {
17
+ function parseObject (obj, silent = true, parseValue = false) {
17
18
  const keys = Object.keys(obj)
18
19
  keys.forEach(k => {
19
20
  const v = obj[k]
@@ -21,9 +22,11 @@ function parseObject (obj, silent = true) {
21
22
  else if (isArray(v)) {
22
23
  v.forEach((i, idx) => {
23
24
  if (isPlainObject(i)) obj[k][idx] = parseObject(i)
25
+ else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
24
26
  })
25
27
  } else if (isSet(v)) {
26
28
  try {
29
+ if (parseValue) obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
27
30
  if (k.slice(-3) === 'Dur') obj[k] = parseDur(v)
28
31
  if (k.slice(-2) === 'Dt') obj[k] = parseDt(v)
29
32
  } catch (err) {
@@ -0,0 +1,13 @@
1
+ import isSet from './is-set.js'
2
+
3
+ function pick (obj, items, excludeUnset) {
4
+ const result = {}
5
+ items.forEach(item => {
6
+ const [k, nk] = item.split(':')
7
+ if (excludeUnset && !isSet(obj[k])) return undefined
8
+ result[nk ?? k] = obj[k]
9
+ })
10
+ return result
11
+ }
12
+
13
+ export default pick
@@ -0,0 +1,6 @@
1
+ function round (val, scale = 0) {
2
+ scale = scale <= 0 ? 1 : 10 ** scale
3
+ return Math.round(val * scale) / scale
4
+ }
5
+
6
+ export default round
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {
@@ -40,9 +40,8 @@
40
40
  "lodash-es": "^4.17.21",
41
41
  "ms": "^2.1.3",
42
42
  "nanoid": "^4.0.2",
43
- "node-fetch": "^3.3.1",
44
43
  "omit-deep": "^0.3.0",
45
- "ora": "^6.3.1",
44
+ "ora": "^8.0.1",
46
45
  "outmatch": "^0.7.0",
47
46
  "proper-lockfile": "^4.1.2",
48
47
  "semver": "^7.5.1",