bajo 1.2.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsdoc.conf.json +6 -3
- package/README.md +8 -148
- package/class/app.js +113 -0
- package/{boot/class/bajo-core.js → class/bajo.js} +449 -37
- package/{boot/class/error.js → class/base/err.js} +29 -3
- package/class/base/log.js +205 -0
- package/class/base/plugin.js +177 -0
- package/class/base/print.js +272 -0
- package/class/helper/bajo.js +344 -0
- package/class/helper/plugin.js +169 -0
- package/{boot/class/bajo-plugin.js → class/plugin.js} +60 -3
- package/docs/App.html +3 -0
- package/docs/Bajo.html +15 -0
- package/docs/BasePlugin.html +5 -0
- package/docs/Err.html +3 -0
- package/docs/Log.html +3 -0
- package/docs/Plugin.html +3 -0
- package/docs/Print.html +3 -0
- package/docs/bitcoin.jpeg +0 -0
- package/docs/class_app.js.html +116 -0
- package/docs/class_bajo.js.html +1100 -0
- package/docs/class_base_err.js.html +99 -0
- package/docs/class_base_log.js.html +208 -0
- package/docs/class_base_plugin.js.html +180 -0
- package/docs/class_base_print.js.html +275 -0
- package/docs/class_helper_bajo.js.html +347 -0
- package/docs/class_helper_plugin.js.html +172 -0
- package/docs/class_plugin.js.html +121 -0
- package/docs/data/search.json +1 -0
- package/docs/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/global.html +3 -0
- package/docs/index.html +3 -0
- package/docs/lib_create-method.js.html +42 -0
- package/docs/lib_formats.js.html +68 -0
- package/docs/lib_log-levels.js.html +28 -0
- package/docs/lib_resolve-path.js.html +30 -0
- package/docs/lib_shim.js.html +35 -0
- package/docs/module-class_helper_bajo.html +3 -0
- package/docs/module-class_helper_plugin.html +3 -0
- package/docs/module-lib_create-method.html +3 -0
- package/docs/module-lib_formats.html +3 -0
- package/docs/module-lib_log-levels.html +3 -0
- package/docs/module-lib_resolve-path.html +3 -0
- package/docs/module-lib_shim.html +3 -0
- package/docs/scripts/core.js +726 -0
- package/docs/scripts/core.min.js +23 -0
- package/docs/scripts/resize.js +90 -0
- package/docs/scripts/search.js +265 -0
- package/docs/scripts/search.min.js +6 -0
- package/docs/scripts/third-party/Apache-License-2.0.txt +202 -0
- package/docs/scripts/third-party/fuse.js +9 -0
- package/docs/scripts/third-party/hljs-line-num-original.js +369 -0
- package/docs/scripts/third-party/hljs-line-num.js +1 -0
- package/docs/scripts/third-party/hljs-original.js +5171 -0
- package/docs/scripts/third-party/hljs.js +1 -0
- package/docs/scripts/third-party/popper.js +5 -0
- package/docs/scripts/third-party/tippy.js +1 -0
- package/docs/scripts/third-party/tocbot.js +672 -0
- package/docs/scripts/third-party/tocbot.min.js +1 -0
- package/docs/styles/clean-jsdoc-theme-base.css +1159 -0
- package/docs/styles/clean-jsdoc-theme-dark.css +412 -0
- package/docs/styles/clean-jsdoc-theme-light.css +482 -0
- package/docs/styles/clean-jsdoc-theme-scrollbar.css +30 -0
- package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +1 -0
- package/docs/styles/clean-jsdoc-theme.min.css +1 -0
- package/docs/tutorial-contribution.html +3 -0
- package/docs/tutorial-ecosystem.html +3 -0
- package/docs/tutorial-getting-started.html +13 -0
- package/docs/tutorial-plugin-dev.html +3 -0
- package/docs/tutorial-user-guide.html +3 -0
- package/lib/create-method.js +39 -0
- package/{boot/lib → lib}/formats.js +28 -0
- package/lib/log-levels.js +25 -0
- package/{boot/lib → lib}/parse-args-argv.js +1 -1
- package/{boot/lib → lib}/resolve-path.js +12 -0
- package/{boot/lib → lib}/shim.js +10 -0
- package/misc-docs/bitcoin.jpeg +0 -0
- package/misc-docs/contribution.md +20 -0
- package/{docs → misc-docs}/ecosystem.md +41 -12
- package/misc-docs/getting-started.md +142 -0
- package/misc-docs/plugin-dev.md +0 -0
- package/misc-docs/toc.json +17 -0
- package/misc-docs/user-guide.md +1 -0
- package/package.json +14 -12
- package/bajoBook/book/doc/.metadata.json +0 -28
- package/bajoBook/book/doc/How-to-Make-a-Paper-Boat-564x400@2x.jpg +0 -0
- package/bajoBook/book/doc/pages/guides/definition.md +0 -7
- package/bajoBook/book/doc/pages/guides/intro.md +0 -3
- package/bajoBook/book/doc/pages/guides/setup.md +0 -22
- package/bajoBook/book/reference/.metadata.json +0 -152
- package/bajoBook/book/reference/concept-leadership-business-with-paper-boats.jpg +0 -0
- package/bajoBook/book/reference/pages/configuration/configuration-file.md +0 -52
- package/bajoBook/book/reference/pages/helper/break-ns-path.md +0 -24
- package/bajoBook/book/reference/pages/helper/build-collections.md +0 -19
- package/bajoBook/book/reference/pages/helper/call-helper-or-handler.md +0 -35
- package/bajoBook/book/reference/pages/helper/current-loc.md +0 -28
- package/bajoBook/book/reference/pages/helper/dayjs.md +0 -13
- package/bajoBook/book/reference/pages/helper/defaults-deep.md +0 -29
- package/bajoBook/book/reference/pages/helper/dump.md +0 -32
- package/bajoBook/book/reference/pages/helper/each-plugins.md +0 -24
- package/bajoBook/book/reference/pages/helper/envs.md +0 -11
- package/bajoBook/book/reference/pages/helper/error.md +0 -29
- package/bajoBook/book/reference/pages/helper/fatal.md +0 -18
- package/bajoBook/book/reference/pages/helper/freeze.md +0 -13
- package/bajoBook/book/reference/pages/helper/generate-id.md +0 -36
- package/bajoBook/book/reference/pages/helper/get-config.md +0 -27
- package/bajoBook/book/reference/pages/helper/get-global-module-dir.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-helper.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-item-by-name.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-key-by-value.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-module-dir.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-plugin-data-dir.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-plugin-name.md +0 -13
- package/bajoBook/book/reference/pages/helper/get-plugin.md +0 -13
- package/bajoBook/book/reference/pages/helper/import-module.md +0 -13
- package/bajoBook/book/reference/pages/helper/import-pkg.md +0 -13
- package/bajoBook/book/reference/pages/helper/is-empty-dir.md +0 -13
- package/bajoBook/book/reference/pages/helper/is-log-in-range.md +0 -13
- package/bajoBook/book/reference/pages/helper/is-set.md +0 -13
- package/bajoBook/book/reference/pages/helper/is-valid-app.md +0 -13
- package/bajoBook/book/reference/pages/helper/is-valid-plugin.md +0 -13
- package/bajoBook/book/reference/pages/helper/log-levels.md +0 -13
- package/bajoBook/book/reference/pages/helper/log.md +0 -13
- package/bajoBook/book/reference/pages/helper/paginate.md +0 -13
- package/bajoBook/book/reference/pages/helper/pascal-case.md +0 -13
- package/bajoBook/book/reference/pages/helper/print.md +0 -13
- package/bajoBook/book/reference/pages/helper/read-config.md +0 -13
- package/bajoBook/book/reference/pages/helper/read-json.md +0 -13
- package/bajoBook/book/reference/pages/helper/resolve-path.md +0 -13
- package/bajoBook/book/reference/pages/helper/resolve-tpl-path.md +0 -13
- package/bajoBook/book/reference/pages/helper/run-hook.md +0 -13
- package/bajoBook/book/reference/pages/helper/save-as-download.md +0 -13
- package/bajoBook/book/reference/pages/helper/titleize.md +0 -13
- package/bajoBook/book/reference/pages/helper/white-space.md +0 -13
- package/boot/class/app.js +0 -67
- package/boot/class/bajo-core/boot-order.js +0 -35
- package/boot/class/bajo-core/boot-plugins.js +0 -17
- package/boot/class/bajo-core/build-config.js +0 -85
- package/boot/class/bajo-core/build-plugins.js +0 -44
- package/boot/class/bajo-core/collect-config-handlers.js +0 -20
- package/boot/class/bajo-core/exit-handler.js +0 -53
- package/boot/class/bajo-core/run-as-applet.js +0 -26
- package/boot/class/bajo-plugin/attach-method.js +0 -14
- package/boot/class/bajo-plugin/build-config.js +0 -15
- package/boot/class/bajo-plugin/check-clash.js +0 -18
- package/boot/class/bajo-plugin/check-dependency.js +0 -39
- package/boot/class/bajo-plugin/collect-hooks.js +0 -31
- package/boot/class/bajo-plugin/run.js +0 -23
- package/boot/class/log.js +0 -90
- package/boot/class/plugin.js +0 -79
- package/boot/class/print.js +0 -153
- package/boot/lib/create-method.js +0 -33
- package/boot/lib/log-levels.js +0 -1
- package/test/method/isSet.js +0 -43
- /package/{bajo → extend/bajo}/intl/en-US.json +0 -0
- /package/{bajo → extend/bajo}/intl/id.json +0 -0
- /package/{waibuStatic → extend/waibuStatic}/virtual.json +0 -0
- /package/{boot/index.js → index.js} +0 -0
- /package/{boot/lib → lib}/current-loc.js +0 -0
- /package/{boot/lib → lib}/dayjs.js +0 -0
- /package/{boot/lib → lib}/import-module.js +0 -0
- /package/{boot/lib → lib}/omitted-plugin-keys.js +0 -0
- /package/{boot/lib → lib}/parse-env.js +0 -0
- /package/{boot/lib → lib}/read-all-configs.js +0 -0
- /package/{docs/hook.md → misc-docs/.hook.md} +0 -0
|
@@ -3,7 +3,16 @@ const { isPlainObject, each, isArray, get, isEmpty, merge } = lodash
|
|
|
3
3
|
|
|
4
4
|
Error.stackTraceLimit = 15
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Bajo error class, a thin wrapper of node's Error object. Use this name instead of Error
|
|
8
|
+
* because, of course, Error is a reserved keyword in node.
|
|
9
|
+
*/
|
|
10
|
+
class Err {
|
|
11
|
+
/**
|
|
12
|
+
* @param {Object} plugin - Plugin instance
|
|
13
|
+
* @param {string} msg - Error message
|
|
14
|
+
* @param {...any} [args] - Variables to interpolate with error message. Payload object can be pushed as the very last argument
|
|
15
|
+
*/
|
|
7
16
|
constructor (plugin, msg, ...args) {
|
|
8
17
|
this.plugin = plugin
|
|
9
18
|
this.payload = args.length > 0 && isPlainObject(args[args.length - 1]) ? args[args.length - 1] : {}
|
|
@@ -12,6 +21,12 @@ class BajoError {
|
|
|
12
21
|
this.write()
|
|
13
22
|
}
|
|
14
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Create the error object
|
|
26
|
+
*
|
|
27
|
+
* @method
|
|
28
|
+
* @returns {Object} Error object
|
|
29
|
+
*/
|
|
15
30
|
write = () => {
|
|
16
31
|
let err
|
|
17
32
|
if (this.payload.class) err = this.payload.class(this.message)
|
|
@@ -36,17 +51,28 @@ class BajoError {
|
|
|
36
51
|
return err
|
|
37
52
|
}
|
|
38
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Print error object on screen and terminate app process
|
|
56
|
+
*
|
|
57
|
+
* @method
|
|
58
|
+
*/
|
|
39
59
|
fatal = () => {
|
|
40
60
|
const err = this.write()
|
|
41
61
|
console.error(err)
|
|
42
62
|
process.kill(process.pid, 'SIGINT')
|
|
43
63
|
}
|
|
44
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Pretty format error details
|
|
67
|
+
*
|
|
68
|
+
* @method
|
|
69
|
+
* @param {Object} value - Value to format
|
|
70
|
+
* @returns {Object}
|
|
71
|
+
*/
|
|
45
72
|
formatErrorDetails = (value) => {
|
|
46
73
|
const { isString } = this.plugin.app.bajo.lib._
|
|
47
74
|
const result = {}
|
|
48
75
|
const me = this
|
|
49
|
-
this.plugin.app.dump(value)
|
|
50
76
|
each(value, (v, i) => {
|
|
51
77
|
const print = me.plugin.print
|
|
52
78
|
if (isString(v)) v = { error: v }
|
|
@@ -67,4 +93,4 @@ class BajoError {
|
|
|
67
93
|
}
|
|
68
94
|
}
|
|
69
95
|
|
|
70
|
-
export default
|
|
96
|
+
export default Err
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import os from 'os'
|
|
2
|
+
import lodash from 'lodash'
|
|
3
|
+
import dayjs from 'dayjs'
|
|
4
|
+
import logLevels from '../../lib/log-levels.js'
|
|
5
|
+
import chalk from 'chalk'
|
|
6
|
+
|
|
7
|
+
const { isEmpty, without, merge } = lodash
|
|
8
|
+
|
|
9
|
+
export function isIgnored (level) {
|
|
10
|
+
const { filter, isArray } = this.lib._
|
|
11
|
+
let ignore = this.app.bajo.config.log.ignore ?? []
|
|
12
|
+
if (!isArray(ignore)) ignore = [ignore]
|
|
13
|
+
const items = filter(ignore, i => {
|
|
14
|
+
const [ns, lvl] = i.split(':')
|
|
15
|
+
if (lvl) return ns === this.name && lvl === level
|
|
16
|
+
return ns === this.name
|
|
17
|
+
})
|
|
18
|
+
return items.length > 0
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A thin logger system.
|
|
23
|
+
*
|
|
24
|
+
* @class
|
|
25
|
+
*/
|
|
26
|
+
class Log {
|
|
27
|
+
/**
|
|
28
|
+
* @param {Object} plugin - Plugin instance
|
|
29
|
+
*/
|
|
30
|
+
constructor (plugin) {
|
|
31
|
+
this.plugin = plugin
|
|
32
|
+
this.format = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Initialize logger. Auto detect to use different logger via Bajo's config file
|
|
37
|
+
*
|
|
38
|
+
* @method
|
|
39
|
+
*/
|
|
40
|
+
init = () => {
|
|
41
|
+
this.bajoLog = this.plugin.app.bajo.config.log.logger ?? 'bajoLogger'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Interpolate and translate text via plugin's print engine. Check Print class
|
|
46
|
+
* for more information
|
|
47
|
+
*
|
|
48
|
+
* @method
|
|
49
|
+
* @param {string} text - Text pattern to use
|
|
50
|
+
* @param {...any} [args] - Variables to interpolate with text pattern above
|
|
51
|
+
* @returns {string} Interpolated & translated text
|
|
52
|
+
*/
|
|
53
|
+
write = (text, ...args) => {
|
|
54
|
+
return this.plugin.print.write(text, ...args)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Do we use external logger or Bajo's built-in one?
|
|
59
|
+
*
|
|
60
|
+
* @method
|
|
61
|
+
* @returns {boolean}
|
|
62
|
+
*/
|
|
63
|
+
isExtLogger = () => {
|
|
64
|
+
return !!(this.plugin.app[this.bajoLog] && this.plugin.app[this.bajoLog].logger)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Is provided level being ignored by config?
|
|
69
|
+
*
|
|
70
|
+
* @method
|
|
71
|
+
* @param {string} level - Log level
|
|
72
|
+
* @returns {boolean}
|
|
73
|
+
*/
|
|
74
|
+
isIgnored = level => {
|
|
75
|
+
return isIgnored.call(this.plugin, level)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Create child logger
|
|
80
|
+
*
|
|
81
|
+
* @method
|
|
82
|
+
* @returns {Object} Child logger instance
|
|
83
|
+
*/
|
|
84
|
+
child = () => {
|
|
85
|
+
if (this.isExtLogger()) return this.plugin.app[this.bajoLog].logger.child()
|
|
86
|
+
return this.plugin.app
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Display & format message according to one of these rules:
|
|
91
|
+
* 1. ```level``` ```text``` ```var 1``` ```var 2``` ```...var n``` - Translate ```text``` and interpolate with ```vars``` for level ```level```
|
|
92
|
+
* 2. ```level``` ```data``` ```text``` ```var 1``` ```var 2``` ```...var n``` - As above, and append stringified ```data```
|
|
93
|
+
* 3. ```level``` ```error``` - Format as **error**. If current log level is _trace_, dump the error object on screen
|
|
94
|
+
*
|
|
95
|
+
* @method
|
|
96
|
+
* @param {string} level - Log level to use
|
|
97
|
+
* @param {...any} params - See format above
|
|
98
|
+
*/
|
|
99
|
+
formatMsg = (level, ...params) => {
|
|
100
|
+
if (this.plugin.app.bajo.config.log.level === 'silent') return
|
|
101
|
+
if (!this.plugin.app.bajo.isLogInRange(level)) return
|
|
102
|
+
const plain = this.plugin.app.bajo.config.log.plain
|
|
103
|
+
let [data, msg, ...args] = params
|
|
104
|
+
if (typeof data === 'string') {
|
|
105
|
+
args.unshift(msg)
|
|
106
|
+
msg = data
|
|
107
|
+
data = null
|
|
108
|
+
}
|
|
109
|
+
args = without(args, undefined)
|
|
110
|
+
if (data instanceof Error) {
|
|
111
|
+
msg = 'error%s'
|
|
112
|
+
args = [data.message]
|
|
113
|
+
}
|
|
114
|
+
msg = this.write(msg, ...args)
|
|
115
|
+
if (this.plugin.app[this.bajoLog] && this.plugin.app[this.bajoLog].logger) {
|
|
116
|
+
this.plugin.app[this.bajoLog].logger[level](data, `[${this.plugin.name}] ${msg}`, ...args)
|
|
117
|
+
} else {
|
|
118
|
+
let text
|
|
119
|
+
const dt = new Date()
|
|
120
|
+
if (this.plugin.app.bajo.config.env === 'prod') {
|
|
121
|
+
const json = { level: logLevels[level].number, time: dt.valueOf(), pid: process.pid, hostname: os.hostname() }
|
|
122
|
+
if (!isEmpty(data)) merge(json, data)
|
|
123
|
+
merge(json, { msg: `[${this.plugin.name}] ${msg}` })
|
|
124
|
+
text = JSON.stringify(json)
|
|
125
|
+
} else {
|
|
126
|
+
const date = dayjs(dt).utc(true).format(this.format)
|
|
127
|
+
const tdate = plain ? `[${date}]` : chalk.cyan(date)
|
|
128
|
+
const tlevel = plain ? level.toUpperCase() : chalk[logLevels[level].color](level.toUpperCase())
|
|
129
|
+
const tplugin = plain ? `[${this.plugin.name}]` : chalk.bgBlue(`${this.plugin.name}`)
|
|
130
|
+
text = `${tdate} ${tlevel}: ${tplugin} ${msg}`
|
|
131
|
+
if (!isEmpty(data)) text += '\n' + JSON.stringify(data)
|
|
132
|
+
}
|
|
133
|
+
if (!this.isIgnored(level)) {
|
|
134
|
+
console.log(text)
|
|
135
|
+
if (data instanceof Error && level === 'trace') console.error(data)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Display & format message as ```trace``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
142
|
+
*
|
|
143
|
+
* @method
|
|
144
|
+
* @param {...any} params
|
|
145
|
+
*/
|
|
146
|
+
trace = (...params) => {
|
|
147
|
+
this.formatMsg('trace', ...params)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Display & format message as ```debug``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
152
|
+
*
|
|
153
|
+
* @method
|
|
154
|
+
* @param {...any} params
|
|
155
|
+
*/
|
|
156
|
+
debug = (...params) => {
|
|
157
|
+
this.formatMsg('debug', ...params)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Display & format message as ```info``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
162
|
+
*
|
|
163
|
+
* @method
|
|
164
|
+
* @param {...any} params
|
|
165
|
+
*/
|
|
166
|
+
info = (...params) => {
|
|
167
|
+
this.formatMsg('info', ...params)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Display & format message as ```warn``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
172
|
+
*
|
|
173
|
+
* @method
|
|
174
|
+
* @param {...any} params
|
|
175
|
+
*/
|
|
176
|
+
warn = (...params) => {
|
|
177
|
+
this.formatMsg('warn', ...params)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Display & format message as ```error``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
182
|
+
*
|
|
183
|
+
* @method
|
|
184
|
+
* @param {...any} params
|
|
185
|
+
*/
|
|
186
|
+
error = (...params) => {
|
|
187
|
+
this.formatMsg('error', ...params)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Display & format message as ```fatal``` level. See {@link Log#formatMsg|formatMsg} for details
|
|
192
|
+
*
|
|
193
|
+
* @method
|
|
194
|
+
* @param {...any} params
|
|
195
|
+
*/
|
|
196
|
+
fatal = (...params) => {
|
|
197
|
+
this.formatMsg('fatal', ...params)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
silent = (...params) => {
|
|
201
|
+
this.formatMsg('silent', ...params)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export default Log
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
import omittedPluginKeys from '../../lib/omitted-plugin-keys.js'
|
|
3
|
+
import Log from './log.js'
|
|
4
|
+
import Print from './print.js'
|
|
5
|
+
import Err from './err.js'
|
|
6
|
+
import fastGlob from 'fast-glob'
|
|
7
|
+
import { sprintf } from 'sprintf-js'
|
|
8
|
+
import outmatch from 'outmatch'
|
|
9
|
+
import dayjs from '../../lib/dayjs.js'
|
|
10
|
+
import fs from 'fs-extra'
|
|
11
|
+
import aneka from 'aneka/index.js'
|
|
12
|
+
|
|
13
|
+
function outmatchNs (source, pattern) {
|
|
14
|
+
const { breakNsPath } = this.app.bajo
|
|
15
|
+
const [src, subSrc] = source.split(':')
|
|
16
|
+
if (!subSrc) return pattern === src
|
|
17
|
+
try {
|
|
18
|
+
const { fullNs, path } = breakNsPath(pattern)
|
|
19
|
+
const isMatch = outmatch(path)
|
|
20
|
+
return src === fullNs && isMatch(subSrc)
|
|
21
|
+
} catch (err) {
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const lib = {
|
|
27
|
+
_: lodash,
|
|
28
|
+
fs,
|
|
29
|
+
fastGlob,
|
|
30
|
+
sprintf,
|
|
31
|
+
outmatch,
|
|
32
|
+
dayjs,
|
|
33
|
+
aneka
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { get, isEmpty, cloneDeep, omit, isPlainObject, camelCase } = lodash
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* This is the base class of bajo's plugin system.
|
|
40
|
+
*
|
|
41
|
+
* @class
|
|
42
|
+
*/
|
|
43
|
+
class BasePlugin {
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} pkgName - Package name (the one you use in package.json)
|
|
46
|
+
* @param {Object} app - App instance reference. Usefull to call app method inside a plugin
|
|
47
|
+
*/
|
|
48
|
+
constructor (pkgName, app) {
|
|
49
|
+
/**
|
|
50
|
+
* Package name, the one from package.json
|
|
51
|
+
* @type {string}
|
|
52
|
+
*/
|
|
53
|
+
this.pkgName = pkgName
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Plugin name. Simply the camel cased version of plugin's package name
|
|
57
|
+
*
|
|
58
|
+
* @type {string}
|
|
59
|
+
*/
|
|
60
|
+
this.name = camelCase(pkgName)
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reference to app instance
|
|
64
|
+
*
|
|
65
|
+
* @type {Object}
|
|
66
|
+
*/
|
|
67
|
+
this.app = app
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Config object
|
|
71
|
+
*
|
|
72
|
+
* @type {Object}
|
|
73
|
+
*/
|
|
74
|
+
this.config = {}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Property to give you direct access to the most commonly used 3rd party library in a bajo based app.
|
|
78
|
+
* No manual import necessary, always available, everywhere, anytime!
|
|
79
|
+
*
|
|
80
|
+
* Example:
|
|
81
|
+
* ```javascript
|
|
82
|
+
* const { camelCase, kebabCase } = this.lib._
|
|
83
|
+
* console.log(camelCase('Elit commodo sit et aliqua'))
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @type {Object}
|
|
87
|
+
* @property {Object} _ - Access to {@link https://lodash.com|lodash}
|
|
88
|
+
* @property {Object} fs - Access to {@link https://github.com/jprichardson/node-fs-extra|fs-extra}
|
|
89
|
+
* @property {Object} fastGlob - Access to {@link https://github.com/mrmlnc/fast-glob|fast-glob}
|
|
90
|
+
* @property {Object} sprintf - Access to {@link https://github.com/alexei/sprintf.js|sprintf}
|
|
91
|
+
* @property {Object} aneka - Access to {@link https://github.com/ardhi/aneka|aneka}
|
|
92
|
+
* @property {Object} outmatch - Access to {@link https://github.com/axtgr/outmatch|outmatch}
|
|
93
|
+
* @property {Object} dayjs - Access to {@link https://day.js.org|dayjs} with utc & customParseFormat plugin already applied
|
|
94
|
+
*/
|
|
95
|
+
this.lib = lib
|
|
96
|
+
this.lib.outmatchNs = outmatchNs.bind(this)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get plugin's config value
|
|
101
|
+
*
|
|
102
|
+
* @method
|
|
103
|
+
* @param {string} [path] - dot separated config path (think of lodash's 'get'). If not provided, the full config will be given
|
|
104
|
+
* @param {Object} [options={}] - Options
|
|
105
|
+
* @param {any} [options.defValue={}] - Default value to use if returned object is undefined
|
|
106
|
+
* @param {string[]} [options.omit=[]] - Omit these keys from returned object
|
|
107
|
+
* @param {boolean} [options.noClone=false] - Set true to NOT clone returned object
|
|
108
|
+
* @returns {Object} Returned object. If no path provided, the whole config object is returned
|
|
109
|
+
*/
|
|
110
|
+
getConfig = (path, options = {}) => {
|
|
111
|
+
let obj = isEmpty(path) ? this.config : get(this.config, path, options.defValue ?? {})
|
|
112
|
+
options.omit = options.omit ?? omittedPluginKeys
|
|
113
|
+
if (isPlainObject(obj) && !isEmpty(options.omit)) obj = omit(obj, options.omit)
|
|
114
|
+
if (!options.noClone) obj = cloneDeep(obj)
|
|
115
|
+
return obj
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Initialize log. Please refer to {@link Log} class for more info
|
|
120
|
+
*
|
|
121
|
+
* @method
|
|
122
|
+
*/
|
|
123
|
+
initLog = () => {
|
|
124
|
+
this.log = new Log(this)
|
|
125
|
+
this.log.init()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Initialize print engine. Please refer to {@link Print} class for more info
|
|
130
|
+
*
|
|
131
|
+
* @method
|
|
132
|
+
* @param {Object} [options] - Print options
|
|
133
|
+
*/
|
|
134
|
+
initPrint = (options) => {
|
|
135
|
+
this.print = new Print(this, options)
|
|
136
|
+
this.print.init()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Create an instance of {@link Err} object
|
|
141
|
+
*
|
|
142
|
+
* @method
|
|
143
|
+
* @param {msg} msg - Error message
|
|
144
|
+
* @param {...any} [args] - Argument variables you might want to add to the error object
|
|
145
|
+
* @returns {Object} Err instance
|
|
146
|
+
*/
|
|
147
|
+
err = (msg, ...args) => {
|
|
148
|
+
if (!this.print) return new Error(msg, ...args)
|
|
149
|
+
const error = new Err(this, msg, ...args)
|
|
150
|
+
return error.write()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Alias to err () method above
|
|
155
|
+
*
|
|
156
|
+
* @method
|
|
157
|
+
*/
|
|
158
|
+
error = (msg, ...args) => {
|
|
159
|
+
return this.error(msg, ...args)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Create an instance of Err object, display it on screen and then force
|
|
164
|
+
* terminate the app process
|
|
165
|
+
*
|
|
166
|
+
* @method
|
|
167
|
+
* @param {msg} msg - Error message
|
|
168
|
+
* @param {...any} [args] - Argument variables you might want to add to the error object
|
|
169
|
+
*/
|
|
170
|
+
fatal = (msg, ...args) => {
|
|
171
|
+
if (!this.print) return new Error(msg, ...args)
|
|
172
|
+
const error = new Err(this, msg, ...args)
|
|
173
|
+
error.fatal()
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export default BasePlugin
|