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,4 +1,4 @@
|
|
|
1
|
-
import createMethod from '
|
|
1
|
+
import createMethod from '../../lib/create-method.js'
|
|
2
2
|
|
|
3
3
|
async function attachMethod () {
|
|
4
4
|
const { eachPlugins } = this.bajo
|
|
@@ -6,7 +6,7 @@ async function attachMethod () {
|
|
|
6
6
|
me.bajo.log.debug('attachMethods')
|
|
7
7
|
await eachPlugins(async function ({ ns, pkgName }) {
|
|
8
8
|
const dir = ns === me.bajo.mainNs ? (`${me.bajo.dir.base}/${me.bajo.mainNs}`) : me.bajo.getModuleDir(pkgName)
|
|
9
|
-
const num = await createMethod.call(me[ns], `${dir}/
|
|
9
|
+
const num = await createMethod.call(me[ns], `${dir}/plugin/method`, pkgName)
|
|
10
10
|
me.bajo.log.trace('- %s (%d)', ns, num)
|
|
11
11
|
})
|
|
12
12
|
}
|
|
@@ -3,7 +3,7 @@ async function collectExitHandlers () {
|
|
|
3
3
|
if (!this.bajo.config.exitHandler) return
|
|
4
4
|
const nss = []
|
|
5
5
|
await eachPlugins(async function ({ ns, dir }) {
|
|
6
|
-
const mod = await importModule(`${dir}/
|
|
6
|
+
const mod = await importModule(`${dir}/plugin/exit.js`)
|
|
7
7
|
if (!mod) return undefined
|
|
8
8
|
this.app[ns].exitHandler = mod
|
|
9
9
|
nss.push(ns)
|
|
@@ -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
|
|
@@ -13,13 +12,13 @@ class BajoPlugin extends Plugin {
|
|
|
13
12
|
this.state = {}
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
async
|
|
15
|
+
loadConfig = async () => {
|
|
17
16
|
const { log, getModuleDir, readJson, defaultsDeep } = this.app.bajo
|
|
18
17
|
log.trace('- %s', this.name)
|
|
19
18
|
const dir = this.name === this.app.bajo.mainNs ? (`${this.app.bajo.dir.base}/${this.app.bajo.mainNs}`) : getModuleDir(this.pkgName)
|
|
20
|
-
let cfg = await readAllConfigs.call(this.app, `${dir}/
|
|
21
|
-
this.alias = this.pkgName.slice(0, 5) === 'bajo-' ? this.pkgName.slice(5).toLowerCase() : this.name.toLowerCase()
|
|
22
|
-
const aliasFile = `${dir}/
|
|
19
|
+
let cfg = await readAllConfigs.call(this.app, `${dir}/plugin/config`)
|
|
20
|
+
this.alias = this.alias ?? (this.pkgName.slice(0, 5) === 'bajo-' ? this.pkgName.slice(5).toLowerCase() : this.name.toLowerCase())
|
|
21
|
+
const aliasFile = `${dir}/plugin/.alias`
|
|
23
22
|
if (fs.existsSync(aliasFile)) this.alias = fs.readFileSync(aliasFile, 'utf8')
|
|
24
23
|
this.alias = camelCase(this.alias)
|
|
25
24
|
|
|
@@ -41,19 +40,19 @@ class BajoPlugin extends Plugin {
|
|
|
41
40
|
cfg = defaultsDeep({}, omit(altCfg, omittedPluginKeys), cfg)
|
|
42
41
|
} catch (err) {}
|
|
43
42
|
const envArgv = defaultsDeep({}, omit(this.app.env[this.name] ?? {}, omittedPluginKeys) ?? {}, omit(this.app.argv[this.name] ?? {}, omittedPluginKeys) ?? {})
|
|
44
|
-
cfg = defaultsDeep({}, envArgv ?? {}, cfg ?? {})
|
|
45
|
-
this.title = this.title ?? cfg.title ?? titleize(this.alias)
|
|
43
|
+
cfg = defaultsDeep({}, envArgv ?? {}, cfg ?? {}, this.config ?? {})
|
|
44
|
+
this.title = this.title ?? cfg.title ?? this.app.bajo.titleize(this.alias)
|
|
46
45
|
|
|
47
|
-
this.dependencies = []
|
|
48
|
-
const depFile = `${dir}/
|
|
46
|
+
this.dependencies = this.dependencies ?? []
|
|
47
|
+
const depFile = `${dir}/plugin/.dependencies`
|
|
49
48
|
if (fs.existsSync(depFile)) this.dependencies = without(fs.readFileSync(depFile, 'utf8').split('\n').map(item => trim(item)), '')
|
|
50
49
|
this.config = omit(cfg, ['title', 'dependencies'])
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
async
|
|
52
|
+
_onoff = async (item, ...args) => {
|
|
54
53
|
this.state[item] = false
|
|
55
54
|
const { runHook, importModule } = this.app.bajo
|
|
56
|
-
const mod = await importModule(`${this.dir.pkg}/
|
|
55
|
+
const mod = await importModule(`${this.dir.pkg}/plugin/${item}.js`)
|
|
57
56
|
if (mod) {
|
|
58
57
|
const text = this.print.write('plugin%s', this.print.write(item))
|
|
59
58
|
this.log.trace(text)
|
|
@@ -64,17 +63,17 @@ class BajoPlugin extends Plugin {
|
|
|
64
63
|
this.state[item] = true
|
|
65
64
|
}
|
|
66
65
|
|
|
67
|
-
async
|
|
66
|
+
init = async () => {
|
|
68
67
|
await this._onoff('init')
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
async
|
|
70
|
+
start = async (...args) => {
|
|
72
71
|
const { freeze } = this.app.bajo
|
|
73
72
|
freeze(this.config)
|
|
74
73
|
await this._onoff('start', ...args)
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
async
|
|
76
|
+
stop = async () => {
|
|
78
77
|
await this._onoff('stop')
|
|
79
78
|
}
|
|
80
79
|
}
|
package/boot/class/error.js
CHANGED
|
@@ -12,7 +12,7 @@ class BajoError {
|
|
|
12
12
|
this.write()
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
write (
|
|
15
|
+
write = () => {
|
|
16
16
|
let err
|
|
17
17
|
if (this.payload.class) err = this.payload.class(this.message)
|
|
18
18
|
else err = Error(this.message)
|
|
@@ -31,10 +31,18 @@ class BajoError {
|
|
|
31
31
|
err[key] = value
|
|
32
32
|
}
|
|
33
33
|
if (!isEmpty(values)) err.values = values
|
|
34
|
+
err.ns = this.plugin.name
|
|
35
|
+
err.orgMessage = this.orgMessage
|
|
34
36
|
return err
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
fatal = () => {
|
|
40
|
+
const err = this.write()
|
|
41
|
+
console.error(err)
|
|
42
|
+
process.kill(process.pid, 'SIGINT')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
formatErrorDetails = (value) => {
|
|
38
46
|
const { isString } = this.plugin.app.bajo.lib._
|
|
39
47
|
const result = {}
|
|
40
48
|
const me = this
|
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
|
|
|
@@ -12,26 +11,26 @@ class Log {
|
|
|
12
11
|
this.format = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
init () {
|
|
14
|
+
init = () => {
|
|
16
15
|
this.bajoLog = this.plugin.app.bajo.config.log.logger ?? 'bajoLogger'
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
write (text, ...args) {
|
|
18
|
+
write = (text, ...args) => {
|
|
20
19
|
return this.plugin.print.write(text, ...args)
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
isExtLogger () {
|
|
22
|
+
isExtLogger = () => {
|
|
24
23
|
return this.plugin.app[this.bajoLog] && this.plugin.app[this.bajoLog].logger
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
child () {
|
|
26
|
+
child = () => {
|
|
28
27
|
if (this.isExtLogger()) return this.plugin.app[this.bajoLog].logger.child()
|
|
29
28
|
return this.plugin.app
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
formatMsg (level, ...params) {
|
|
31
|
+
formatMsg = (level, ...params) => {
|
|
33
32
|
if (this.plugin.app.bajo.config.log.level === 'silent') return
|
|
34
|
-
if (!
|
|
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:
|
|
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(
|
|
61
|
+
Object.keys(logLevels).forEach(level => {
|
|
63
62
|
Log.prototype[level] = function (...params) {
|
|
64
63
|
this.formatMsg(level, ...params)
|
|
65
64
|
}
|
package/boot/class/plugin.js
CHANGED
|
@@ -16,7 +16,7 @@ class Plugin {
|
|
|
16
16
|
this.exitHandler = undefined
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
getConfig (path, options = {}) {
|
|
19
|
+
getConfig = (path, options = {}) => {
|
|
20
20
|
let obj = isEmpty(path) ? this.config : get(this.config, path, options.defValue)
|
|
21
21
|
options.omit = options.omit ?? omittedPluginKeys
|
|
22
22
|
if (isPlainObject(obj) && !isEmpty(options.omit)) obj = omit(obj, options.omit)
|
|
@@ -24,30 +24,26 @@ class Plugin {
|
|
|
24
24
|
return obj
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
initLog () {
|
|
27
|
+
initLog = () => {
|
|
28
28
|
this.log = new Log(this)
|
|
29
29
|
this.log.init()
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
initPrint (opts) {
|
|
32
|
+
initPrint = (opts) => {
|
|
33
33
|
this.print = new Print(this, opts)
|
|
34
34
|
this.print.init()
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
error (msg, ...args) {
|
|
37
|
+
error = (msg, ...args) => {
|
|
38
38
|
if (!this.print) return new Error(msg, ...args)
|
|
39
39
|
const error = new BajoError(this, msg, ...args)
|
|
40
40
|
return error.write()
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
fatal (msg, ...args) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
e = error.write(true)
|
|
48
|
-
} else e = new Error(msg, ...args)
|
|
49
|
-
console.error(e)
|
|
50
|
-
process.kill(process.pid, 'SIGINT')
|
|
43
|
+
fatal = (msg, ...args) => {
|
|
44
|
+
if (!this.print) return new Error(msg, ...args)
|
|
45
|
+
const error = new BajoError(this, msg, ...args)
|
|
46
|
+
return error.fatal()
|
|
51
47
|
}
|
|
52
48
|
}
|
|
53
49
|
|
package/boot/class/print.js
CHANGED
|
@@ -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
|
|
@@ -18,7 +17,7 @@ class Print {
|
|
|
18
17
|
this.intl = {}
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
init () {
|
|
20
|
+
init = () => {
|
|
22
21
|
for (const l of this.plugin.app.bajo.config.intl.supported) {
|
|
23
22
|
this.intl[l] = {}
|
|
24
23
|
const path = `${this.plugin.dir.pkg}/bajo/intl/${l}.json`
|
|
@@ -30,7 +29,7 @@ class Print {
|
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
write (text, ...args) {
|
|
32
|
+
write = (text, ...args) => {
|
|
34
33
|
const opts = last(args)
|
|
35
34
|
let lang = this.plugin.app.bajo.config.lang
|
|
36
35
|
if (isPlainObject(opts)) {
|
|
@@ -67,15 +66,15 @@ class Print {
|
|
|
67
66
|
return sprintf(trans, ...params)
|
|
68
67
|
}
|
|
69
68
|
|
|
70
|
-
setOpts (args = []) {
|
|
69
|
+
setOpts = (args = []) => {
|
|
71
70
|
const config = this.plugin.app.bajo.config
|
|
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
|
-
setText (text, ...args) {
|
|
77
|
+
setText = (text, ...args) => {
|
|
79
78
|
text = this.write(text, ...args)
|
|
80
79
|
this.setOpts(args)
|
|
81
80
|
const prefixes = []
|
|
@@ -88,65 +87,65 @@ class Print {
|
|
|
88
87
|
return this
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
getElapsed (unit = 'hms') {
|
|
90
|
+
getElapsed = (unit = 'hms') => {
|
|
92
91
|
const u = unit === 'hms' ? 'second' : unit
|
|
93
92
|
const elapsed = this.plugin.app.bajo.lib.dayjs().diff(this.startTime, u)
|
|
94
93
|
return unit === 'hms' ? this.plugin.app.bajo.secToHms(elapsed) : elapsed
|
|
95
94
|
}
|
|
96
95
|
|
|
97
|
-
start (text, ...args) {
|
|
96
|
+
start = (text, ...args) => {
|
|
98
97
|
this.setOpts(args)
|
|
99
98
|
this.setText(text, ...args)
|
|
100
99
|
this.ora.start()
|
|
101
100
|
return this
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
stop () {
|
|
103
|
+
stop = () => {
|
|
105
104
|
this.ora.stop()
|
|
106
105
|
return this
|
|
107
106
|
}
|
|
108
107
|
|
|
109
|
-
succeed (text, ...args) {
|
|
108
|
+
succeed = (text, ...args) => {
|
|
110
109
|
this.setText(text, ...args)
|
|
111
110
|
this.ora.succeed()
|
|
112
111
|
return this
|
|
113
112
|
}
|
|
114
113
|
|
|
115
|
-
fail (text, ...args) {
|
|
114
|
+
fail = (text, ...args) => {
|
|
116
115
|
this.setText(text, ...args)
|
|
117
116
|
this.ora.fail()
|
|
118
117
|
return this
|
|
119
118
|
}
|
|
120
119
|
|
|
121
|
-
warn (text, ...args) {
|
|
120
|
+
warn = (text, ...args) => {
|
|
122
121
|
this.setText(text, ...args)
|
|
123
122
|
this.ora.warn()
|
|
124
123
|
return this
|
|
125
124
|
}
|
|
126
125
|
|
|
127
|
-
info (text, ...args) {
|
|
126
|
+
info = (text, ...args) => {
|
|
128
127
|
this.setText(text, ...args)
|
|
129
128
|
this.ora.info()
|
|
130
129
|
return this
|
|
131
130
|
}
|
|
132
131
|
|
|
133
|
-
clear () {
|
|
132
|
+
clear = () => {
|
|
134
133
|
this.ora.clear()
|
|
135
134
|
return this
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
render () {
|
|
137
|
+
render = () => {
|
|
139
138
|
this.ora.render()
|
|
140
139
|
return this
|
|
141
140
|
}
|
|
142
141
|
|
|
143
|
-
fatal (text, ...args) {
|
|
142
|
+
fatal = (text, ...args) => {
|
|
144
143
|
this.setText(text, ...args)
|
|
145
144
|
this.ora.fail()
|
|
146
145
|
process.kill(process.pid, 'SIGINT')
|
|
147
146
|
}
|
|
148
147
|
|
|
149
|
-
spinner () {
|
|
148
|
+
spinner = () => {
|
|
150
149
|
return new Print(this.plugin)
|
|
151
150
|
}
|
|
152
151
|
}
|
|
@@ -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 '
|
|
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)) {
|
package/boot/lib/current-loc.js
CHANGED
|
@@ -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 '
|
|
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 (!
|
|
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
|
|
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
|
|
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
|
-
|
|
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,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
|