bajo 1.0.11 → 1.1.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/bajo/intl/en-US.json +5 -2
- package/bajo/intl/id.json +5 -2
- package/boot/class/app.js +11 -13
- package/boot/class/bajo-core/{helper/boot-order.js → boot-order.js} +3 -4
- package/boot/class/bajo-core/boot-plugins.js +19 -0
- package/boot/class/bajo-core/{helper/build-config.js → build-config.js} +16 -18
- package/boot/class/bajo-core/{helper/build-plugins.js → build-plugins.js} +13 -5
- package/boot/class/bajo-core/{helper/collect-config-handlers.js → collect-config-handlers.js} +2 -4
- package/boot/class/bajo-core.js +738 -11
- package/boot/class/bajo-plugin/{helper/attach-method.js → attach-method.js} +2 -2
- package/boot/class/bajo-plugin/{helper/collect-exit-handlers.js → collect-exit-handlers.js} +1 -1
- package/boot/class/bajo-plugin.js +13 -14
- package/boot/class/error.js +10 -2
- package/boot/class/log.js +9 -10
- package/boot/class/plugin.js +8 -12
- package/boot/class/print.js +16 -17
- package/boot/lib/create-method.js +3 -5
- package/boot/lib/current-loc.js +1 -1
- package/boot/lib/import-module.js +27 -0
- package/boot/lib/log-levels.js +1 -0
- package/boot/lib/parse-args-argv.js +2 -3
- package/boot/lib/read-all-configs.js +3 -6
- package/boot/{class/bajo-core/method → lib}/resolve-path.js +1 -1
- package/package.json +1 -1
- package/boot/class/bajo-core/helper/attach-method.js +0 -31
- package/boot/class/bajo-core/helper/boot-plugins.js +0 -19
- package/boot/class/bajo-core/method/arrange-array.js +0 -19
- package/boot/class/bajo-core/method/break-ns-path-from-file.js +0 -25
- package/boot/class/bajo-core/method/break-ns-path.js +0 -31
- package/boot/class/bajo-core/method/build-collections.js +0 -48
- package/boot/class/bajo-core/method/call-handler.js +0 -31
- package/boot/class/bajo-core/method/defaults-deep.js +0 -17
- package/boot/class/bajo-core/method/each-plugins.js +0 -57
- package/boot/class/bajo-core/method/envs.js +0 -7
- package/boot/class/bajo-core/method/extract-text.js +0 -15
- package/boot/class/bajo-core/method/format.js +0 -36
- package/boot/class/bajo-core/method/generate-id.js +0 -23
- package/boot/class/bajo-core/method/get-global-module-dir.js +0 -28
- package/boot/class/bajo-core/method/get-key-by-value.js +0 -5
- package/boot/class/bajo-core/method/get-method.js +0 -12
- package/boot/class/bajo-core/method/get-module-dir.js +0 -35
- package/boot/class/bajo-core/method/get-plugin-data-dir.js +0 -11
- package/boot/class/bajo-core/method/get-plugin-file.js +0 -21
- package/boot/class/bajo-core/method/get-plugin.js +0 -23
- package/boot/class/bajo-core/method/import-module.js +0 -27
- package/boot/class/bajo-core/method/import-pkg.js +0 -48
- package/boot/class/bajo-core/method/includes.js +0 -11
- package/boot/class/bajo-core/method/is-class.js +0 -7
- package/boot/class/bajo-core/method/is-empty-dir.js +0 -9
- package/boot/class/bajo-core/method/is-log-in-range.js +0 -12
- package/boot/class/bajo-core/method/is-set.js +0 -5
- package/boot/class/bajo-core/method/is-valid-app.js +0 -12
- package/boot/class/bajo-core/method/is-valid-plugin.js +0 -12
- package/boot/class/bajo-core/method/join.js +0 -20
- package/boot/class/bajo-core/method/log-levels.js +0 -9
- package/boot/class/bajo-core/method/num-unit.js +0 -11
- package/boot/class/bajo-core/method/paginate.js +0 -28
- package/boot/class/bajo-core/method/parse-object.js +0 -62
- package/boot/class/bajo-core/method/pascal-case.js +0 -9
- package/boot/class/bajo-core/method/pick.js +0 -13
- package/boot/class/bajo-core/method/read-config.js +0 -50
- package/boot/class/bajo-core/method/read-json.js +0 -14
- package/boot/class/bajo-core/method/round.js +0 -6
- package/boot/class/bajo-core/method/run-hook.js +0 -28
- package/boot/class/bajo-core/method/save-as-download.js +0 -19
- package/boot/class/bajo-core/method/sec-to-hms.js +0 -25
- package/boot/class/bajo-core/method/slice-string.js +0 -13
- package/boot/class/bajo-core/method/titleize.js +0 -24
- package/boot/class/bajo-core/method/white-space.js +0 -3
- package/boot/lib/translate.js +0 -19
- /package/boot/class/bajo-core/{helper/exit-handler.js → exit-handler.js} +0 -0
- /package/boot/class/bajo-core/{helper/run-as-applet.js → run-as-applet.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/build-config.js → build-config.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/check-clash.js → check-clash.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/check-dependency.js → check-dependency.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/collect-hooks.js → collect-hooks.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/run.js → run.js} +0 -0
|
@@ -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,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,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
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import fs from 'fs-extra'
|
|
3
|
-
import getGlobalModuleDir from './get-global-module-dir.js'
|
|
4
|
-
import resolvePath from './resolve-path.js'
|
|
5
|
-
import { createRequire } from 'module'
|
|
6
|
-
const require = createRequire(import.meta.url)
|
|
7
|
-
|
|
8
|
-
function findDeep (item, paths) {
|
|
9
|
-
let dir
|
|
10
|
-
for (const p of paths) {
|
|
11
|
-
const d = `${p}/${item}`
|
|
12
|
-
if (fs.existsSync(d)) {
|
|
13
|
-
dir = d
|
|
14
|
-
break
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return dir
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function getModuleDir (pkgName, base) {
|
|
21
|
-
if (pkgName === 'main') return resolvePath(process.env.BAJOCWD)
|
|
22
|
-
if (base === 'main') base = process.env.BAJOCWD
|
|
23
|
-
else if (this && this.app && this.app[base]) base = this.app[base].pkgName
|
|
24
|
-
const pkgPath = pkgName + '/package.json'
|
|
25
|
-
const paths = require.resolve.paths(pkgPath)
|
|
26
|
-
const gdir = getGlobalModuleDir()
|
|
27
|
-
paths.unshift(gdir)
|
|
28
|
-
paths.unshift(resolvePath(path.join(process.env.BAJOCWD, 'node_modules')))
|
|
29
|
-
let dir = findDeep(pkgPath, paths)
|
|
30
|
-
if (base && !dir) dir = findDeep(`${base}/node_modules/${pkgPath}`, paths)
|
|
31
|
-
if (!dir) return null
|
|
32
|
-
return resolvePath(path.dirname(dir))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export default getModuleDir
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
|
|
3
|
-
function getPluginDataDir (name, ensureDir = true) {
|
|
4
|
-
const { getPlugin } = this.app.bajo
|
|
5
|
-
const plugin = getPlugin(name)
|
|
6
|
-
const dir = `${this.app.bajo.dir.data}/plugins/${plugin.name}`
|
|
7
|
-
if (ensureDir) fs.ensureDirSync(dir)
|
|
8
|
-
return dir
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default getPluginDataDir
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import breakNsPath from './break-ns-path.js'
|
|
2
|
-
import currentLoc from '../../../lib/current-loc.js'
|
|
3
|
-
import lodash from 'lodash'
|
|
4
|
-
|
|
5
|
-
const { trim } = lodash
|
|
6
|
-
|
|
7
|
-
function getPluginFile (file) {
|
|
8
|
-
if (!this) return file
|
|
9
|
-
if (file[0] === '.') file = `${currentLoc(import.meta).dir}/${trim(file.slice(1), '/')}`
|
|
10
|
-
if (!this) return file
|
|
11
|
-
if (file.includes(':')) {
|
|
12
|
-
if (file.slice(1, 2) === ':') return file // windows fs
|
|
13
|
-
const { ns, path } = breakNsPath.call(this, file)
|
|
14
|
-
if (ns !== 'file' && this && this.app && this.app[ns] && ns.length > 1) {
|
|
15
|
-
file = `${this.app[ns].dir.pkg}${path}`
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return file
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default getPluginFile
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import BajoPlugin from '../../bajo-plugin.js'
|
|
2
|
-
|
|
3
|
-
function getPlugin (name, silent) {
|
|
4
|
-
if (!this.app[name]) {
|
|
5
|
-
// alias?
|
|
6
|
-
let plugin
|
|
7
|
-
for (const key in this.app) {
|
|
8
|
-
const item = this.app[key]
|
|
9
|
-
if (item instanceof BajoPlugin && (item.alias === name || item.pkgName === name)) {
|
|
10
|
-
plugin = item
|
|
11
|
-
break
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
if (!plugin) {
|
|
15
|
-
if (silent) return false
|
|
16
|
-
throw this.error('pluginWithAliasNotLoaded%s', name)
|
|
17
|
-
}
|
|
18
|
-
name = plugin.name
|
|
19
|
-
}
|
|
20
|
-
return this.app[name]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default getPlugin
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import resolvePath from './resolve-path.js'
|
|
2
|
-
import getPluginFile from './get-plugin-file.js'
|
|
3
|
-
import lodash from 'lodash'
|
|
4
|
-
import fs from 'fs-extra'
|
|
5
|
-
|
|
6
|
-
const { isFunction, isPlainObject } = lodash
|
|
7
|
-
|
|
8
|
-
async function load (file, asDefaultImport = true, noCache = false) {
|
|
9
|
-
file = resolvePath(file, true)
|
|
10
|
-
if (noCache) file += `?_=${Date.now()}`
|
|
11
|
-
const imported = await import(file)
|
|
12
|
-
if (asDefaultImport) return imported.default
|
|
13
|
-
return imported
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
|
|
17
|
-
const me = this
|
|
18
|
-
file = getPluginFile.call(me, file)
|
|
19
|
-
if (!fs.existsSync(file)) return
|
|
20
|
-
let mod = await load(file, asDefaultImport, noCache)
|
|
21
|
-
if (!asHandler) return mod
|
|
22
|
-
if (isFunction(mod)) mod = { level: 999, handler: mod }
|
|
23
|
-
if (!isPlainObject(mod)) throw this.error.call(me.app, 'File \'%s\' is NOT a handler module', file)
|
|
24
|
-
return mod
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default importModule
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import os from 'os'
|
|
3
|
-
import getModuleDir from './get-module-dir.js'
|
|
4
|
-
import resolvePath from './resolve-path.js'
|
|
5
|
-
import readJson from './read-json.js'
|
|
6
|
-
import defaultsDeep from './defaults-deep.js'
|
|
7
|
-
import breakNsPath from './break-ns-path.js'
|
|
8
|
-
import path from 'path'
|
|
9
|
-
import fs from 'fs-extra'
|
|
10
|
-
|
|
11
|
-
const { isPlainObject, last, isEmpty, has, keys, values, get } = lodash
|
|
12
|
-
|
|
13
|
-
async function importPkg (...pkgs) {
|
|
14
|
-
const result = {}
|
|
15
|
-
const notFound = []
|
|
16
|
-
let opts = { returnDefault: true, thrownNotFound: false }
|
|
17
|
-
if (isPlainObject(last(pkgs))) {
|
|
18
|
-
opts = defaultsDeep(pkgs.pop(), opts)
|
|
19
|
-
}
|
|
20
|
-
for (const pkg of pkgs) {
|
|
21
|
-
const { ns, path: name } = breakNsPath.call(this, pkg)
|
|
22
|
-
const dir = getModuleDir.call(this, name, ns)
|
|
23
|
-
if (!dir) {
|
|
24
|
-
notFound.push(pkg)
|
|
25
|
-
continue
|
|
26
|
-
}
|
|
27
|
-
const p = readJson(`${dir}/package.json`, opts.thrownNotFound)
|
|
28
|
-
const mainFileOrg = dir + '/' + (p.main ?? get(p, 'exports.default', 'index.js'))
|
|
29
|
-
let mainFile = resolvePath(mainFileOrg, os.platform() === 'win32')
|
|
30
|
-
if (isEmpty(path.extname(mainFile))) {
|
|
31
|
-
if (fs.existsSync(`${mainFileOrg}/index.js`)) mainFile += '/index.js'
|
|
32
|
-
else mainFile += '.js'
|
|
33
|
-
}
|
|
34
|
-
if (opts.noCache) mainFile += `?_=${Date.now()}`
|
|
35
|
-
let mod = await import(mainFile)
|
|
36
|
-
if (opts.returnDefault && has(mod, 'default')) {
|
|
37
|
-
mod = mod.default
|
|
38
|
-
if (opts.returnDefault && has(mod, 'default')) mod = mod.default
|
|
39
|
-
}
|
|
40
|
-
result[name] = mod
|
|
41
|
-
}
|
|
42
|
-
if (notFound.length > 0) throw this.error('cantFind%s', this.join(notFound))
|
|
43
|
-
if (pkgs.length === 1) return result[keys(result)[0]]
|
|
44
|
-
if (opts.asObject) return result
|
|
45
|
-
return values(result)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default importPkg
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import logLevels from './log-levels.js'
|
|
2
|
-
import lodash from 'lodash'
|
|
3
|
-
|
|
4
|
-
const { keys, indexOf } = lodash
|
|
5
|
-
|
|
6
|
-
function isLogInRange (level) {
|
|
7
|
-
const levels = keys(logLevels)
|
|
8
|
-
const logLevel = indexOf(levels, this.app.bajo.config.log.level)
|
|
9
|
-
return indexOf(levels, level) >= logLevel
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isLogInRange
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
import resolvePath from './resolve-path.js'
|
|
3
|
-
|
|
4
|
-
const isValidApp = (dir) => {
|
|
5
|
-
if (!dir) dir = process.env.BAJOCWD
|
|
6
|
-
dir = resolvePath(dir)
|
|
7
|
-
const hasMainDir = fs.existsSync(`${dir}/main/bajo`)
|
|
8
|
-
const hasPackageJson = fs.existsSync(`${dir}/package.json`)
|
|
9
|
-
return hasMainDir && hasPackageJson
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isValidApp
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
import resolvePath from './resolve-path.js'
|
|
3
|
-
|
|
4
|
-
const isValidPlugin = (dir) => {
|
|
5
|
-
if (!dir) dir = process.env.BAJOCWD
|
|
6
|
-
dir = resolvePath(dir)
|
|
7
|
-
const hasBajoDir = fs.existsSync(`${dir}/bajo`)
|
|
8
|
-
const hasPackageJson = fs.existsSync(`${dir}/package.json`)
|
|
9
|
-
return hasBajoDir && hasPackageJson
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isValidPlugin
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import isSet from './is-set.js'
|
|
3
|
-
|
|
4
|
-
const { isPlainObject } = lodash
|
|
5
|
-
|
|
6
|
-
function join (array, sep) {
|
|
7
|
-
const translate = val => {
|
|
8
|
-
if (this && this.print) return this.print.write(val).toLowerCase()
|
|
9
|
-
return val
|
|
10
|
-
}
|
|
11
|
-
if (array.length === 0) return translate('none')
|
|
12
|
-
if (array.length === 1) return array[0]
|
|
13
|
-
if (isSet(sep) && !isPlainObject(sep)) return array.join(sep)
|
|
14
|
-
let { separator = ', ', joiner = 'and' } = sep ?? {}
|
|
15
|
-
joiner = translate(joiner)
|
|
16
|
-
const last = (array.pop() ?? '').trim()
|
|
17
|
-
return array.map(a => (a + '').trim()).join(separator) + ` ${joiner} ${last}`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default join
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
|
|
3
|
-
const { isEmpty } = lodash
|
|
4
|
-
|
|
5
|
-
function numUnit (value = '', defUnit = '') {
|
|
6
|
-
const num = value.match(/\d+/g)
|
|
7
|
-
const unit = value.match(/[a-zA-Z]+/g)
|
|
8
|
-
return `${num[0]}${isEmpty(unit) ? defUnit : unit[0]}`
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default numUnit
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
|
|
3
|
-
const { isEmpty, isPlainObject, orderBy, forOwn } = lodash
|
|
4
|
-
|
|
5
|
-
function paginate (collection, { page = 1, limit = 25, sort } = {}) {
|
|
6
|
-
const count = collection.length
|
|
7
|
-
const offset = (page - 1) * limit
|
|
8
|
-
const fields = []
|
|
9
|
-
const dirs = []
|
|
10
|
-
if (isPlainObject(sort)) {
|
|
11
|
-
forOwn(sort, (v, k) => {
|
|
12
|
-
fields.push(k)
|
|
13
|
-
dirs.push(v < 0 ? 'desc' : 'asc')
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
|
|
17
|
-
const data = collection.slice(offset, offset + limit)
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
data,
|
|
21
|
-
page,
|
|
22
|
-
limit,
|
|
23
|
-
count,
|
|
24
|
-
pages: Math.ceil(collection.length / limit)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export default paginate
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
3
|
-
import ms from 'ms'
|
|
4
|
-
import dayjs from '../../../lib/dayjs.js'
|
|
5
|
-
import isSet from './is-set.js'
|
|
6
|
-
|
|
7
|
-
const { isPlainObject, isArray, isNumber, set, cloneDeep, isString, omit } = lodash
|
|
8
|
-
const statics = ['*']
|
|
9
|
-
|
|
10
|
-
function parseDur (val) {
|
|
11
|
-
return isNumber(val) ? val : ms(val)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function parseDt (val) {
|
|
15
|
-
const dt = dayjs(val)
|
|
16
|
-
if (!dt.isValid()) throw this.error('dtUnparsable%s', val)
|
|
17
|
-
return dt.toDate()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function parseObject (input, { silent = true, parseValue = false, lang, ns } = {}) {
|
|
21
|
-
let obj = cloneDeep(input)
|
|
22
|
-
const keys = Object.keys(obj)
|
|
23
|
-
const me = this
|
|
24
|
-
const mutated = []
|
|
25
|
-
keys.forEach(k => {
|
|
26
|
-
const v = obj[k]
|
|
27
|
-
if (isPlainObject(v)) obj[k] = parseObject(v)
|
|
28
|
-
else if (isArray(v)) {
|
|
29
|
-
v.forEach((i, idx) => {
|
|
30
|
-
if (isPlainObject(i)) obj[k][idx] = parseObject(i)
|
|
31
|
-
else if (statics.includes(i)) obj[k][idx] = i
|
|
32
|
-
else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
|
|
33
|
-
if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
|
|
34
|
-
})
|
|
35
|
-
} else if (isSet(v)) {
|
|
36
|
-
try {
|
|
37
|
-
if (statics.includes(v)) obj[k] = v
|
|
38
|
-
else if (k.startsWith('t:') && isString(v)) {
|
|
39
|
-
const newK = k.slice(2)
|
|
40
|
-
if (lang) {
|
|
41
|
-
const scope = ns ? me.app[ns] : me
|
|
42
|
-
const [text, ...args] = v.split('|')
|
|
43
|
-
obj[newK] = scope.print.write(text, ...args, { lang })
|
|
44
|
-
} else obj[newK] = v
|
|
45
|
-
mutated.push(k)
|
|
46
|
-
} else if (parseValue) {
|
|
47
|
-
obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
|
|
48
|
-
if (isArray(obj[k])) obj[k] = obj[k].map(item => typeof item === 'string' ? item.trim() : item)
|
|
49
|
-
}
|
|
50
|
-
if (k.slice(-3) === 'Dur') obj[k] = parseDur.call(me, v)
|
|
51
|
-
if (k.slice(-2) === 'Dt') obj[k] = parseDt.call(me, v)
|
|
52
|
-
} catch (err) {
|
|
53
|
-
obj[k] = undefined
|
|
54
|
-
if (!silent) throw err
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
})
|
|
58
|
-
if (mutated.length > 0) obj = omit(obj, mutated)
|
|
59
|
-
return obj
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export default parseObject
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import isSet from './is-set.js'
|
|
2
|
-
|
|
3
|
-
function pick (obj, items, excludeUnset) {
|
|
4
|
-
const result = {}
|
|
5
|
-
for (const item of items) {
|
|
6
|
-
const [k, nk] = item.split(':')
|
|
7
|
-
if (excludeUnset && !isSet(obj[k])) continue
|
|
8
|
-
result[nk ?? k] = obj[k]
|
|
9
|
-
}
|
|
10
|
-
return result
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default pick
|