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.
Files changed (77) hide show
  1. package/bajo/intl/en-US.json +5 -2
  2. package/bajo/intl/id.json +5 -2
  3. package/boot/class/app.js +11 -13
  4. package/boot/class/bajo-core/{helper/boot-order.js → boot-order.js} +3 -4
  5. package/boot/class/bajo-core/boot-plugins.js +19 -0
  6. package/boot/class/bajo-core/{helper/build-config.js → build-config.js} +16 -18
  7. package/boot/class/bajo-core/{helper/build-plugins.js → build-plugins.js} +13 -5
  8. package/boot/class/bajo-core/{helper/collect-config-handlers.js → collect-config-handlers.js} +2 -4
  9. package/boot/class/bajo-core.js +738 -11
  10. package/boot/class/bajo-plugin/{helper/attach-method.js → attach-method.js} +2 -2
  11. package/boot/class/bajo-plugin/{helper/collect-exit-handlers.js → collect-exit-handlers.js} +1 -1
  12. package/boot/class/bajo-plugin.js +13 -14
  13. package/boot/class/error.js +10 -2
  14. package/boot/class/log.js +9 -10
  15. package/boot/class/plugin.js +8 -12
  16. package/boot/class/print.js +16 -17
  17. package/boot/lib/create-method.js +3 -5
  18. package/boot/lib/current-loc.js +1 -1
  19. package/boot/lib/import-module.js +27 -0
  20. package/boot/lib/log-levels.js +1 -0
  21. package/boot/lib/parse-args-argv.js +2 -3
  22. package/boot/lib/read-all-configs.js +3 -6
  23. package/boot/{class/bajo-core/method → lib}/resolve-path.js +1 -1
  24. package/package.json +1 -1
  25. package/boot/class/bajo-core/helper/attach-method.js +0 -31
  26. package/boot/class/bajo-core/helper/boot-plugins.js +0 -19
  27. package/boot/class/bajo-core/method/arrange-array.js +0 -19
  28. package/boot/class/bajo-core/method/break-ns-path-from-file.js +0 -25
  29. package/boot/class/bajo-core/method/break-ns-path.js +0 -31
  30. package/boot/class/bajo-core/method/build-collections.js +0 -48
  31. package/boot/class/bajo-core/method/call-handler.js +0 -31
  32. package/boot/class/bajo-core/method/defaults-deep.js +0 -17
  33. package/boot/class/bajo-core/method/each-plugins.js +0 -57
  34. package/boot/class/bajo-core/method/envs.js +0 -7
  35. package/boot/class/bajo-core/method/extract-text.js +0 -15
  36. package/boot/class/bajo-core/method/format.js +0 -36
  37. package/boot/class/bajo-core/method/generate-id.js +0 -23
  38. package/boot/class/bajo-core/method/get-global-module-dir.js +0 -28
  39. package/boot/class/bajo-core/method/get-key-by-value.js +0 -5
  40. package/boot/class/bajo-core/method/get-method.js +0 -12
  41. package/boot/class/bajo-core/method/get-module-dir.js +0 -35
  42. package/boot/class/bajo-core/method/get-plugin-data-dir.js +0 -11
  43. package/boot/class/bajo-core/method/get-plugin-file.js +0 -21
  44. package/boot/class/bajo-core/method/get-plugin.js +0 -23
  45. package/boot/class/bajo-core/method/import-module.js +0 -27
  46. package/boot/class/bajo-core/method/import-pkg.js +0 -48
  47. package/boot/class/bajo-core/method/includes.js +0 -11
  48. package/boot/class/bajo-core/method/is-class.js +0 -7
  49. package/boot/class/bajo-core/method/is-empty-dir.js +0 -9
  50. package/boot/class/bajo-core/method/is-log-in-range.js +0 -12
  51. package/boot/class/bajo-core/method/is-set.js +0 -5
  52. package/boot/class/bajo-core/method/is-valid-app.js +0 -12
  53. package/boot/class/bajo-core/method/is-valid-plugin.js +0 -12
  54. package/boot/class/bajo-core/method/join.js +0 -20
  55. package/boot/class/bajo-core/method/log-levels.js +0 -9
  56. package/boot/class/bajo-core/method/num-unit.js +0 -11
  57. package/boot/class/bajo-core/method/paginate.js +0 -28
  58. package/boot/class/bajo-core/method/parse-object.js +0 -62
  59. package/boot/class/bajo-core/method/pascal-case.js +0 -9
  60. package/boot/class/bajo-core/method/pick.js +0 -13
  61. package/boot/class/bajo-core/method/read-config.js +0 -50
  62. package/boot/class/bajo-core/method/read-json.js +0 -14
  63. package/boot/class/bajo-core/method/round.js +0 -6
  64. package/boot/class/bajo-core/method/run-hook.js +0 -28
  65. package/boot/class/bajo-core/method/save-as-download.js +0 -19
  66. package/boot/class/bajo-core/method/sec-to-hms.js +0 -25
  67. package/boot/class/bajo-core/method/slice-string.js +0 -13
  68. package/boot/class/bajo-core/method/titleize.js +0 -24
  69. package/boot/class/bajo-core/method/white-space.js +0 -3
  70. package/boot/lib/translate.js +0 -19
  71. /package/boot/class/bajo-core/{helper/exit-handler.js → exit-handler.js} +0 -0
  72. /package/boot/class/bajo-core/{helper/run-as-applet.js → run-as-applet.js} +0 -0
  73. /package/boot/class/bajo-plugin/{helper/build-config.js → build-config.js} +0 -0
  74. /package/boot/class/bajo-plugin/{helper/check-clash.js → check-clash.js} +0 -0
  75. /package/boot/class/bajo-plugin/{helper/check-dependency.js → check-dependency.js} +0 -0
  76. /package/boot/class/bajo-plugin/{helper/collect-hooks.js → collect-hooks.js} +0 -0
  77. /package/boot/class/bajo-plugin/{helper/run.js → run.js} +0 -0
@@ -1,50 +0,0 @@
1
- import path from 'path'
2
- import resolvePath from './resolve-path.js'
3
- import getPluginFile from './get-plugin-file.js'
4
- import readJson from './read-json.js'
5
- import parseObject from './parse-object.js'
6
- import lodash from 'lodash'
7
- import fg from 'fast-glob'
8
-
9
- const { find, map, isEmpty } = lodash
10
-
11
- async function readConfig (file, { ns, pattern, globOptions = {}, ignoreError, defValue = {}, opts = {} } = {}) {
12
- if (!ns) ns = this.name
13
- file = resolvePath(getPluginFile.call(this, file))
14
- let ext = path.extname(file)
15
- const fname = path.dirname(file) + '/' + path.basename(file, ext)
16
- ext = ext.toLowerCase()
17
- if (['.mjs', '.js'].includes(ext)) {
18
- const { readHandler } = find(this.app.bajo.configHandlers, { ext })
19
- return parseObject(await readHandler.call(this.app[ns], file, opts))
20
- }
21
- if (ext === '.json') return await readJson(file)
22
- if (!['', '.*'].includes(ext)) {
23
- const item = find(this.app.bajo.configHandlers, { ext })
24
- if (!item) {
25
- if (!ignoreError) throw this.error('cantParse%s', file, { code: 'BAJO_CONFIG_NO_PARSER' })
26
- return parseObject(defValue)
27
- }
28
- return parseObject(await item.readHandler.call(this.app[ns], file, opts))
29
- }
30
- const item = pattern ?? `${fname}.{${map(map(this.app.bajo.configHandlers, 'ext'), k => k.slice(1)).join(',')}}`
31
- const files = await fg(item, globOptions)
32
- if (files.length === 0) {
33
- if (!ignoreError) throw this.error('noConfigFileFound', { code: 'BAJO_CONFIG_FILE_NOT_FOUND' })
34
- return parseObject(defValue)
35
- }
36
- let config = defValue
37
- for (const f of files) {
38
- const ext = path.extname(f).toLowerCase()
39
- const item = find(this.app.bajo.configHandlers, { ext })
40
- if (!item) {
41
- if (!ignoreError) throw this.error('cantParse%s', f, { code: 'BAJO_CONFIG_NO_PARSER' })
42
- continue
43
- }
44
- config = await item.readHandler.call(this.app[ns], f, opts)
45
- if (!isEmpty(config)) break
46
- }
47
- return parseObject(config)
48
- }
49
-
50
- export default readConfig
@@ -1,14 +0,0 @@
1
- import fs from 'fs-extra'
2
- import lodash from 'lodash'
3
- import parseObject from './parse-object.js'
4
-
5
- const { isEmpty } = lodash
6
-
7
- function readJson (file, thrownNotFound) {
8
- if (!fs.existsSync(file) && thrownNotFound) throw this.error('notFound%s%s', this.print.write('file'), file)
9
- let resp = fs.readFileSync(file, 'utf8')
10
- if (isEmpty(resp)) resp = '{}'
11
- return parseObject(JSON.parse(resp))
12
- }
13
-
14
- export default readJson
@@ -1,6 +0,0 @@
1
- function round (val, scale = 0) {
2
- scale = scale <= 0 ? 1 : 10 ** scale
3
- return Math.round(val * scale) / scale
4
- }
5
-
6
- export default round
@@ -1,28 +0,0 @@
1
- import lodash from 'lodash'
2
-
3
- const { filter, isEmpty, orderBy, pullAt } = lodash
4
-
5
- async function runHook (hookName, ...args) {
6
- const [ns, path] = (hookName ?? '').split(':')
7
- let fns = filter(this.app.bajo.hooks, { ns, path })
8
- if (isEmpty(fns)) return
9
- fns = orderBy(fns, ['level'])
10
- const results = []
11
- const removed = []
12
- for (const i in fns) {
13
- const fn = fns[i]
14
- const scope = this.app[fn.src]
15
- const res = await fn.handler.call(scope, ...args)
16
- results.push({
17
- hook: hookName,
18
- resp: res
19
- })
20
- if (path.startsWith('once')) removed.push(i)
21
- if (this.config.log.traceHook) scope.log.trace('hookExecuted%s', hookName)
22
- }
23
- if (removed.length > 0) pullAt(this.app.bajo.hooks, removed)
24
-
25
- return results
26
- }
27
-
28
- export default runHook
@@ -1,19 +0,0 @@
1
- import fs from 'fs-extra'
2
- import path from 'path'
3
- import lodash from 'lodash'
4
- import increment from 'add-filename-increment'
5
-
6
- const { trim } = lodash
7
-
8
- async function saveAsDownload (file, obj, printSaved = true) {
9
- const { print, getPluginDataDir } = this.app.bajo
10
- const plugin = this.name
11
- const fname = increment(`${getPluginDataDir(plugin)}/${trim(file, '/')}`, { fs: true })
12
- const dir = path.dirname(fname)
13
- if (!fs.existsSync(dir)) fs.ensureDirSync(dir)
14
- await fs.writeFile(fname, obj, 'utf8')
15
- if (printSaved) print.succeed('savedAs%s', path.resolve(fname), { skipSilence: true })
16
- return fname
17
- }
18
-
19
- export default saveAsDownload
@@ -1,25 +0,0 @@
1
- import lodash from 'lodash'
2
- const { padStart } = lodash
3
-
4
- // based on: https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript
5
-
6
- function secToHms (secs, ms) {
7
- let remain
8
- if (ms) {
9
- remain = secs % 1000
10
- secs = Math.floor(secs / 1000)
11
- }
12
- const secNum = parseInt(secs, 10)
13
- const hours = Math.floor(secNum / 3600)
14
- const minutes = Math.floor(secNum / 60) % 60
15
- const seconds = secNum % 60
16
-
17
- let hms = [hours, minutes, seconds]
18
- .map(v => v < 10 ? '0' + v : v)
19
- .filter((v, i) => v !== '00' || i > 0)
20
- .join(':')
21
- if (ms) hms += '+' + padStart(remain, 3, '0')
22
- return hms
23
- }
24
-
25
- export default secToHms
@@ -1,13 +0,0 @@
1
- function sliceString (content, start, end) {
2
- const idx1 = content.indexOf(start)
3
- if (idx1 === -1) return false
4
- let idx2 = content.indexOf(end)
5
- if (idx2 === -1) return false
6
- if (idx2 < idx1) {
7
- const tmp = content.slice(idx1)
8
- idx2 = tmp.indexOf(end) + idx1
9
- }
10
- return content.slice(idx1, idx2 + end.length)
11
- }
12
-
13
- export default sliceString
@@ -1,24 +0,0 @@
1
- import lodash from 'lodash'
2
- import generateId from './generate-id.js'
3
-
4
- const { words, upperFirst, map, concat, uniq, forOwn } = lodash
5
- const defIgnores = ['or', 'and', 'of', 'with']
6
-
7
- const titleize = (text, { ignores = [], replacement = {} } = {}) => {
8
- const replacer = {}
9
- forOwn(replacement, (v, k) => {
10
- const id = generateId('int')
11
- replacer[id] = k
12
- text = text.replace(k, ` ${id} `)
13
- })
14
- return map(words(text), t => {
15
- forOwn(replacer, (v, k) => {
16
- if (k === t) t = replacement[replacer[k]]
17
- })
18
- ignores = uniq(concat(ignores, defIgnores))
19
- if (ignores.includes(t)) return t
20
- return upperFirst(t)
21
- }).join(' ')
22
- }
23
-
24
- export default titleize
@@ -1,3 +0,0 @@
1
- const whiteSpace = [' ', '\t', '\n', '\r']
2
-
3
- export default whiteSpace
@@ -1,19 +0,0 @@
1
- import lodash from 'lodash'
2
- import Sprintf from 'sprintf-js'
3
- const { sprintf } = Sprintf
4
- const { get, isPlainObject } = lodash
5
-
6
- function translate (instance, text, ...args) {
7
- let ntext = text
8
- if (text) {
9
- const i18n = instance ?? get(this, 'app.bajoI18N.instance')
10
- if (i18n) {
11
- if (isPlainObject(args[0])) ntext = i18n.t(text, args[0])
12
- else ntext = i18n.t(text, { ns: this.name, postProcess: 'sprintf', sprintf: args })
13
- } else ntext = sprintf(text, ...args)
14
- }
15
- if (ntext === '') ntext = text
16
- return ntext
17
- }
18
-
19
- export default translate