bajo 1.1.0 → 1.1.2

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.
Files changed (73) hide show
  1. package/bajo/intl/en-US.json +4 -2
  2. package/bajo/intl/id.json +4 -2
  3. package/boot/class/app.js +8 -10
  4. package/boot/class/bajo-core/{helper/boot-order.js → boot-order.js} +1 -2
  5. package/boot/class/bajo-core/boot-plugins.js +19 -0
  6. package/boot/class/bajo-core/{helper/build-config.js → build-config.js} +15 -17
  7. package/boot/class/bajo-core/{helper/build-plugins.js → build-plugins.js} +2 -3
  8. package/boot/class/bajo-core/{helper/collect-config-handlers.js → collect-config-handlers.js} +2 -4
  9. package/boot/class/bajo-core.js +739 -11
  10. package/boot/class/bajo-plugin/{helper/attach-method.js → attach-method.js} +1 -1
  11. package/boot/class/bajo-plugin.js +1 -2
  12. package/boot/class/log.js +4 -5
  13. package/boot/class/print.js +1 -2
  14. package/boot/lib/create-method.js +3 -5
  15. package/boot/lib/current-loc.js +1 -1
  16. package/boot/lib/import-module.js +27 -0
  17. package/boot/lib/log-levels.js +1 -0
  18. package/boot/lib/parse-args-argv.js +2 -3
  19. package/boot/lib/read-all-configs.js +3 -6
  20. package/boot/{class/bajo-core/method → lib}/resolve-path.js +1 -1
  21. package/package.json +1 -1
  22. package/boot/class/bajo-core/helper/attach-method.js +0 -31
  23. package/boot/class/bajo-core/helper/boot-plugins.js +0 -19
  24. package/boot/class/bajo-core/method/arrange-array.js +0 -19
  25. package/boot/class/bajo-core/method/break-ns-path-from-file.js +0 -25
  26. package/boot/class/bajo-core/method/break-ns-path.js +0 -31
  27. package/boot/class/bajo-core/method/build-collections.js +0 -48
  28. package/boot/class/bajo-core/method/call-handler.js +0 -31
  29. package/boot/class/bajo-core/method/defaults-deep.js +0 -17
  30. package/boot/class/bajo-core/method/each-plugins.js +0 -57
  31. package/boot/class/bajo-core/method/envs.js +0 -7
  32. package/boot/class/bajo-core/method/extract-text.js +0 -15
  33. package/boot/class/bajo-core/method/format.js +0 -36
  34. package/boot/class/bajo-core/method/generate-id.js +0 -23
  35. package/boot/class/bajo-core/method/get-global-module-dir.js +0 -28
  36. package/boot/class/bajo-core/method/get-key-by-value.js +0 -5
  37. package/boot/class/bajo-core/method/get-method.js +0 -12
  38. package/boot/class/bajo-core/method/get-module-dir.js +0 -35
  39. package/boot/class/bajo-core/method/get-plugin-data-dir.js +0 -11
  40. package/boot/class/bajo-core/method/get-plugin-file.js +0 -21
  41. package/boot/class/bajo-core/method/get-plugin.js +0 -23
  42. package/boot/class/bajo-core/method/import-module.js +0 -27
  43. package/boot/class/bajo-core/method/import-pkg.js +0 -48
  44. package/boot/class/bajo-core/method/includes.js +0 -11
  45. package/boot/class/bajo-core/method/is-class.js +0 -7
  46. package/boot/class/bajo-core/method/is-empty-dir.js +0 -9
  47. package/boot/class/bajo-core/method/is-log-in-range.js +0 -12
  48. package/boot/class/bajo-core/method/is-set.js +0 -5
  49. package/boot/class/bajo-core/method/is-valid-app.js +0 -12
  50. package/boot/class/bajo-core/method/is-valid-plugin.js +0 -12
  51. package/boot/class/bajo-core/method/join.js +0 -20
  52. package/boot/class/bajo-core/method/log-levels.js +0 -9
  53. package/boot/class/bajo-core/method/num-unit.js +0 -11
  54. package/boot/class/bajo-core/method/paginate.js +0 -28
  55. package/boot/class/bajo-core/method/parse-object.js +0 -62
  56. package/boot/class/bajo-core/method/pascal-case.js +0 -9
  57. package/boot/class/bajo-core/method/pick.js +0 -13
  58. package/boot/class/bajo-core/method/read-config.js +0 -50
  59. package/boot/class/bajo-core/method/read-json.js +0 -14
  60. package/boot/class/bajo-core/method/round.js +0 -6
  61. package/boot/class/bajo-core/method/run-hook.js +0 -28
  62. package/boot/class/bajo-core/method/save-as-download.js +0 -19
  63. package/boot/class/bajo-core/method/sec-to-hms.js +0 -25
  64. package/boot/class/bajo-core/method/titleize.js +0 -24
  65. package/boot/class/bajo-core/method/white-space.js +0 -3
  66. /package/boot/class/bajo-core/{helper/exit-handler.js → exit-handler.js} +0 -0
  67. /package/boot/class/bajo-core/{helper/run-as-applet.js → run-as-applet.js} +0 -0
  68. /package/boot/class/bajo-plugin/{helper/build-config.js → build-config.js} +0 -0
  69. /package/boot/class/bajo-plugin/{helper/check-clash.js → check-clash.js} +0 -0
  70. /package/boot/class/bajo-plugin/{helper/check-dependency.js → check-dependency.js} +0 -0
  71. /package/boot/class/bajo-plugin/{helper/collect-exit-handlers.js → collect-exit-handlers.js} +0 -0
  72. /package/boot/class/bajo-plugin/{helper/collect-hooks.js → collect-hooks.js} +0 -0
  73. /package/boot/class/bajo-plugin/{helper/run.js → run.js} +0 -0
@@ -1,4 +1,4 @@
1
- import createMethod from '../../../lib/create-method.js'
1
+ import createMethod from '../../lib/create-method.js'
2
2
 
3
3
  async function attachMethod () {
4
4
  const { eachPlugins } = this.bajo
@@ -2,7 +2,6 @@ import Plugin from './plugin.js'
2
2
  import lodash from 'lodash'
3
3
  import omittedPluginKeys from '../lib/omitted-plugin-keys.js'
4
4
  import readAllConfigs from '../lib/read-all-configs.js'
5
- import titleize from './bajo-core/method/titleize.js'
6
5
  import fs from 'fs-extra'
7
6
 
8
7
  const { pick, omit, camelCase, trim, without } = lodash
@@ -42,7 +41,7 @@ class BajoPlugin extends Plugin {
42
41
  } catch (err) {}
43
42
  const envArgv = defaultsDeep({}, omit(this.app.env[this.name] ?? {}, omittedPluginKeys) ?? {}, omit(this.app.argv[this.name] ?? {}, omittedPluginKeys) ?? {})
44
43
  cfg = defaultsDeep({}, envArgv ?? {}, cfg ?? {}, this.config ?? {})
45
- this.title = this.title ?? cfg.title ?? titleize(this.alias)
44
+ this.title = this.title ?? cfg.title ?? this.app.bajo.titleize(this.alias)
46
45
 
47
46
  this.dependencies = this.dependencies ?? []
48
47
  const depFile = `${dir}/plugin/.dependencies`
package/boot/class/log.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import os from 'os'
2
2
  import lodash from 'lodash'
3
- import levels from './bajo-core/method/log-levels.js'
4
- import isLogInRange from './bajo-core/method/is-log-in-range.js'
5
3
  import dayjs from 'dayjs'
4
+ import logLevels from '../lib/log-levels.js'
6
5
 
7
6
  const { isEmpty, without, merge, upperFirst } = lodash
8
7
 
@@ -31,7 +30,7 @@ class Log {
31
30
 
32
31
  formatMsg = (level, ...params) => {
33
32
  if (this.plugin.app.bajo.config.log.level === 'silent') return
34
- if (!isLogInRange.call(this.plugin.app.bajo, level)) return
33
+ if (!this.plugin.app.bajo.isLogInRange(level)) return
35
34
  let [data, msg, ...args] = params
36
35
  if (typeof data === 'string') {
37
36
  args.unshift(msg)
@@ -46,7 +45,7 @@ class Log {
46
45
  let text
47
46
  const dt = new Date()
48
47
  if (this.plugin.app.bajo.config.env === 'prod') {
49
- const json = { level: levels[level], time: dt.valueOf(), pid: process.pid, hostname: os.hostname() }
48
+ const json = { level: logLevels[level], time: dt.valueOf(), pid: process.pid, hostname: os.hostname() }
50
49
  if (!isEmpty(data)) merge(json, data)
51
50
  merge(json, { msg })
52
51
  text = JSON.stringify(json)
@@ -59,7 +58,7 @@ class Log {
59
58
  }
60
59
  }
61
60
 
62
- Object.keys(levels).forEach(level => {
61
+ Object.keys(logLevels).forEach(level => {
63
62
  Log.prototype[level] = function (...params) {
64
63
  this.formatMsg(level, ...params)
65
64
  }
@@ -1,6 +1,5 @@
1
1
  import ora from 'ora'
2
2
  import lodash from 'lodash'
3
- import defaultsDeep from './bajo-core/method/defaults-deep.js'
4
3
  import fs from 'fs-extra'
5
4
  import Sprintf from 'sprintf-js'
6
5
  const { sprintf } = Sprintf
@@ -72,7 +71,7 @@ class Print {
72
71
  let opts = {}
73
72
  if (isPlainObject(args.slice(-1)[0])) opts = args.pop()
74
73
  this.opts.isSilent = !!(config.silent || this.opts.isSilent)
75
- this.opts = defaultsDeep(opts, this.opts)
74
+ this.opts = this.plugin.app.bajo.defaultsDeep(opts, this.opts)
76
75
  }
77
76
 
78
77
  setText = (text, ...args) => {
@@ -1,9 +1,7 @@
1
1
  import fastGlob from 'fast-glob'
2
2
  import path from 'path'
3
3
  import lodash from 'lodash'
4
- import resolvePath from '../class/bajo-core/method/resolve-path.js'
5
- import importModule from '../class/bajo-core/method/import-module.js'
6
- import readJson from '../class/bajo-core/method/read-json.js'
4
+ import resolvePath from './resolve-path.js'
7
5
 
8
6
  const { camelCase, isFunction, isPlainObject, forOwn } = lodash
9
7
 
@@ -15,8 +13,8 @@ export default async function (dir, pkg = 'bajo') {
15
13
  const base = f.replace(dir, '').replace(ext, '')
16
14
  const name = camelCase(base)
17
15
  let mod
18
- if (ext === '.json') mod = readJson(f)
19
- else mod = await importModule(f)
16
+ if (ext === '.json') mod = this.app.bajo.readJson(f)
17
+ else mod = await this.app.bajo.importModule(f)
20
18
  if (isFunction(mod)) {
21
19
  mod = mod.bind(this)
22
20
  } else if (isPlainObject(mod)) {
@@ -1,5 +1,5 @@
1
1
  import path from 'path'
2
- import resolvePath from '../class/bajo-core/method/resolve-path.js'
2
+ import resolvePath from './resolve-path.js'
3
3
  import { fileURLToPath } from 'url'
4
4
 
5
5
  const currentLoc = (meta) => {
@@ -0,0 +1,27 @@
1
+ import resolvePath from './resolve-path.js'
2
+ import fs from 'fs-extra'
3
+ import lodash from 'lodash'
4
+ const { isFunction, isPlainObject } = lodash
5
+
6
+ async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
7
+ const load = async (file, asDefaultImport = true, noCache = false) => {
8
+ file = resolvePath(file, true)
9
+ if (noCache) file += `?_=${Date.now()}`
10
+ const imported = await import(file)
11
+ if (asDefaultImport) return imported.default
12
+ return imported
13
+ }
14
+
15
+ if (this) file = this.getPluginFile(file)
16
+ if (!fs.existsSync(file)) return
17
+ let mod = await load(file, asDefaultImport, noCache)
18
+ if (!asHandler) return mod
19
+ if (isFunction(mod)) mod = { level: 999, handler: mod }
20
+ if (!isPlainObject(mod)) {
21
+ if (this) throw this.error('fileNotModuleHandler%s', file)
22
+ throw new Error(`File '${file}' is NOT a handler module`)
23
+ }
24
+ return mod
25
+ }
26
+
27
+ export default importModule
@@ -0,0 +1 @@
1
+ export default { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60, silent: 99 }
@@ -1,9 +1,8 @@
1
1
  import yargs from 'yargs'
2
2
  import { Parser } from 'yargs/helpers'
3
3
  import { unflatten } from 'flat'
4
- import isSet from '../class/bajo-core/method/is-set.js'
5
4
  import dotenvParseVariables from 'dotenv-parse-variables'
6
- import importModule from '../class/bajo-core/method/import-module.js'
5
+ import importModule from './import-module.js'
7
6
  import lodash from 'lodash'
8
7
  import fs from 'fs-extra'
9
8
  import path from 'path'
@@ -51,7 +50,7 @@ const parseWithYargs = async () => {
51
50
  }
52
51
 
53
52
  async function parseArgsArgv ({ delimiter = '-', splitter = ':', useParser } = {}) {
54
- if (!isSet(useParser)) useParser = find(process.argv, a => a.startsWith('--spawn'))
53
+ if (!useParser) useParser = find(process.argv, a => a.startsWith('--spawn'))
55
54
  let argv = useParser ? await parseWithParser() : await parseWithYargs()
56
55
  const args = argv._
57
56
  delete argv._
@@ -1,22 +1,19 @@
1
- import readConfig from '../class/bajo-core/method/read-config.js'
2
- import defaultsDeep from '../class/bajo-core/method/defaults-deep.js'
3
-
4
1
  async function readAllConfigs (base) {
5
2
  let cfg = {}
6
3
  let ext = {}
7
4
  // default config file
8
5
  try {
9
- cfg = await readConfig.call(this.bajo, `${base}.*`, { ignoreError: true })
6
+ cfg = await this.bajo.readConfig(`${base}.*`, { ignoreError: true })
10
7
  } catch (err) {
11
8
  if (['BAJO_CONFIG_NO_PARSER'].includes(err.code)) throw err
12
9
  }
13
10
  // env based config file
14
11
  try {
15
- ext = await readConfig.call(this.bajo, `${base}-${this.bajo.config.env}.*`, { ignoreError: true })
12
+ ext = await this.bajo.readConfig(`${base}-${this.bajo.config.env}.*`, { ignoreError: true })
16
13
  } catch (err) {
17
14
  if (!['BAJO_CONFIG_FILE_NOT_FOUND'].includes(err.code)) throw err
18
15
  }
19
- return defaultsDeep({}, ext, cfg)
16
+ return this.bajo.defaultsDeep({}, ext, cfg)
20
17
  /*
21
18
  try {
22
19
  cfg = await readConfig.call(this.bajo, `${base}-${this.bajo.config.env}.*`)
@@ -2,7 +2,7 @@ import path from 'path'
2
2
  import { fileURLToPath } from 'url'
3
3
  import os from 'os'
4
4
 
5
- const resolvePath = (item, asFileUrl) => {
5
+ function resolvePath (item, asFileUrl) {
6
6
  if (item.startsWith('file://')) item = fileURLToPath(item)
7
7
  item = path.resolve(item)
8
8
  if (os.platform() === 'win32') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {
@@ -1,31 +0,0 @@
1
- import createMethod from '../../../lib/create-method.js'
2
- import deepFreeze from 'deep-freeze-strict'
3
- import currentLoc from '../../../lib/current-loc.js'
4
- import fs from 'fs-extra'
5
- import fastGlob from 'fast-glob'
6
- import { sprintf } from 'sprintf-js'
7
- import outmatch from 'outmatch'
8
- import lodash from 'lodash'
9
-
10
- export default async function () {
11
- await createMethod.call(this, `${currentLoc(import.meta).dir}/../method`)
12
- this.freeze = (o, shallow) => {
13
- if (shallow) Object.freeze(o)
14
- else deepFreeze(o)
15
- }
16
- this.setImmediate = function () {
17
- return new Promise((resolve) => {
18
- setImmediate(() => resolve())
19
- })
20
- }
21
- // commonly used libraries
22
- this.lib._ = lodash
23
- this.lib.fs = fs
24
- this.lib.fastGlob = fastGlob
25
- this.lib.sprintf = sprintf
26
- this.lib.outmatch = outmatch
27
- // last cleanup
28
- if (!fs.existsSync(this.dir.data)) {
29
- this.log.warn('ddirNotExists%s', this.dir.data)
30
- }
31
- }
@@ -1,19 +0,0 @@
1
- import buildConfig from '../../bajo-plugin/helper/build-config.js'
2
- import checkDependency from '../../bajo-plugin/helper/check-dependency.js'
3
- import checkClash from '../../bajo-plugin/helper/check-clash.js'
4
- import attachMethod from '../../bajo-plugin/helper/attach-method.js'
5
- import collectHooks from '../../bajo-plugin/helper/collect-hooks.js'
6
- import run from '../../bajo-plugin/helper/run.js'
7
- import collectExitHandlers from '../../bajo-plugin/helper/collect-exit-handlers.js'
8
-
9
- async function bootPlugins () {
10
- await buildConfig.call(this.app)
11
- await checkClash.call(this.app)
12
- await checkDependency.call(this.app)
13
- await attachMethod.call(this.app)
14
- await collectHooks.call(this.app)
15
- await collectExitHandlers.call(this.app)
16
- await run.call(this.app)
17
- }
18
-
19
- export default bootPlugins
@@ -1,19 +0,0 @@
1
- import lodash from 'lodash'
2
- const { filter, trim } = lodash
3
-
4
- function arrangeArray (inputs, trimItem = true) {
5
- const first = []
6
- const last = []
7
-
8
- const items = filter(inputs, item => {
9
- if (trimItem) item = trim(item)
10
- if (item[0] === '^') first.push(item.slice(1))
11
- else if (item[0] === '$') last.push(item.slice(1))
12
- return !['^', '$'].includes(item[0])
13
- })
14
- items.unshift(...first)
15
- items.push(...last)
16
- return items
17
- }
18
-
19
- export default arrangeArray
@@ -1,25 +0,0 @@
1
- import _path from 'path'
2
- import lodash from 'lodash'
3
- const { map, camelCase } = lodash
4
-
5
- function breakNsPathFromFile ({ file, dir, baseNs, suffix = '', getType } = {}) {
6
- let item = file.replace(dir + suffix, '')
7
- let type
8
- if (getType) {
9
- const items = item.split('/')
10
- type = items.shift()
11
- item = items.join('/')
12
- }
13
- item = item.slice(0, item.length - _path.extname(item).length)
14
- let [name, path] = item.split('@')
15
- if (!path) {
16
- path = name
17
- name = baseNs
18
- }
19
- path = camelCase(path)
20
- const names = map(name.split('.'), n => camelCase(n))
21
- const [ns, subNs] = names
22
- return { ns, subNs, path, fullNs: names.join('.'), type }
23
- }
24
-
25
- export default breakNsPathFromFile
@@ -1,31 +0,0 @@
1
- import lodash from 'lodash'
2
- import querystring from 'querystring'
3
-
4
- const { isEmpty } = lodash
5
-
6
- function breakNsPath (item = '', defaultNs = 'bajo', checkNs = true) {
7
- let [ns, ...path] = item.split(':')
8
- let subNs
9
- let subSubNs
10
- path = path.join(':')
11
- if (path.startsWith('//')) return { ns: undefined, path: item } // for: http:// etc
12
- if (isEmpty(path)) {
13
- path = ns
14
- ns = defaultNs
15
- }
16
- [ns, subNs, subSubNs] = ns.split('.')
17
- if (checkNs) {
18
- if (!this.app[ns]) {
19
- const plugin = this.getPlugin(ns)
20
- if (plugin) ns = plugin.name
21
- }
22
- if (!this.app[ns]) throw this.error('unknownPluginOrNotLoaded%s')
23
- }
24
- const fullPath = path
25
- let qs
26
- [path, qs] = path.split('?')
27
- qs = querystring.parse(qs) ?? {}
28
- return { ns, path, subNs, subSubNs, qs, fullPath }
29
- }
30
-
31
- export default breakNsPath
@@ -1,48 +0,0 @@
1
- import lodash from 'lodash'
2
-
3
- const { filter, isArray, pullAt, camelCase, has, find, set, get, isFunction } = lodash
4
-
5
- async function buildCollections (options = {}) {
6
- const { runHook, join } = this.app.bajo
7
- let { ns, handler, dupChecks = [], container, useDefaultName } = options
8
- useDefaultName = useDefaultName ?? true
9
- if (!ns) ns = this.name
10
- const cfg = this.app[ns].getConfig()
11
- let items = get(cfg, container, [])
12
- if (!isArray(items)) items = [items]
13
- this.app[ns].log.trace('collecting%s', this.app[ns].print.write(container))
14
- await runHook(`${ns}:${camelCase('beforeBuildCollection')}`, container)
15
- const deleted = []
16
- for (const index in items) {
17
- const item = items[index]
18
- if (useDefaultName) {
19
- if (!has(item, 'name')) {
20
- if (find(items, { name: 'default' })) throw this.app[ns].error('collExists%s', 'default')
21
- else item.name = 'default'
22
- }
23
- }
24
- this.app[ns].log.trace('- %s', item.name)
25
- const result = await handler.call(this.app[ns], { item, index, cfg })
26
- if (result) items[index] = result
27
- else if (result === false) deleted.push(index)
28
- if (this.app.bajo.applet && item.skipOnTool && !deleted.includes(index)) deleted.push(index)
29
- }
30
- if (deleted.length > 0) pullAt(items, deleted)
31
-
32
- // check for duplicity
33
- for (const c of items) {
34
- for (const d of dupChecks) {
35
- if (isFunction(d)) await d.call(this.app[ns], c, items)
36
- else {
37
- const checker = set({}, d, c[d])
38
- const match = filter(items, checker)
39
- if (match.length > 1) this.app[ns].fatal('oneOrMoreSharedTheSame%s%s', container, join(dupChecks.filter(i => !isFunction(i))))
40
- }
41
- }
42
- }
43
- await runHook(`${ns}:${camelCase('afterBuildCollection')}`, container)
44
- this.app[ns].log.debug('collected%s%d', this.app[ns].print.write(container), items.length)
45
- return items
46
- }
47
-
48
- export default buildCollections
@@ -1,31 +0,0 @@
1
- import lodash from 'lodash'
2
- import BajoPlugin from '../../bajo-plugin.js'
3
-
4
- const { isString, isFunction, isPlainObject, find } = lodash
5
-
6
- async function callHandler (item, ...args) {
7
- let result
8
- let scope = this
9
- if (item instanceof BajoPlugin) {
10
- scope = item
11
- item = args.shift()
12
- }
13
- const bajo = scope.app.bajo
14
- if (isString(item)) {
15
- if (item.startsWith('applet:') && bajo.applets.length > 0) {
16
- const [, ns, path] = item.split(':')
17
- const applet = find(bajo.applets, a => (a.ns === ns || a.alias === ns))
18
- if (applet) result = await bajo.runApplet(applet, path, ...args)
19
- } else {
20
- const method = bajo.getMethod(item)
21
- if (method) result = await method(...args)
22
- }
23
- } else if (isFunction(item)) {
24
- result = await item.call(scope, ...args)
25
- } else if (isPlainObject(item) && item.handler) {
26
- result = await item.handler.call(scope, ...args)
27
- }
28
- return result
29
- }
30
-
31
- export default callHandler
@@ -1,17 +0,0 @@
1
- import lodash from 'lodash'
2
-
3
- const { mergeWith, isArray } = lodash
4
-
5
- // based on https://github.com/nodeutils/defaults-deep/blob/master/lib/index.js
6
-
7
- const defaultsDeep = (...args) => {
8
- const output = {}
9
- args.reverse().forEach(function (item) {
10
- mergeWith(output, item, function (objectValue, sourceValue) {
11
- return isArray(sourceValue) ? sourceValue : undefined
12
- })
13
- })
14
- return output
15
- }
16
-
17
- export default defaultsDeep
@@ -1,57 +0,0 @@
1
- import lodash from 'lodash'
2
- import fastGlob from 'fast-glob'
3
- import path from 'path'
4
-
5
- const { camelCase, isString, omit, cloneDeep } = lodash
6
-
7
- async function eachPlugins (handler, options = {}) {
8
- if (typeof options === 'string') options = { glob: options }
9
- const result = {}
10
- const pluginPkgs = cloneDeep(this.app.bajo.pluginPkgs) ?? []
11
- const { glob, useBajo, prefix = '', noUnderscore = true, returnItems } = options
12
- if (useBajo) pluginPkgs.unshift('bajo')
13
- for (const pkgName of pluginPkgs) {
14
- const ns = camelCase(pkgName)
15
- const config = this.app[ns].config
16
- const alias = this.app[ns].alias
17
- let r
18
- if (glob) {
19
- const base = prefix === '' ? this.app[ns].dir.pkg : `${this.app[ns].dir.pkg}/${prefix}`
20
- let opts = isString(glob) ? { pattern: [glob] } : glob
21
- let pattern = opts.pattern ?? []
22
- if (isString(pattern)) pattern = [pattern]
23
- opts = omit(opts, ['pattern'])
24
- for (const i in pattern) {
25
- if (!path.isAbsolute(pattern[i])) pattern[i] = `${base}/${pattern[i]}`
26
- }
27
- const files = await fastGlob(pattern, opts)
28
- for (const f of files) {
29
- if (path.basename(f)[0] === '_' && noUnderscore) continue
30
- const resp = await handler.call(this.app[ns], { ns, pkgName, config, alias, file: f, dir: base })
31
- if (resp === false) break
32
- else if (resp === undefined) continue
33
- else {
34
- result[ns] = result[ns] ?? {}
35
- result[ns][f] = resp
36
- }
37
- }
38
- } else {
39
- r = await handler.call(this.app[ns], { ns, pkgName, config, dir: this.app[ns].dir.pkg, alias })
40
- if (r === false) break
41
- else if (r === undefined) continue
42
- else result[ns] = r
43
- }
44
- }
45
- if (returnItems) {
46
- const data = []
47
- for (const r in result) {
48
- for (const f in result[r]) {
49
- data.push(result[r][f])
50
- }
51
- }
52
- return data
53
- }
54
- return result
55
- }
56
-
57
- export default eachPlugins
@@ -1,7 +0,0 @@
1
- const envs = {
2
- dev: 'development',
3
- staging: 'staging',
4
- prod: 'production'
5
- }
6
-
7
- export default envs
@@ -1,15 +0,0 @@
1
- function extractText (text, patternStart, patternEnd) {
2
- let result = ''
3
- const open = text.indexOf(patternStart)
4
- if (open > -1) {
5
- text = text.slice(open + patternStart.length)
6
- const close = text.indexOf(patternEnd)
7
- if (close > -1) {
8
- result = text.slice(0, close)
9
- }
10
- }
11
- const pattern = `${patternStart}${result}${patternEnd}`
12
- return { result, pattern }
13
- }
14
-
15
- export default extractText
@@ -1,36 +0,0 @@
1
- function format (value, type, options = {}) {
2
- const { format } = this.config.intl
3
- const { emptyValue = format.emptyValue } = options
4
- const lang = options.lang ?? this.config.lang
5
- if ([undefined, null, ''].includes(value)) return emptyValue
6
- if (type === 'auto') {
7
- if (value instanceof Date) type = 'datetime'
8
- }
9
- if (['integer', 'smallint'].includes(type)) {
10
- value = parseInt(value)
11
- if (isNaN(value)) return emptyValue
12
- const setting = this.defaultsDeep(options.integer, format.integer)
13
- return new Intl.NumberFormat(lang, setting).format(value)
14
- }
15
- if (['float', 'double'].includes(type)) {
16
- value = parseFloat(value)
17
- if (isNaN(value)) return emptyValue
18
- if (this.app.bajoSpatial && options.latitude) return this.app.bajoSpatial.latToDms(value)
19
- if (this.app.bajoSpatial && options.longitude) return this.app.bajoSpatial.lngToDms(value)
20
- const setting = this.defaultsDeep(options.float, format.float)
21
- return new Intl.NumberFormat(lang, setting).format(value)
22
- }
23
- if (['datetime', 'date'].includes(type)) {
24
- const setting = this.defaultsDeep(options[type], format[type])
25
- return new Intl.DateTimeFormat(lang, setting).format(new Date(value))
26
- }
27
- if (['time'].includes(type)) {
28
- const setting = this.defaultsDeep(options.time, format.time)
29
- return new Intl.DateTimeFormat(lang, setting).format(new Date(`1970-01-01T${value}Z`))
30
- }
31
- if (['array'].includes(type)) return value.join(', ')
32
- if (['object'].includes(type)) return JSON.stringify(value)
33
- return value
34
- }
35
-
36
- export default format
@@ -1,23 +0,0 @@
1
- import { customAlphabet } from 'nanoid'
2
-
3
- const generateId = (options = {}) => {
4
- let type
5
- if (options === true) options = 'alpha'
6
- if (options === 'int') {
7
- type = options
8
- options = { pattern: '0123456789', length: 15 }
9
- } else if (options === 'alpha') {
10
- type = options
11
- options = { pattern: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', length: 15 }
12
- }
13
- let { pattern, length = 13, returnInstance } = options
14
- pattern = pattern ?? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
15
- if (options.case === 'lower') pattern = pattern.toLowerCase()
16
- else if (options.case === 'upper') pattern = pattern.toUpperCase()
17
- const nid = customAlphabet(pattern, length)
18
- if (returnInstance) return nid
19
- const value = nid()
20
- return type === 'int' ? parseInt(value) : value
21
- }
22
-
23
- export default generateId
@@ -1,28 +0,0 @@
1
- import getGlobalPath from 'get-global-path'
2
- import resolvePath from './resolve-path.js'
3
- import lodash from 'lodash'
4
- import fs from 'fs-extra'
5
-
6
- const { dropRight } = lodash
7
-
8
- function getGlobalModuleDir (pkgName, silent = true) {
9
- let nodeModulesDir = process.env.BAJO_GLOBAL_MODULE_DIR
10
- if (!nodeModulesDir) {
11
- const npmPath = getGlobalPath('npm')
12
- if (!npmPath) {
13
- if (silent) return
14
- throw this.error('cantLocateNpmGlobalDir', { code: 'BAJO_CANT_LOCATE_NPM_GLOBAL_DIR' })
15
- }
16
- nodeModulesDir = dropRight(resolvePath(npmPath).split('/'), 1).join('/')
17
- process.env.BAJO_GLOBAL_MODULE_DIR = nodeModulesDir
18
- }
19
- if (!pkgName) return nodeModulesDir
20
- const dir = `${nodeModulesDir}/${pkgName}`
21
- if (!fs.existsSync(dir)) {
22
- if (silent) return
23
- throw this.error('cantLocateGlobalDir%s', pkgName, { code: 'BAJO_CANT_LOCATE_MODULE_GLOBAL_DIR' })
24
- }
25
- return dir
26
- }
27
-
28
- export default getGlobalModuleDir
@@ -1,5 +0,0 @@
1
- const getKeyByValue = (object, value) => {
2
- return Object.keys(object).find(key => object[key] === value)
3
- }
4
-
5
- export default getKeyByValue
@@ -1,12 +0,0 @@
1
- import lodash from 'lodash'
2
-
3
- const { get, isFunction } = lodash
4
-
5
- function getMethod (name = '', thrown = true) {
6
- const { ns, path } = this.breakNsPath(name)
7
- const method = get(this.app, `${ns}.${path}`)
8
- if (method && isFunction(method)) return method
9
- if (thrown) throw this.error('cantFindMethod%s', name)
10
- }
11
-
12
- export default getMethod