bajo 2.17.0 → 2.19.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/class/_helper.js +22 -7
- package/class/app.js +148 -35
- package/class/bajo.js +156 -206
- package/class/base.js +3 -3
- package/class/cache.js +61 -2
- package/class/err.js +14 -11
- package/class/log.js +41 -40
- package/class/plugin.js +35 -36
- package/class/print.js +54 -51
- package/class/tools.js +3 -4
- package/docs/App.html +7 -7
- package/docs/Bajo.html +2 -2
- package/docs/Base.html +1 -1
- package/docs/Cache.html +3 -0
- package/docs/Err.html +2 -2
- package/docs/Log.html +2 -2
- package/docs/Plugin.html +1 -1
- package/docs/Print.html +1 -1
- package/docs/Tools.html +3 -0
- package/docs/class__helper.js.html +694 -0
- package/docs/class_app.js.html +307 -149
- package/docs/class_bajo.js.html +316 -464
- package/docs/class_base.js.html +35 -32
- package/docs/class_cache.js.html +150 -0
- package/docs/class_err.js.html +144 -0
- package/docs/class_log.js.html +270 -0
- package/docs/class_plugin.js.html +98 -71
- package/docs/class_print.js.html +261 -0
- package/docs/class_tools.js.html +44 -0
- package/docs/data/search.json +1 -1
- package/docs/global.html +1 -4
- package/docs/index.html +1 -1
- package/docs/index.js.html +21 -14
- package/docs/lib_find-deep.js.html +27 -0
- package/docs/lib_formats.js.html +19 -19
- package/docs/lib_freeze.js.html +19 -0
- package/docs/lib_import-module.js.html +16 -14
- package/docs/lib_index.js.html +9 -0
- package/docs/lib_log-levels.js.html +2 -2
- package/docs/module-Helper.html +3 -0
- package/docs/module-Lib.html +3 -8
- package/docs/scripts/core.js +477 -476
- package/docs/scripts/resize.js +36 -36
- package/docs/scripts/search.js +105 -105
- package/docs/scripts/third-party/fuse.js +1 -1
- package/docs/scripts/third-party/hljs-line-num-original.js +285 -282
- package/docs/scripts/third-party/hljs-line-num.js +1 -1
- package/docs/scripts/third-party/hljs-original.js +1202 -1195
- package/docs/scripts/third-party/hljs.js +1 -1
- package/docs/scripts/third-party/popper.js +1 -1
- package/docs/scripts/third-party/tippy.js +1 -1
- package/docs/scripts/third-party/tocbot.js +509 -508
- package/index.js +8 -11
- package/lib/find-deep.js +3 -3
- package/lib/formats.js +17 -17
- package/lib/freeze.js +3 -3
- package/lib/import-module.js +9 -9
- package/package.json +1 -1
- package/test/app.test.js +183 -0
- package/test/bajo.test.js +125 -0
- package/test/base.test.js +74 -107
- package/test/cache.test.js +94 -0
- package/test/e2e.test.js +137 -0
- package/test/err.test.js +73 -0
- package/test/helper.test.js +39 -0
- package/test/import-module.test.js +138 -0
- package/test/integration.test.js +218 -0
- package/test/log.test.js +119 -0
- package/test/plugin.test.js +116 -0
- package/test/print.test.js +100 -0
- package/test/tools.test.js +38 -0
- package/wiki/CHANGES.md +10 -0
- package/.mocharc.json +0 -4
package/class/bajo.js
CHANGED
|
@@ -33,7 +33,25 @@ const {
|
|
|
33
33
|
last, get, has, values, dropRight, pick
|
|
34
34
|
} = lodash
|
|
35
35
|
|
|
36
|
-
const { resolvePath
|
|
36
|
+
const { resolvePath } = aneka
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Name based ```{ns}:{path}``` format.
|
|
40
|
+
*
|
|
41
|
+
* @typedef {string} TNsPathPairs
|
|
42
|
+
* @see TNsPathResult
|
|
43
|
+
* @see Bajo#buildNsPath
|
|
44
|
+
* @see Bajo#breakNsPath
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Object returned by {@link Bajo#getUnitFormat|bajo:getUnitFormat}.
|
|
49
|
+
*
|
|
50
|
+
* @typedef {Object} TBajoFormatResult
|
|
51
|
+
* @property {string} unitSys - Unit system.
|
|
52
|
+
* @property {Object} format - Format object.
|
|
53
|
+
* @see Bajo#getUnitFormat
|
|
54
|
+
*/
|
|
37
55
|
|
|
38
56
|
/**
|
|
39
57
|
* The Core. The main engine. The one and only plugin that control app's boot process and
|
|
@@ -43,20 +61,21 @@ const { resolvePath, currentLoc } = aneka
|
|
|
43
61
|
*/
|
|
44
62
|
class Bajo extends Plugin {
|
|
45
63
|
/**
|
|
46
|
-
* @param {App} app - App instance. Usefull to call app method inside a plugin
|
|
64
|
+
* @param {App} app - App instance. Usefull to call app method inside a plugin.
|
|
47
65
|
*/
|
|
48
66
|
constructor (app) {
|
|
49
67
|
super('bajo', app)
|
|
50
68
|
this.alias = 'bajo'
|
|
51
69
|
this.whiteSpace = [' ', '\t', '\n', '\r']
|
|
52
70
|
/**
|
|
53
|
-
* Config object
|
|
71
|
+
* Config object.
|
|
54
72
|
*
|
|
55
73
|
* @type {Object}
|
|
56
74
|
* @see {@tutorial config}
|
|
57
75
|
*/
|
|
58
76
|
this.config = {}
|
|
59
77
|
|
|
78
|
+
// by defaualt, only these config formats below are supported.
|
|
60
79
|
app.configHandlers = [
|
|
61
80
|
{ ext: '.js', readHandler: this.fromJs },
|
|
62
81
|
{ ext: '.json', readHandler: this.fromJson, writeHandler: this.toJson }
|
|
@@ -113,23 +132,15 @@ class Bajo extends Plugin {
|
|
|
113
132
|
}
|
|
114
133
|
|
|
115
134
|
/**
|
|
116
|
-
*
|
|
117
|
-
* @typedef {string} TNsPathPairs
|
|
118
|
-
* @see TNsPathResult
|
|
119
|
-
* @see Bajo#buildNsPath
|
|
120
|
-
* @see Bajo#breakNsPath
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Build ns/path pairs
|
|
135
|
+
* Build ns/path pairs.
|
|
125
136
|
*
|
|
126
137
|
* @method
|
|
127
|
-
* @param {object} [options={}] - Options object
|
|
128
|
-
* @param {string} [options.ns=''] - Namespace
|
|
129
|
-
* @param {string} [options.subNs] - Sub namespace
|
|
130
|
-
* @param {string} [options.subSubNs] - Sub sub namespace
|
|
131
|
-
* @param {string} [options.path] - Path
|
|
132
|
-
* @returns {TNsPathPairs}
|
|
138
|
+
* @param {object} [options={}] - Options object.
|
|
139
|
+
* @param {string} [options.ns=''] - Namespace.
|
|
140
|
+
* @param {string} [options.subNs] - Sub namespace.
|
|
141
|
+
* @param {string} [options.subSubNs] - Sub sub namespace.
|
|
142
|
+
* @param {string} [options.path] - Path.
|
|
143
|
+
* @returns {TNsPathPairs} Ns/path pairs.
|
|
133
144
|
*/
|
|
134
145
|
buildNsPath = ({ ns = '', subNs, subSubNs, path } = {}) => {
|
|
135
146
|
if (subNs) ns += '.' + subNs
|
|
@@ -138,21 +149,21 @@ class Bajo extends Plugin {
|
|
|
138
149
|
}
|
|
139
150
|
|
|
140
151
|
/**
|
|
141
|
-
* Object returned by {@link Bajo#breakNsPath|bajo:breakNsPath}
|
|
152
|
+
* Object returned by {@link Bajo#breakNsPath|bajo:breakNsPath}.
|
|
142
153
|
*
|
|
143
154
|
* @typedef {Object} TNsPathResult
|
|
144
|
-
* @property {string} ns - Namespace
|
|
145
|
-
* @property {string} [subNs] - Sub namespace
|
|
146
|
-
* @property {string} [subSubNs] - Sub of sub namespace
|
|
147
|
-
* @property {string} path - Path without query string or hash
|
|
148
|
-
* @property {string} fullPath - Full path, including query string and hash
|
|
155
|
+
* @property {string} ns - Namespace.
|
|
156
|
+
* @property {string} [subNs] - Sub namespace.
|
|
157
|
+
* @property {string} [subSubNs] - Sub of sub namespace.
|
|
158
|
+
* @property {string} path - Path without query string or hash.
|
|
159
|
+
* @property {string} fullPath - Full path, including query string and hash.
|
|
149
160
|
* @see TNsPathPairs
|
|
150
161
|
* @see Bajo#buildNsPath
|
|
151
162
|
* @see Bajo#breakNsPath
|
|
152
163
|
*/
|
|
153
164
|
|
|
154
165
|
/**
|
|
155
|
-
* Break name to its namespace & path infos
|
|
166
|
+
* Break name to its namespace & path infos.
|
|
156
167
|
*
|
|
157
168
|
* @method
|
|
158
169
|
* @param {(TNsPathPairs|string)} name - Name to break
|
|
@@ -172,7 +183,7 @@ class Bajo extends Plugin {
|
|
|
172
183
|
[ns, subNs, subSubNs] = ns.split('.')
|
|
173
184
|
if (checkNs) {
|
|
174
185
|
if (!this.app[ns]) {
|
|
175
|
-
const plugin = this.getPlugin(ns)
|
|
186
|
+
const plugin = this.app.getPlugin(ns)
|
|
176
187
|
if (plugin) ns = plugin.ns
|
|
177
188
|
}
|
|
178
189
|
if (!this.app[ns]) throw this.error('unknownPluginOrNotLoaded%s')
|
|
@@ -209,12 +220,12 @@ class Bajo extends Plugin {
|
|
|
209
220
|
*
|
|
210
221
|
* @method
|
|
211
222
|
* @async
|
|
212
|
-
* @param {Object} options - Options
|
|
223
|
+
* @param {Object} options - Options.
|
|
213
224
|
* @param {string} [options.ns] - Namespace. If not provided, defaults to ```bajo```
|
|
214
|
-
* @param {function} [options.handler] - Handler to call while building the collection item
|
|
215
|
-
* @param {string[]} [options.dupChecks=[]] - Array of keys to check for duplicates
|
|
216
|
-
* @param {string} options.container - Key used as container name
|
|
217
|
-
* @param {boolean} [options.useDefaultName=true] - If true (default) and ```name``` key is not provided, returned collection will be named ```default
|
|
225
|
+
* @param {function} [options.handler] - Handler to call while building the collection item.
|
|
226
|
+
* @param {string[]} [options.dupChecks=[]] - Array of keys to check for duplicates.
|
|
227
|
+
* @param {string} options.container - Key used as container name.
|
|
228
|
+
* @param {boolean} [options.useDefaultName=true] - If true (default) and ```name``` key is not provided, returned collection will be named ```default```.
|
|
218
229
|
* @fires bajo:beforeBuildCollection
|
|
219
230
|
* @fires bajo:afterBuildCollection
|
|
220
231
|
* @returns {Object[]} The collection
|
|
@@ -229,7 +240,7 @@ class Bajo extends Plugin {
|
|
|
229
240
|
this.app[ns].log.trace('collecting%s', this.t(container))
|
|
230
241
|
|
|
231
242
|
/**
|
|
232
|
-
* Run before collection is built
|
|
243
|
+
* Run before collection is built.
|
|
233
244
|
*
|
|
234
245
|
* @global
|
|
235
246
|
* @event bajo:beforeBuildCollection
|
|
@@ -291,9 +302,9 @@ class Bajo extends Plugin {
|
|
|
291
302
|
*
|
|
292
303
|
* @method
|
|
293
304
|
* @async
|
|
294
|
-
* @param {(TNsPathPairs|Object|function)} name - Method's name, function handler, plain object or plugin instance
|
|
295
|
-
* @param {...any} [args] - One or more arguments passed as parameter to the handler
|
|
296
|
-
* @returns {any} Returned value
|
|
305
|
+
* @param {(TNsPathPairs|Object|function)} name - Method's name, function handler, plain object or plugin instance.
|
|
306
|
+
* @param {...any} [args] - One or more arguments passed as parameter to the handler.
|
|
307
|
+
* @returns {any} Returned value.
|
|
297
308
|
*/
|
|
298
309
|
callHandler = async (item, ...args) => {
|
|
299
310
|
let result
|
|
@@ -332,13 +343,13 @@ class Bajo extends Plugin {
|
|
|
332
343
|
*
|
|
333
344
|
* @method
|
|
334
345
|
* @async
|
|
335
|
-
* @param {function} handler - Function handler. Can be an async function. Scoped to the running plugin
|
|
346
|
+
* @param {function} handler - Function handler. Can be an async function. Scoped to the running plugin.
|
|
336
347
|
* @param {(string|Object)} [options={}] - Options. If a string is provided, it serves as the glob pattern, otherwise:
|
|
337
348
|
* @param {(string|string[])} [options.glob] - Glob pattern. If provided,
|
|
338
|
-
* @param {boolean} [options.useBajo=false] - If true, add ```bajo``` to the running plugins too
|
|
339
|
-
* @param {string} [options.prefix=''] - Prepend glob pattern with prefix
|
|
340
|
-
* @param {boolean} [options.noUnderscore=true] - If true (default), matched file with name starts with underscore is ignored
|
|
341
|
-
* @param {any} [options.returnItems] - If true, each value of returned handler call will be saved as an object with running plugin name as its keys
|
|
349
|
+
* @param {boolean} [options.useBajo=false] - If true, add ```bajo``` to the running plugins too.
|
|
350
|
+
* @param {string} [options.prefix=''] - Prepend glob pattern with prefix.
|
|
351
|
+
* @param {boolean} [options.noUnderscore=true] - If true (default), matched file with name starts with underscore is ignored.
|
|
352
|
+
* @param {any} [options.returnItems] - If true, each value of returned handler call will be saved as an object with running plugin name as its keys.
|
|
342
353
|
* @returns {any}
|
|
343
354
|
*/
|
|
344
355
|
eachPlugins = async (handler, options = {}) => {
|
|
@@ -386,22 +397,13 @@ class Bajo extends Plugin {
|
|
|
386
397
|
}
|
|
387
398
|
|
|
388
399
|
/**
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
* @typedef {Object} TBajoFormatResult
|
|
392
|
-
* @property {string} unitSys - Unit system
|
|
393
|
-
* @property {Object} format - Format object
|
|
394
|
-
* @see Bajo#getUnitFormat
|
|
395
|
-
*/
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Get unit format
|
|
400
|
+
* Get unit format.
|
|
399
401
|
*
|
|
400
402
|
* @method
|
|
401
|
-
* @param {Object} [options={}] - Options
|
|
402
|
-
* @param {string} [options.lang] - Language to use. Defaults to the one you set in config
|
|
403
|
-
* @param {string} [options.unitSys] - Unit system to use. Defaults to language's unit system or ```metric``` if unspecified
|
|
404
|
-
* @returns {TBajoFormatResult}
|
|
403
|
+
* @param {Object} [options={}] - Options.
|
|
404
|
+
* @param {string} [options.lang] - Language to use. Defaults to the one you set in config.
|
|
405
|
+
* @param {string} [options.unitSys] - Unit system to use. Defaults to language's unit system or ```metric``` if unspecified.
|
|
406
|
+
* @returns {TBajoFormatResult} Returned value.
|
|
405
407
|
*/
|
|
406
408
|
getUnitFormat = (options = {}) => {
|
|
407
409
|
const lang = options.lang ?? this.config.lang
|
|
@@ -411,16 +413,16 @@ class Bajo extends Plugin {
|
|
|
411
413
|
}
|
|
412
414
|
|
|
413
415
|
/**
|
|
414
|
-
* Format value by type
|
|
416
|
+
* Format value by type.
|
|
415
417
|
*
|
|
416
418
|
* @method
|
|
417
|
-
* @param {string} type - Format type. See {@link TBajoFormatType} for acceptable values
|
|
418
|
-
* @param {any} value - Value to format
|
|
419
|
-
* @param {string} [dataType] - Value's data type. See {@link TBajoDataType} for acceptable values
|
|
420
|
-
* @param {Object} [options={}] - Options
|
|
421
|
-
* @param {boolean} [options.withUnit=true] - Return with its unit appended
|
|
422
|
-
* @param {string} [options.lang] - Format value according to this language. Defaults to the one you set in config
|
|
423
|
-
* @returns {(Array|string)} Return string if ```withUnit``` is true. Otherwise is an array of ```[value, unit, separator]
|
|
419
|
+
* @param {string} type - Format type. See {@link TBajoFormatType} for acceptable values.
|
|
420
|
+
* @param {any} value - Value to format.
|
|
421
|
+
* @param {string} [dataType] - Value's data type. See {@link TBajoDataType} for acceptable values.
|
|
422
|
+
* @param {Object} [options={}] - Options.
|
|
423
|
+
* @param {boolean} [options.withUnit=true] - Return with its unit appended.
|
|
424
|
+
* @param {string} [options.lang] - Format value according to this language. Defaults to the one you set in config.
|
|
425
|
+
* @returns {(Array|string)} Return string if ```withUnit``` is true. Otherwise is an array of ```[value, unit, separator]```.
|
|
424
426
|
*/
|
|
425
427
|
formatByType = (type, value, dataType, options = {}) => {
|
|
426
428
|
const { defaultsDeep } = this.app.lib.aneka
|
|
@@ -437,18 +439,18 @@ class Bajo extends Plugin {
|
|
|
437
439
|
}
|
|
438
440
|
|
|
439
441
|
/**
|
|
440
|
-
* Format value
|
|
442
|
+
* Format value.
|
|
441
443
|
*
|
|
442
444
|
* @method
|
|
443
|
-
* @param {any} value - Value to format
|
|
444
|
-
* @param {string} [type] - Data type to use. See {@link TBajoDataType} for acceptable values. If not provided, return the untouched value
|
|
445
|
-
* @param {Object} [options={}] - Options
|
|
446
|
-
* @param {string} [options.emptyValue=''] - Empty value to use if function resulted empty. Defaults to the one from your config
|
|
447
|
-
* @param {boolean} [options.withUnit=true] - Return with its unit appended
|
|
448
|
-
* @param {string} [options.lang] - Format value according to this language. Defaults to the one you set in config
|
|
449
|
-
* @param {string} [options.latitude] - If Bajo Spatial is loaded and data type is a double or float, then format it as latitude in degree, minute, second
|
|
450
|
-
* @param {string} [options.longitude] - If Bajo Spatial is loaded and data type is a double or float, then format it as longitude in degree, minute, second
|
|
451
|
-
* @returns {string} Formatted value
|
|
445
|
+
* @param {any} value - Value to format.
|
|
446
|
+
* @param {string} [type] - Data type to use. See {@link TBajoDataType} for acceptable values. If not provided, return the untouched value.
|
|
447
|
+
* @param {Object} [options={}] - Options.
|
|
448
|
+
* @param {string} [options.emptyValue=''] - Empty value to use if function resulted empty. Defaults to the one from your config.
|
|
449
|
+
* @param {boolean} [options.withUnit=true] - Return with its unit appended.
|
|
450
|
+
* @param {string} [options.lang] - Format value according to this language. Defaults to the one you set in config.
|
|
451
|
+
* @param {string} [options.latitude] - If Bajo Spatial is loaded and data type is a double or float, then format it as latitude in degree, minute, second.
|
|
452
|
+
* @param {string} [options.longitude] - If Bajo Spatial is loaded and data type is a double or float, then format it as longitude in degree, minute, second.
|
|
453
|
+
* @returns {string} Formatted value.
|
|
452
454
|
*/
|
|
453
455
|
format = (value, type, options = {}) => {
|
|
454
456
|
const { defaultsDeep, isSet } = this.app.lib.aneka
|
|
@@ -498,19 +500,11 @@ class Bajo extends Plugin {
|
|
|
498
500
|
}
|
|
499
501
|
|
|
500
502
|
/**
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
* @param {string} text - Text to be formatted
|
|
504
|
-
* @param {...any} args - Argumennts
|
|
505
|
-
* @returns {string} Formatted text
|
|
506
|
-
*/
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* Get NPM global module directory
|
|
503
|
+
* Get NPM global module directory.
|
|
510
504
|
*
|
|
511
505
|
* @method
|
|
512
|
-
* @param {string} [pkgName] - If provided, return this package global directory. Otherwise the npm global module directory
|
|
513
|
-
* @param {boolean} [silent=true] - Set to ```false``` to throw exception in case of error. Otherwise silently returns undefined
|
|
506
|
+
* @param {string} [pkgName] - If provided, return this package global directory. Otherwise the npm global module directory.
|
|
507
|
+
* @param {boolean} [silent=true] - Set to ```false``` to throw exception in case of error. Otherwise silently returns undefined.
|
|
514
508
|
* @returns {string}
|
|
515
509
|
*/
|
|
516
510
|
getGlobalModuleDir = (pkgName, silent = true) => {
|
|
@@ -534,12 +528,12 @@ class Bajo extends Plugin {
|
|
|
534
528
|
}
|
|
535
529
|
|
|
536
530
|
/**
|
|
537
|
-
* Get class method by name
|
|
531
|
+
* Get class method by name.
|
|
538
532
|
*
|
|
539
533
|
* @method
|
|
540
|
-
* @param {string} name - Name in format ```ns:methodName
|
|
541
|
-
* @param {boolean} [thrown=true] - If ```true``` (default), throw
|
|
542
|
-
* @returns {function} Class method
|
|
534
|
+
* @param {string} name - Name in format ```ns:methodName```.
|
|
535
|
+
* @param {boolean} [thrown=true] - If ```true``` (default), throw exception in case of error.
|
|
536
|
+
* @returns {function} Class method.
|
|
543
537
|
*/
|
|
544
538
|
getMethod = (name = '', thrown = true) => {
|
|
545
539
|
const { ns, path } = this.breakNsPath(name, thrown)
|
|
@@ -549,12 +543,12 @@ class Bajo extends Plugin {
|
|
|
549
543
|
}
|
|
550
544
|
|
|
551
545
|
/**
|
|
552
|
-
* Get module directory, locally and globally
|
|
546
|
+
* Get module directory, locally and globally.
|
|
553
547
|
*
|
|
554
548
|
* @method
|
|
555
|
-
* @param {string} pkgName - Package name to find
|
|
556
|
-
* @param {string} base - Provide base name if ```pkgName``` is a module under ```base```'s package name
|
|
557
|
-
* @returns {string} Return absolute package directory
|
|
549
|
+
* @param {string} pkgName - Package name to find.
|
|
550
|
+
* @param {string} base - Provide base name if ```pkgName``` is a module under ```base```'s package name.
|
|
551
|
+
* @returns {string} Return absolute package directory.
|
|
558
552
|
*/
|
|
559
553
|
getModuleDir = (pkgName, base) => {
|
|
560
554
|
const { findDeep } = this.app.lib
|
|
@@ -572,77 +566,6 @@ class Bajo extends Plugin {
|
|
|
572
566
|
return resolvePath(path.dirname(dir))
|
|
573
567
|
}
|
|
574
568
|
|
|
575
|
-
/**
|
|
576
|
-
* Get plugin data directory
|
|
577
|
-
*
|
|
578
|
-
* @method
|
|
579
|
-
* @param {string} name - Plugin name (namespace) or alias
|
|
580
|
-
* @param {boolean} [ensureDir=true] - Set ```true``` (default) to ensure directory is existed
|
|
581
|
-
* @returns {string}
|
|
582
|
-
*/
|
|
583
|
-
getPluginDataDir = (name, ensureDir = true) => {
|
|
584
|
-
const plugin = this.getPlugin(name)
|
|
585
|
-
const dir = `${this.app.bajo.dir.data}/plugins/${plugin.ns}`
|
|
586
|
-
if (ensureDir) fs.ensureDirSync(dir)
|
|
587
|
-
return dir
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
/**
|
|
591
|
-
* Resolve file path from:
|
|
592
|
-
*
|
|
593
|
-
* - local/absolute file
|
|
594
|
-
* - TNsPath (```myPlugin:/path/to/file.txt```)
|
|
595
|
-
* - file under node_modules, e.g. ```myPlugin:node_modules/some-package/file.txt```
|
|
596
|
-
*
|
|
597
|
-
* @method
|
|
598
|
-
* @param {string} file - File path, see above for supported types
|
|
599
|
-
* @returns {string} Resolved file path
|
|
600
|
-
*/
|
|
601
|
-
getPluginFile = (file) => {
|
|
602
|
-
if (!this) return file
|
|
603
|
-
if (file[0] === '.') file = `${currentLoc(import.meta).dir}/${trim(file.slice(1), '/')}`
|
|
604
|
-
if (file.includes(':')) {
|
|
605
|
-
if (file.slice(1, 2) === ':') return file // windows fs
|
|
606
|
-
const { ns, path } = this.breakNsPath(file)
|
|
607
|
-
if (ns !== 'file' && this && this.app && this.app[ns] && ns.length > 1) {
|
|
608
|
-
file = `${this.app[ns].dir.pkg}${path}`
|
|
609
|
-
if (path.startsWith('node_modules/')) {
|
|
610
|
-
file = `${this.app[ns].dir.pkg}/${path}`
|
|
611
|
-
if (!fs.existsSync(file)) file = `${this.app[ns].dir.pkg}/../${path.slice('node_modules/'.length)}`
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
return file
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
/**
|
|
619
|
-
* Get plugin by name
|
|
620
|
-
*
|
|
621
|
-
* @method
|
|
622
|
-
* @param {string} name - Plugin name/namespace or alias
|
|
623
|
-
* @param {boolean} [silent] - If ```true```, silently return undefined even on error
|
|
624
|
-
* @returns {Object} Plugin object
|
|
625
|
-
*/
|
|
626
|
-
getPlugin = (name, silent) => {
|
|
627
|
-
if (!this.app[name]) {
|
|
628
|
-
// alias?
|
|
629
|
-
let plugin
|
|
630
|
-
for (const key in this.app) {
|
|
631
|
-
const item = this.app[key]
|
|
632
|
-
if (item instanceof Plugin && (item.alias === name || item.pkgName === name)) {
|
|
633
|
-
plugin = item
|
|
634
|
-
break
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
if (!plugin) {
|
|
638
|
-
if (silent) return false
|
|
639
|
-
throw this.error('pluginWithNameAliasNotLoaded%s', name)
|
|
640
|
-
}
|
|
641
|
-
name = plugin.ns
|
|
642
|
-
}
|
|
643
|
-
return this.app[name]
|
|
644
|
-
}
|
|
645
|
-
|
|
646
569
|
/**
|
|
647
570
|
* Import file/module from any loaded plugins.
|
|
648
571
|
*
|
|
@@ -680,8 +603,8 @@ class Bajo extends Plugin {
|
|
|
680
603
|
*
|
|
681
604
|
* @method
|
|
682
605
|
* @async
|
|
683
|
-
* @param {...TNsPathPairs} pkgs - One or more packages in format ```{ns}:{packageName}
|
|
684
|
-
* @returns {(Object|Array)} See above
|
|
606
|
+
* @param {...TNsPathPairs} pkgs - One or more packages in format ```{ns}:{packageName}```.
|
|
607
|
+
* @returns {(Object|Array)} See above.
|
|
685
608
|
*/
|
|
686
609
|
importPkg = async (...pkgs) => {
|
|
687
610
|
const { defaultsDeep } = this.app.lib.aneka
|
|
@@ -725,21 +648,21 @@ class Bajo extends Plugin {
|
|
|
725
648
|
*
|
|
726
649
|
* @method
|
|
727
650
|
* @async
|
|
728
|
-
* @param {(string|TNsPathPairs)} dir - Directory to check
|
|
651
|
+
* @param {(string|TNsPathPairs)} dir - Directory to check.
|
|
729
652
|
* @param {function} filterFn - Filter function to filter out files that cause false positives.
|
|
730
653
|
* @returns {boolean}
|
|
731
654
|
*/
|
|
732
655
|
isEmptyDir = async (dir, filterFn) => {
|
|
733
|
-
dir = resolvePath(this.getPluginFile(dir))
|
|
656
|
+
dir = resolvePath(this.app.getPluginFile(dir))
|
|
734
657
|
await fs.exists(dir)
|
|
735
658
|
return await emptyDir(dir, filterFn)
|
|
736
659
|
}
|
|
737
660
|
|
|
738
661
|
/**
|
|
739
|
-
* Check whether log level is within log's app current level
|
|
662
|
+
* Check whether log level is within log's app current level.
|
|
740
663
|
*
|
|
741
664
|
* @method
|
|
742
|
-
* @param {string} level - Level to check. See {@link TLogLevels} for more
|
|
665
|
+
* @param {string} level - Level to check. See {@link TLogLevels} for more.
|
|
743
666
|
* @returns {boolean}
|
|
744
667
|
*/
|
|
745
668
|
isLogInRange = (level) => {
|
|
@@ -763,11 +686,11 @@ class Bajo extends Plugin {
|
|
|
763
686
|
}
|
|
764
687
|
|
|
765
688
|
/**
|
|
766
|
-
* Check whether directory is a valid Bajo app
|
|
689
|
+
* Check whether directory is a valid Bajo app.
|
|
767
690
|
*
|
|
768
691
|
* @method
|
|
769
|
-
* @param {string} dir - Directory to check
|
|
770
|
-
* @param {boolean} [returnPkg] - Set ```true``` to return its package.json content
|
|
692
|
+
* @param {string} dir - Directory to check.
|
|
693
|
+
* @param {boolean} [returnPkg] - Set ```true``` to return its package.json content.
|
|
771
694
|
* @returns {(boolean|Object)}
|
|
772
695
|
*/
|
|
773
696
|
isValidApp = (dir, returnPkg) => {
|
|
@@ -776,11 +699,11 @@ class Bajo extends Plugin {
|
|
|
776
699
|
}
|
|
777
700
|
|
|
778
701
|
/**
|
|
779
|
-
* Check whether directory is a valid Bajo plugin
|
|
702
|
+
* Check whether directory is a valid Bajo plugin.
|
|
780
703
|
*
|
|
781
704
|
* @method
|
|
782
|
-
* @param {string} dir - Directory to check
|
|
783
|
-
* @param {boolean} [returnPkg] - Set ```true``` to return its package.json content
|
|
705
|
+
* @param {string} dir - Directory to check.
|
|
706
|
+
* @param {boolean} [returnPkg] - Set ```true``` to return its package.json content.
|
|
784
707
|
* @returns {(boolean|Object)}
|
|
785
708
|
*/
|
|
786
709
|
isValidPlugin = (dir, returnPkg) => {
|
|
@@ -793,9 +716,9 @@ class Bajo extends Plugin {
|
|
|
793
716
|
*
|
|
794
717
|
* @method
|
|
795
718
|
* @param {any[]} array - Array to join
|
|
796
|
-
* @param {(string|Object)} options - If provided and is a string, it will be used as separator
|
|
797
|
-
* @param {string} [options.separator=', '] - Separator to use
|
|
798
|
-
* @param {string} [options.lastSeparator=and] - Text to use as the last separator
|
|
719
|
+
* @param {(string|Object)} options - If provided and is a string, it will be used as separator.
|
|
720
|
+
* @param {string} [options.separator=', '] - Separator to use.
|
|
721
|
+
* @param {string} [options.lastSeparator=and] - Text to use as the last separator.
|
|
799
722
|
* @returns {string}
|
|
800
723
|
*/
|
|
801
724
|
join = (input = [], options = {}) => {
|
|
@@ -813,11 +736,11 @@ class Bajo extends Plugin {
|
|
|
813
736
|
}
|
|
814
737
|
|
|
815
738
|
/**
|
|
816
|
-
* Return its numeric portion of a value
|
|
739
|
+
* Return its numeric portion of a value.
|
|
817
740
|
*
|
|
818
741
|
* @method
|
|
819
|
-
* @param {string} [value=''] - Value to get its numeric portion
|
|
820
|
-
* @param {string} [defUnit=''] - Default unit if value doesn't have one
|
|
742
|
+
* @param {string} [value=''] - Value to get its numeric portion.
|
|
743
|
+
* @param {string} [defUnit=''] - Default unit if value doesn't have one.
|
|
821
744
|
* @returns {string}
|
|
822
745
|
*/
|
|
823
746
|
numUnit = (value = '', defUnit = '') => {
|
|
@@ -828,20 +751,20 @@ class Bajo extends Plugin {
|
|
|
828
751
|
|
|
829
752
|
/**
|
|
830
753
|
* Read and parse file as config object. Supported types: ```.js``` and ```.json```.
|
|
831
|
-
* More supports can be added using plugin. {@link https://github.com/ardhi/bajo-config|bajo-config} gives you additional supports for ```.yml```, ```.yaml``` and ```.toml``` file
|
|
754
|
+
* More supports can be added using plugin. {@link https://github.com/ardhi/bajo-config|bajo-config} gives you additional supports for ```.yml```, ```.yaml``` and ```.toml``` file.
|
|
832
755
|
*
|
|
833
756
|
* If file extension is ```.*```, it will be auto detected and parsed accordingly
|
|
834
757
|
*
|
|
835
758
|
* @method
|
|
836
759
|
* @async
|
|
837
|
-
* @param {string} file - File to read and parse
|
|
838
|
-
* @param {Object} [options={}] - Options
|
|
839
|
-
* @param {boolean} [options.ignoreError] - Any exception will be silently discarded
|
|
840
|
-
* @param {string} [options.ns] - If given, use this as the scope
|
|
841
|
-
* @param {string} [options.pattern] - If given and auto detection is on (extension is ```.*```), it will be used for instead the default one
|
|
842
|
-
* @param {Object} [options.defValue={}] - Default value to use if value returned empty
|
|
843
|
-
* @param {Object} [options.parserOpts={}] - Parser setting
|
|
844
|
-
* @param {Object} [options.globOpts={}] - {@link https://github.com/mrmlnc/fast-glob|fast-glob} options
|
|
760
|
+
* @param {string} file - File to read and parse.
|
|
761
|
+
* @param {Object} [options={}] - Options.
|
|
762
|
+
* @param {boolean} [options.ignoreError] - Any exception will be silently discarded.
|
|
763
|
+
* @param {string} [options.ns] - If given, use this as the scope.
|
|
764
|
+
* @param {string} [options.pattern] - If given and auto detection is on (extension is ```.*```), it will be used for instead the default one.
|
|
765
|
+
* @param {Object} [options.defValue={}] - Default value to use if value returned empty.
|
|
766
|
+
* @param {Object} [options.parserOpts={}] - Parser setting.
|
|
767
|
+
* @param {Object} [options.globOpts={}] - {@link https://github.com/mrmlnc/fast-glob|fast-glob} options.
|
|
845
768
|
* @returns {Object}
|
|
846
769
|
*/
|
|
847
770
|
readConfig = async (file, options = {}) => {
|
|
@@ -916,7 +839,7 @@ class Bajo extends Plugin {
|
|
|
916
839
|
await this.runHook('bajo:beforeReadConfig', file, options)
|
|
917
840
|
parserOpts.readFromFile = true
|
|
918
841
|
if (!ns) ns = this.ns
|
|
919
|
-
file = resolvePath(this.getPluginFile(file))
|
|
842
|
+
file = resolvePath(this.app.getPluginFile(file))
|
|
920
843
|
let ext = path.extname(file)
|
|
921
844
|
const fname = path.dirname(file) + '/' + path.basename(file, ext)
|
|
922
845
|
ext = ext.toLowerCase()
|
|
@@ -954,11 +877,11 @@ class Bajo extends Plugin {
|
|
|
954
877
|
}
|
|
955
878
|
|
|
956
879
|
/**
|
|
957
|
-
* Read and parse json file
|
|
880
|
+
* Read and parse json file.
|
|
958
881
|
*
|
|
959
882
|
* @method
|
|
960
|
-
* @param {string} file - File to read
|
|
961
|
-
* @param {boolean} [thrownNotFound=false] - If ```true```, silently ignore if file is not found
|
|
883
|
+
* @param {string} file - File to read.
|
|
884
|
+
* @param {boolean} [thrownNotFound=false] - If ```true```, silently ignore if file is not found.
|
|
962
885
|
* @returns {Object}
|
|
963
886
|
*/
|
|
964
887
|
readJson = (file, thrownNotFound = false) => {
|
|
@@ -973,6 +896,15 @@ class Bajo extends Plugin {
|
|
|
973
896
|
return parseObject(JSON.parse(resp))
|
|
974
897
|
}
|
|
975
898
|
|
|
899
|
+
/**
|
|
900
|
+
* Read and parse JavaScript file.
|
|
901
|
+
*
|
|
902
|
+
* @async
|
|
903
|
+
* @method
|
|
904
|
+
* @param {string} file - File to read and parse.
|
|
905
|
+
* @param {Object} [options={}] - Options.
|
|
906
|
+
* @returns {Object} Parsed JavaScript object.
|
|
907
|
+
*/
|
|
976
908
|
async fromJs (file, options = {}) {
|
|
977
909
|
const args = options.args ?? []
|
|
978
910
|
let mod = await importModule(file)
|
|
@@ -980,11 +912,28 @@ class Bajo extends Plugin {
|
|
|
980
912
|
return mod
|
|
981
913
|
}
|
|
982
914
|
|
|
915
|
+
/**
|
|
916
|
+
* Read and parse JSON string or object.
|
|
917
|
+
*
|
|
918
|
+
* @param {string} data - Filename to load from or JSON string to parse.
|
|
919
|
+
* @param {Object} [options={}] - Options.
|
|
920
|
+
* @returns {Object} Parsed JSON object.
|
|
921
|
+
*/
|
|
983
922
|
fromJson (data, options = {}) {
|
|
984
923
|
const content = options.readFromFile ? fs.readFileSync(data, 'utf8') : data
|
|
985
924
|
return JSON.parse(content)
|
|
986
925
|
}
|
|
987
926
|
|
|
927
|
+
/**
|
|
928
|
+
* Convert data to JSON string.
|
|
929
|
+
*
|
|
930
|
+
* @method
|
|
931
|
+
* @param {Object} data - Data to convert to JSON string.
|
|
932
|
+
* @param {Object} [options={}] - Options.
|
|
933
|
+
* @param {boolean} [options.writeToFile=false] - If true, write the JSON string to a file.
|
|
934
|
+
* @param {string} [options.saveAsFile] - The file path to save the JSON string if writeToFile is true.
|
|
935
|
+
* @returns {string} JSON string
|
|
936
|
+
*/
|
|
988
937
|
toJson = (data, options = {}) => {
|
|
989
938
|
const content = JSON.stringify(data, null, omit(options, ['writeToFile']))
|
|
990
939
|
if (options.writeToFile) {
|
|
@@ -995,12 +944,13 @@ class Bajo extends Plugin {
|
|
|
995
944
|
}
|
|
996
945
|
|
|
997
946
|
/**
|
|
998
|
-
* Read all config files
|
|
947
|
+
* Read all config files from path.
|
|
999
948
|
*
|
|
1000
949
|
* @method
|
|
1001
950
|
* @async
|
|
1002
|
-
* @param {string} path - Base path to start looking config files
|
|
1003
|
-
* @
|
|
951
|
+
* @param {string} path - Base path to start looking config files.
|
|
952
|
+
* @param {Object} [options={}] - Options.
|
|
953
|
+
* @returns {Object} Merged configuration object.
|
|
1004
954
|
*/
|
|
1005
955
|
readAllConfigs = async (path, options) => {
|
|
1006
956
|
const { defaultsDeep } = this.app.lib.aneka
|
|
@@ -1022,13 +972,13 @@ class Bajo extends Plugin {
|
|
|
1022
972
|
}
|
|
1023
973
|
|
|
1024
974
|
/**
|
|
1025
|
-
* Run named hook/event
|
|
975
|
+
* Run named hook/event.
|
|
1026
976
|
*
|
|
1027
977
|
* @method
|
|
1028
978
|
* @async
|
|
1029
|
-
* @param {TNsPathPairs} hookName
|
|
1030
|
-
* @param
|
|
1031
|
-
* @returns {Array} Array of hook execution results
|
|
979
|
+
* @param {TNsPathPairs} hookName - Name of the hook to run.
|
|
980
|
+
* @param {...any} [args] - Argument passed to the hook function.
|
|
981
|
+
* @returns {Array} Array of hook execution results.
|
|
1032
982
|
*/
|
|
1033
983
|
runHook = async (hookName, ...args) => {
|
|
1034
984
|
let fns = filter(this.hooks, { name: hookName })
|
|
@@ -1059,14 +1009,14 @@ class Bajo extends Plugin {
|
|
|
1059
1009
|
*
|
|
1060
1010
|
* @method
|
|
1061
1011
|
* @async
|
|
1062
|
-
* @param {string} file - File name
|
|
1063
|
-
* @param {Object} item - Item to save
|
|
1064
|
-
* @param {boolean} [printSaved=true] - Print info on screen
|
|
1065
|
-
* @returns {string} Full file path
|
|
1012
|
+
* @param {string} file - File name.
|
|
1013
|
+
* @param {Object} item - Item to save.
|
|
1014
|
+
* @param {boolean} [printSaved=true] - Print info on screen.
|
|
1015
|
+
* @returns {string} Full file path.
|
|
1066
1016
|
*/
|
|
1067
1017
|
saveAsDownload = async (file, item, printSaved = true) => {
|
|
1068
|
-
const { print
|
|
1069
|
-
const fname = increment(`${getPluginDataDir(this.ns)}/download/${trim(file, '/')}`, { fs: true })
|
|
1018
|
+
const { print } = this.app.bajo
|
|
1019
|
+
const fname = increment(`${this.app.getPluginDataDir(this.ns)}/download/${trim(file, '/')}`, { fs: true })
|
|
1070
1020
|
const dir = path.dirname(fname)
|
|
1071
1021
|
if (!fs.existsSync(dir)) fs.ensureDirSync(dir)
|
|
1072
1022
|
await fs.writeFile(fname, item, 'utf8')
|
package/class/base.js
CHANGED
|
@@ -9,8 +9,8 @@ import Plugin from './plugin.js'
|
|
|
9
9
|
|
|
10
10
|
class Base extends Plugin {
|
|
11
11
|
/**
|
|
12
|
-
* @param {string} pkgName - Package name (the one you use in package.json)
|
|
13
|
-
* @param {Object} app - App instance reference.
|
|
12
|
+
* @param {string} pkgName - Package name (the one you use in package.json).
|
|
13
|
+
* @param {Object} app - App instance reference. Useful to call app method inside a plugin.
|
|
14
14
|
*/
|
|
15
15
|
constructor (pkgName, app) {
|
|
16
16
|
super(pkgName, app)
|
|
@@ -98,7 +98,7 @@ class Base extends Plugin {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* Dispose internal references
|
|
101
|
+
* Dispose internal references.
|
|
102
102
|
*/
|
|
103
103
|
dispose = async () => {
|
|
104
104
|
await super.dispose()
|