bajo 1.0.3 → 1.0.5
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 +136 -0
- package/bajo/intl/id.json +136 -0
- package/boot/class/app.js +4 -3
- package/boot/class/bajo-core/helper/attach-method.js +3 -2
- package/boot/class/bajo-core/helper/boot-order.js +6 -4
- package/boot/class/bajo-core/helper/build-config.js +18 -3
- package/boot/class/bajo-core/helper/build-plugins.js +3 -1
- package/boot/class/bajo-core/helper/collect-config-handlers.js +2 -1
- package/boot/class/bajo-core/helper/exit-handler.js +3 -3
- package/boot/class/bajo-core/helper/run-as-applet.js +4 -4
- package/boot/class/bajo-core/method/arrange-array.js +2 -1
- package/boot/class/bajo-core/method/break-ns-path-from-file.js +2 -1
- package/boot/class/bajo-core/method/break-ns-path.js +4 -2
- package/boot/class/bajo-core/method/build-collections.js +7 -5
- package/boot/class/bajo-core/method/call-handler.js +3 -1
- package/boot/class/bajo-core/method/defaults-deep.js +4 -1
- package/boot/class/bajo-core/method/each-plugins.js +3 -1
- package/boot/class/bajo-core/method/format.js +36 -0
- package/boot/class/bajo-core/method/get-global-module-dir.js +5 -3
- package/boot/class/bajo-core/method/get-method.js +4 -2
- package/boot/class/bajo-core/method/get-plugin-file.js +3 -1
- package/boot/class/bajo-core/method/get-plugin.js +1 -1
- package/boot/class/bajo-core/method/import-module.js +3 -1
- package/boot/class/bajo-core/method/import-pkg.js +4 -2
- package/boot/class/bajo-core/method/includes.js +1 -3
- package/boot/class/bajo-core/method/is-log-in-range.js +3 -1
- package/boot/class/bajo-core/method/join.js +3 -1
- package/boot/class/bajo-core/method/num-unit.js +3 -1
- package/boot/class/bajo-core/method/paginate.js +3 -1
- package/boot/class/bajo-core/method/parse-object.js +10 -7
- package/boot/class/bajo-core/method/pascal-case.js +3 -1
- package/boot/class/bajo-core/method/read-config.js +6 -4
- package/boot/class/bajo-core/method/read-json.js +4 -2
- package/boot/class/bajo-core/method/run-hook.js +4 -2
- package/boot/class/bajo-core/method/save-as-download.js +4 -2
- package/boot/class/bajo-core/method/sec-to-hms.js +2 -1
- package/boot/class/bajo-core/method/titleize.js +2 -1
- package/boot/class/bajo-core.js +2 -1
- package/boot/class/bajo-plugin/helper/attach-method.js +1 -1
- package/boot/class/bajo-plugin/helper/build-config.js +8 -3
- package/boot/class/bajo-plugin/helper/check-clash.js +6 -4
- package/boot/class/bajo-plugin/helper/check-dependency.js +6 -4
- package/boot/class/bajo-plugin/helper/collect-exit-handlers.js +1 -1
- package/boot/class/bajo-plugin/helper/collect-hooks.js +4 -2
- package/boot/class/bajo-plugin/helper/run.js +3 -2
- package/boot/class/bajo-plugin.js +7 -4
- package/boot/class/error.js +2 -1
- package/boot/class/log.js +6 -4
- package/boot/class/plugin.js +5 -2
- package/boot/class/print.js +42 -6
- package/boot/lib/create-method.js +3 -1
- package/boot/lib/parse-args-argv.js +3 -1
- package/boot/lib/parse-env.js +3 -1
- package/boot/lib/read-all-configs.js +2 -2
- package/boot/lib/translate.js +2 -1
- package/package.json +2 -2
- package/bajoI18N/resource/id.json +0 -31
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import breakNsPath from './break-ns-path.js'
|
|
2
2
|
import currentLoc from '../../../lib/current-loc.js'
|
|
3
|
-
import
|
|
3
|
+
import lodash from 'lodash'
|
|
4
|
+
|
|
5
|
+
const { trim } = lodash
|
|
4
6
|
|
|
5
7
|
function getPluginFile (file) {
|
|
6
8
|
if (file[0] === '.') file = `${currentLoc(import.meta).dir}/${trim(file.slice(1), '/')}`
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import resolvePath from './resolve-path.js'
|
|
2
2
|
import getPluginFile from './get-plugin-file.js'
|
|
3
|
-
import
|
|
3
|
+
import lodash from 'lodash'
|
|
4
4
|
import fs from 'fs-extra'
|
|
5
5
|
|
|
6
|
+
const { isFunction, isPlainObject } = lodash
|
|
7
|
+
|
|
6
8
|
async function load (file, asDefaultImport = true, noCache = false) {
|
|
7
9
|
file = resolvePath(file, true)
|
|
8
10
|
if (noCache) file += `?_=${Date.now()}`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import os from 'os'
|
|
3
3
|
import getModuleDir from './get-module-dir.js'
|
|
4
4
|
import resolvePath from './resolve-path.js'
|
|
@@ -8,6 +8,8 @@ import breakNsPath from './break-ns-path.js'
|
|
|
8
8
|
import path from 'path'
|
|
9
9
|
import fs from 'fs-extra'
|
|
10
10
|
|
|
11
|
+
const { isPlainObject, last, isEmpty, has, keys, values, get } = lodash
|
|
12
|
+
|
|
11
13
|
async function importPkg (...pkgs) {
|
|
12
14
|
const result = {}
|
|
13
15
|
const notFound = []
|
|
@@ -37,7 +39,7 @@ async function importPkg (...pkgs) {
|
|
|
37
39
|
}
|
|
38
40
|
result[name] = mod
|
|
39
41
|
}
|
|
40
|
-
if (notFound.length > 0) throw this.error('
|
|
42
|
+
if (notFound.length > 0) throw this.error('cantFind%s', this.join(notFound))
|
|
41
43
|
if (pkgs.length === 1) return result[keys(result)[0]]
|
|
42
44
|
if (opts.asObject) return result
|
|
43
45
|
return values(result)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import isSet from './is-set.js'
|
|
3
3
|
|
|
4
|
+
const { isPlainObject } = lodash
|
|
5
|
+
|
|
4
6
|
function join (array, sep) {
|
|
5
7
|
const isI18n = this && this.app && this.app.bajoI18N
|
|
6
8
|
if (array.length === 0) return isI18n ? this.app.bajoI18N.t('none') : 'none'
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
|
|
3
|
+
const { isEmpty, isPlainObject, orderBy, forOwn } = lodash
|
|
2
4
|
|
|
3
5
|
function paginate (collection, { page = 1, limit = 25, sort } = {}) {
|
|
4
6
|
const count = collection.length
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
3
3
|
import ms from 'ms'
|
|
4
4
|
import dayjs from '../../../lib/dayjs.js'
|
|
5
5
|
import isSet from './is-set.js'
|
|
6
|
-
import translate from '../../../lib/translate.js'
|
|
6
|
+
// import translate from '../../../lib/translate.js'
|
|
7
7
|
|
|
8
|
+
const { isPlainObject, isArray, isNumber, set, cloneDeep, isString, omit } = lodash
|
|
8
9
|
const statics = ['*']
|
|
9
10
|
|
|
10
11
|
function parseDur (val) {
|
|
@@ -13,11 +14,11 @@ function parseDur (val) {
|
|
|
13
14
|
|
|
14
15
|
function parseDt (val) {
|
|
15
16
|
const dt = dayjs(val)
|
|
16
|
-
if (!dt.isValid()) throw this.error('
|
|
17
|
+
if (!dt.isValid()) throw this.error('dtUnparsable%s', val)
|
|
17
18
|
return dt.toDate()
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
function parseObject (input, { silent = true, parseValue = false,
|
|
21
|
+
function parseObject (input, { silent = true, parseValue = false, lang, ns } = {}) {
|
|
21
22
|
let obj = cloneDeep(input)
|
|
22
23
|
const keys = Object.keys(obj)
|
|
23
24
|
const me = this
|
|
@@ -37,14 +38,16 @@ function parseObject (input, { silent = true, parseValue = false, i18n, ns } = {
|
|
|
37
38
|
if (statics.includes(v)) obj[k] = v
|
|
38
39
|
else if (k.startsWith('t:') && isString(v)) {
|
|
39
40
|
const newK = k.slice(2)
|
|
40
|
-
if (
|
|
41
|
+
if (lang) {
|
|
41
42
|
const scope = ns ? me.app[ns] : me
|
|
42
43
|
let [text, ...args] = v.split('|')
|
|
43
44
|
args = args.map(a => {
|
|
44
|
-
if (a.slice(0, 2) === 't:') a = translate.call(scope, i18n, a.slice(2))
|
|
45
|
+
// if (a.slice(0, 2) === 't:') a = translate.call(scope, i18n, a.slice(2))
|
|
46
|
+
if (a.slice(0, 2) === 't:') a = scope.print.write(lang, a.slice(2))
|
|
45
47
|
return a
|
|
46
48
|
})
|
|
47
|
-
obj[newK] = translate.call(scope, i18n, text, ...args)
|
|
49
|
+
// obj[newK] = translate.call(scope, i18n, text, ...args)
|
|
50
|
+
obj[newK] = scope.print.write(text, lang, ...args)
|
|
48
51
|
} else obj[newK] = v
|
|
49
52
|
mutated.push(k)
|
|
50
53
|
} else if (parseValue) {
|
|
@@ -3,9 +3,11 @@ import resolvePath from './resolve-path.js'
|
|
|
3
3
|
import getPluginFile from './get-plugin-file.js'
|
|
4
4
|
import readJson from './read-json.js'
|
|
5
5
|
import parseObject from './parse-object.js'
|
|
6
|
-
import
|
|
6
|
+
import lodash from 'lodash'
|
|
7
7
|
import fg from 'fast-glob'
|
|
8
8
|
|
|
9
|
+
const { find, map, isEmpty } = lodash
|
|
10
|
+
|
|
9
11
|
async function readConfig (file, { ns, pattern, globOptions = {}, ignoreError, defValue = {}, opts = {} } = {}) {
|
|
10
12
|
if (!ns) ns = this.name
|
|
11
13
|
file = resolvePath(getPluginFile.call(this, file))
|
|
@@ -20,7 +22,7 @@ async function readConfig (file, { ns, pattern, globOptions = {}, ignoreError, d
|
|
|
20
22
|
if (!['', '.*'].includes(ext)) {
|
|
21
23
|
const item = find(this.app.bajo.configHandlers, { ext })
|
|
22
24
|
if (!item) {
|
|
23
|
-
if (!ignoreError) throw this.error('
|
|
25
|
+
if (!ignoreError) throw this.error('cantParse%s', file, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
24
26
|
return parseObject(defValue)
|
|
25
27
|
}
|
|
26
28
|
return parseObject(await item.readHandler.call(this.app[ns], file, opts))
|
|
@@ -28,7 +30,7 @@ async function readConfig (file, { ns, pattern, globOptions = {}, ignoreError, d
|
|
|
28
30
|
const item = pattern ?? `${fname}.{${map(map(this.app.bajo.configHandlers, 'ext'), k => k.slice(1)).join(',')}}`
|
|
29
31
|
const files = await fg(item, globOptions)
|
|
30
32
|
if (files.length === 0) {
|
|
31
|
-
if (!ignoreError) throw this.error('
|
|
33
|
+
if (!ignoreError) throw this.error('noConfigFileFound', { code: 'BAJO_CONFIG_FILE_NOT_FOUND' })
|
|
32
34
|
return parseObject(defValue)
|
|
33
35
|
}
|
|
34
36
|
let config = defValue
|
|
@@ -36,7 +38,7 @@ async function readConfig (file, { ns, pattern, globOptions = {}, ignoreError, d
|
|
|
36
38
|
const ext = path.extname(f).toLowerCase()
|
|
37
39
|
const item = find(this.app.bajo.configHandlers, { ext })
|
|
38
40
|
if (!item) {
|
|
39
|
-
if (!ignoreError) throw this.error('
|
|
41
|
+
if (!ignoreError) throw this.error('cantParse%s', f, { code: 'BAJO_CONFIG_NO_PARSER' })
|
|
40
42
|
continue
|
|
41
43
|
}
|
|
42
44
|
config = await item.readHandler.call(this.app[ns], f, opts)
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import fs from 'fs-extra'
|
|
2
|
-
import
|
|
2
|
+
import lodash from 'lodash'
|
|
3
3
|
import parseObject from './parse-object.js'
|
|
4
4
|
|
|
5
|
+
const { isEmpty } = lodash
|
|
6
|
+
|
|
5
7
|
function readJson (file, thrownNotFound) {
|
|
6
|
-
if (!fs.existsSync(file) && thrownNotFound) throw this.error('
|
|
8
|
+
if (!fs.existsSync(file) && thrownNotFound) throw this.error('notFound%s%s', this.print.write('file'), file)
|
|
7
9
|
let resp = fs.readFileSync(file, 'utf8')
|
|
8
10
|
if (isEmpty(resp)) resp = '{}'
|
|
9
11
|
return parseObject(JSON.parse(resp))
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
|
|
3
|
+
const { filter, isEmpty, orderBy, pullAt } = lodash
|
|
2
4
|
|
|
3
5
|
async function runHook (hookName, ...args) {
|
|
4
6
|
const [ns, path] = (hookName ?? '').split(':')
|
|
@@ -16,7 +18,7 @@ async function runHook (hookName, ...args) {
|
|
|
16
18
|
resp: res
|
|
17
19
|
})
|
|
18
20
|
if (path.startsWith('once')) removed.push(i)
|
|
19
|
-
if (this.config.log.traceHook) scope.log.trace('
|
|
21
|
+
if (this.config.log.traceHook) scope.log.trace('hookExecuted%s', hookName)
|
|
20
22
|
}
|
|
21
23
|
if (removed.length > 0) pullAt(this.app.bajo.hooks, removed)
|
|
22
24
|
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import fs from 'fs-extra'
|
|
2
2
|
import path from 'path'
|
|
3
|
-
import
|
|
3
|
+
import lodash from 'lodash'
|
|
4
4
|
import increment from 'add-filename-increment'
|
|
5
5
|
|
|
6
|
+
const { trim } = lodash
|
|
7
|
+
|
|
6
8
|
async function saveAsDownload (file, obj, printSaved = true) {
|
|
7
9
|
const { print, getPluginDataDir } = this.app.bajo
|
|
8
10
|
const plugin = this.name
|
|
@@ -10,7 +12,7 @@ async function saveAsDownload (file, obj, printSaved = true) {
|
|
|
10
12
|
const dir = path.dirname(fname)
|
|
11
13
|
if (!fs.existsSync(dir)) fs.ensureDirSync(dir)
|
|
12
14
|
await fs.writeFile(fname, obj, 'utf8')
|
|
13
|
-
if (printSaved) print.succeed('
|
|
15
|
+
if (printSaved) print.succeed('savedAs%s', path.resolve(fname), { skipSilence: true })
|
|
14
16
|
return fname
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import generateId from './generate-id.js'
|
|
3
3
|
|
|
4
|
+
const { words, upperFirst, map, concat, uniq, forOwn } = lodash
|
|
4
5
|
const defIgnores = ['or', 'and', 'of', 'with']
|
|
5
6
|
|
|
6
7
|
const titleize = (text, { ignores = [], replacement = {} } = {}) => {
|
package/boot/class/bajo-core.js
CHANGED
|
@@ -3,7 +3,8 @@ import dayjs from '../lib/dayjs.js'
|
|
|
3
3
|
import importModule from './bajo-core/method/import-module.js'
|
|
4
4
|
import readJson from './bajo-core/method/read-json.js'
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import lodash from 'lodash'
|
|
7
|
+
const { isFunction } = lodash
|
|
7
8
|
|
|
8
9
|
async function defConfigHandler (file, opts) {
|
|
9
10
|
let mod = await importModule(file)
|
|
@@ -3,7 +3,7 @@ import createMethod from '../../../lib/create-method.js'
|
|
|
3
3
|
async function attachMethod () {
|
|
4
4
|
const { eachPlugins } = this.bajo
|
|
5
5
|
const me = this
|
|
6
|
-
me.bajo.log.debug('
|
|
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
9
|
const num = await createMethod.call(me[ns], `${dir}/bajo/method`, pkgName)
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
|
|
3
|
+
const { camelCase } = lodash
|
|
2
4
|
|
|
3
5
|
async function buildConfig () {
|
|
4
|
-
this.bajo.log.debug('
|
|
6
|
+
this.bajo.log.debug('readConfigs')
|
|
5
7
|
for (const pkg of this.bajo.pluginPkgs) {
|
|
6
|
-
|
|
8
|
+
const plugin = this[camelCase(pkg)]
|
|
9
|
+
await plugin.loadConfig()
|
|
10
|
+
plugin.initPrint()
|
|
11
|
+
plugin.initLog()
|
|
7
12
|
}
|
|
8
13
|
}
|
|
9
14
|
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
|
|
3
|
+
const { find } = lodash
|
|
2
4
|
|
|
3
5
|
async function checkAlias () {
|
|
4
6
|
const { eachPlugins } = this.bajo
|
|
5
|
-
this.bajo.log.debug('
|
|
7
|
+
this.bajo.log.debug('checkAliasNameClash')
|
|
6
8
|
const refs = []
|
|
7
9
|
await eachPlugins(async function ({ ns, pkgName, alias }) {
|
|
8
10
|
let item = find(refs, { ns })
|
|
9
|
-
if (item) throw this.error('
|
|
11
|
+
if (item) throw this.error('pluginNameClash%s%s%s%s', ns, pkgName, item.ns, item.pkgName, { code: 'BAJO_NAME_CLASH' })
|
|
10
12
|
item = find(refs, { alias })
|
|
11
|
-
if (item) throw this.error('
|
|
13
|
+
if (item) throw this.error('pluginNameClash%s%s%s%s', alias, pkgName, item.alias, item.pkgName, { code: 'BAJO_ALIAS_CLASH' })
|
|
12
14
|
refs.push({ ns, alias, pkgName })
|
|
13
15
|
})
|
|
14
16
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import semver from 'semver'
|
|
3
3
|
|
|
4
|
+
const { reduce, map, trim, keys, intersection, each, camelCase, get } = lodash
|
|
5
|
+
|
|
4
6
|
async function runner ({ ns, pkgName }) {
|
|
5
7
|
const { join } = this.app.bajo
|
|
6
8
|
this.app.bajo.log.trace('- %s', ns)
|
|
@@ -13,14 +15,14 @@ async function runner ({ ns, pkgName }) {
|
|
|
13
15
|
const deps = keys(odep)
|
|
14
16
|
if (deps.length > 0) {
|
|
15
17
|
if (intersection(this.app.bajo.pluginPkgs, deps).length !== deps.length) {
|
|
16
|
-
throw this.error('
|
|
18
|
+
throw this.error('dependencyUnfulfilled%s%s', pkgName, join(deps), { code: 'BAJO_DEPENDENCY' })
|
|
17
19
|
}
|
|
18
20
|
each(deps, d => {
|
|
19
21
|
if (!odep[d]) return
|
|
20
22
|
const ver = get(this.app[camelCase(d)], 'config.pkg.version')
|
|
21
23
|
if (!ver) return
|
|
22
24
|
if (!semver.satisfies(ver, odep[d])) {
|
|
23
|
-
throw this.error('
|
|
25
|
+
throw this.error('semverCheckFailed%s%s', pkgName, `${d}@${odep[d]}`, { code: 'BAJO_DEPENDENCY_SEMVER' })
|
|
24
26
|
}
|
|
25
27
|
})
|
|
26
28
|
}
|
|
@@ -28,7 +30,7 @@ async function runner ({ ns, pkgName }) {
|
|
|
28
30
|
|
|
29
31
|
async function checkDependency () {
|
|
30
32
|
const { eachPlugins } = this.bajo
|
|
31
|
-
this.bajo.log.debug('
|
|
33
|
+
this.bajo.log.debug('checkDeps')
|
|
32
34
|
await eachPlugins(async function ({ ns, pkgName, config }) {
|
|
33
35
|
await runner.call(this, { ns, pkgName, config })
|
|
34
36
|
})
|
|
@@ -8,7 +8,7 @@ async function collectExitHandlers () {
|
|
|
8
8
|
this.app[ns].exitHandler = mod
|
|
9
9
|
nss.push(ns)
|
|
10
10
|
})
|
|
11
|
-
this.bajo.log.trace('
|
|
11
|
+
this.bajo.log.trace('exitHandlers%s', nss.length === 0 ? print.write('none') : join(nss))
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export default collectExitHandlers
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
|
|
3
|
+
const { merge, forOwn, groupBy } = lodash
|
|
2
4
|
|
|
3
5
|
async function collectHooks () {
|
|
4
6
|
const { eachPlugins, runHook, isLogInRange, importModule, breakNsPathFromFile } = this.bajo
|
|
5
7
|
const me = this
|
|
6
8
|
me.bajo.hooks = this.bajo.hooks ?? []
|
|
7
|
-
me.bajo.log.debug('
|
|
9
|
+
me.bajo.log.debug('collectHooks')
|
|
8
10
|
// collects
|
|
9
11
|
await eachPlugins(async function ({ ns, dir, file }) {
|
|
10
12
|
const { fullNs, path } = breakNsPathFromFile({ file, dir, baseNs: ns, suffix: '/hook/' })
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
|
+
const { camelCase, map } = lodash
|
|
2
3
|
|
|
3
4
|
async function run () {
|
|
4
5
|
const me = this
|
|
@@ -14,7 +15,7 @@ async function run () {
|
|
|
14
15
|
})
|
|
15
16
|
await runHook(`bajo:${camelCase(`after ${method} all plugins`)}`)
|
|
16
17
|
}
|
|
17
|
-
me.bajo.log.debug('
|
|
18
|
+
me.bajo.log.debug('loadedPlugins%s', join(map(me.bajo.pluginPkgs, b => camelCase(b))))
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
export default run
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import Plugin from './plugin.js'
|
|
2
|
-
import
|
|
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
5
|
import titleize from './bajo-core/method/titleize.js'
|
|
6
6
|
import fs from 'fs-extra'
|
|
7
7
|
|
|
8
|
+
const { pick, omit, camelCase, trim, without } = lodash
|
|
9
|
+
|
|
8
10
|
class BajoPlugin extends Plugin {
|
|
9
11
|
constructor (pkgName, app) {
|
|
10
12
|
super(pkgName, app)
|
|
@@ -41,15 +43,16 @@ class BajoPlugin extends Plugin {
|
|
|
41
43
|
const envArgv = defaultsDeep({}, omit(this.app.env[this.name] ?? {}, omittedPluginKeys) ?? {}, omit(this.app.argv[this.name] ?? {}, omittedPluginKeys) ?? {})
|
|
42
44
|
cfg = defaultsDeep({}, envArgv ?? {}, cfg ?? {})
|
|
43
45
|
this.title = this.title ?? cfg.title ?? titleize(this.alias)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
|
|
47
|
+
this.dependencies = []
|
|
48
|
+
const depFile = `${dir}/bajo/.dependencies`
|
|
49
|
+
if (fs.existsSync(depFile)) this.dependencies = without(fs.readFileSync(depFile, 'utf8').split('\n').map(item => trim(item)), '')
|
|
46
50
|
this.config = omit(cfg, ['title', 'dependencies'])
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
async _onoff (item, text, ...args) {
|
|
50
54
|
this.state[item] = false
|
|
51
55
|
const { runHook, importModule } = this.app.bajo
|
|
52
|
-
const { camelCase } = this.app.bajo.lib._
|
|
53
56
|
const mod = await importModule(`${this.dir.pkg}/bajo/${item}.js`)
|
|
54
57
|
if (mod) {
|
|
55
58
|
this.log.trace(text)
|
package/boot/class/error.js
CHANGED
package/boot/class/log.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import os from 'os'
|
|
2
|
-
import
|
|
2
|
+
import lodash from 'lodash'
|
|
3
3
|
import levels from './bajo-core/method/log-levels.js'
|
|
4
4
|
import isLogInRange from './bajo-core/method/is-log-in-range.js'
|
|
5
|
-
import translate from '../lib/translate.js'
|
|
6
5
|
import dayjs from 'dayjs'
|
|
7
6
|
|
|
7
|
+
const { isEmpty, without, merge, upperFirst } = lodash
|
|
8
|
+
|
|
8
9
|
class Log {
|
|
9
10
|
constructor (plugin) {
|
|
10
11
|
this.plugin = plugin
|
|
@@ -16,7 +17,8 @@ class Log {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
write (text, ...args) {
|
|
19
|
-
|
|
20
|
+
const lang = this.plugin.app.bajo.config.lang
|
|
21
|
+
return this.plugin.print.write(text, lang, ...args)
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
isExtLogger () {
|
|
@@ -38,7 +40,7 @@ class Log {
|
|
|
38
40
|
data = null
|
|
39
41
|
}
|
|
40
42
|
args = without(args, undefined)
|
|
41
|
-
msg =
|
|
43
|
+
msg = `[${this.plugin.name}] ${this.write(msg, ...args)}`
|
|
42
44
|
if (this.plugin.app[this.bajoLog] && this.plugin.app[this.bajoLog].logger) {
|
|
43
45
|
this.plugin.app[this.bajoLog].logger[level](data, msg, ...args)
|
|
44
46
|
} else {
|
package/boot/class/plugin.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash'
|
|
2
2
|
import omittedPluginKeys from '../lib/omitted-plugin-keys.js'
|
|
3
3
|
import Log from './log.js'
|
|
4
4
|
import Print from './print.js'
|
|
5
5
|
import BajoError from './error.js'
|
|
6
6
|
|
|
7
|
+
const { get, isEmpty, cloneDeep, omit, isPlainObject, camelCase } = lodash
|
|
8
|
+
|
|
7
9
|
class Plugin {
|
|
8
10
|
constructor (pkgName, app) {
|
|
9
11
|
this.pkgName = pkgName
|
|
@@ -12,7 +14,6 @@ class Plugin {
|
|
|
12
14
|
this.config = {}
|
|
13
15
|
this.lib = {}
|
|
14
16
|
this.exitHandler = undefined
|
|
15
|
-
this.initLog()
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
getConfig (path, options = {}) {
|
|
@@ -25,10 +26,12 @@ class Plugin {
|
|
|
25
26
|
|
|
26
27
|
initLog () {
|
|
27
28
|
this.log = new Log(this)
|
|
29
|
+
this.log.init()
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
initPrint (opts) {
|
|
31
33
|
this.print = new Print(this, opts)
|
|
34
|
+
this.print.init()
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
error (msg, ...args) {
|
package/boot/class/print.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import ora from 'ora'
|
|
2
|
-
import
|
|
2
|
+
import lodash from 'lodash'
|
|
3
3
|
import defaultsDeep from './bajo-core/method/defaults-deep.js'
|
|
4
|
-
import
|
|
4
|
+
import fs from 'fs-extra'
|
|
5
|
+
import Sprintf from 'sprintf-js'
|
|
6
|
+
const { sprintf } = Sprintf
|
|
7
|
+
|
|
8
|
+
const { isPlainObject, get, without, reverse } = lodash
|
|
5
9
|
|
|
6
10
|
class Print {
|
|
7
11
|
constructor (plugin, opts = {}) {
|
|
@@ -10,6 +14,42 @@ class Print {
|
|
|
10
14
|
this.startTime = this.plugin.app.bajo.lib.dayjs()
|
|
11
15
|
this.setOpts()
|
|
12
16
|
this.ora = ora(this.opts)
|
|
17
|
+
this.intl = {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
init () {
|
|
21
|
+
for (const l of this.plugin.app.bajo.config.intl.supported) {
|
|
22
|
+
this.intl[l] = {}
|
|
23
|
+
const path = `${this.plugin.dir.pkg}/bajo/intl/${l}.json`
|
|
24
|
+
if (!fs.existsSync(path)) continue
|
|
25
|
+
const trans = fs.readFileSync(path, 'utf8')
|
|
26
|
+
try {
|
|
27
|
+
this.intl[l] = JSON.parse(trans)
|
|
28
|
+
} catch (err) {}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
write (text, lang, ...args) {
|
|
33
|
+
const fallback = this.plugin.app.bajo.config.intl.fallback
|
|
34
|
+
const plugins = reverse(without([...this.plugin.app.bajo.pluginNames], this.plugin.name))
|
|
35
|
+
plugins.unshift(this.plugin.name)
|
|
36
|
+
plugins.push('bajo')
|
|
37
|
+
|
|
38
|
+
let trans
|
|
39
|
+
for (const p of plugins) {
|
|
40
|
+
const root = get(this, `plugin.app.${p}.print.intl.${lang}`, {})
|
|
41
|
+
trans = get(root, text)
|
|
42
|
+
if (trans) break
|
|
43
|
+
}
|
|
44
|
+
if (!trans) {
|
|
45
|
+
for (const p of plugins) {
|
|
46
|
+
const root = get(this, `plugin.app.${p}.print.intl.${fallback}`, {})
|
|
47
|
+
trans = get(root, text)
|
|
48
|
+
if (trans) break
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!trans) trans = text
|
|
52
|
+
return sprintf(trans, ...args)
|
|
13
53
|
}
|
|
14
54
|
|
|
15
55
|
setOpts (args = []) {
|
|
@@ -33,10 +73,6 @@ class Print {
|
|
|
33
73
|
return this
|
|
34
74
|
}
|
|
35
75
|
|
|
36
|
-
write (text, ...args) {
|
|
37
|
-
return translate.call(this.plugin, null, text, ...args)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
76
|
getElapsed (unit = 'hms') {
|
|
41
77
|
const u = unit === 'hms' ? 'second' : unit
|
|
42
78
|
const elapsed = this.plugin.app.bajo.lib.dayjs().diff(this.startTime, u)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import fastGlob from 'fast-glob'
|
|
2
2
|
import path from 'path'
|
|
3
|
-
import
|
|
3
|
+
import lodash from 'lodash'
|
|
4
4
|
import resolvePath from '../class/bajo-core/method/resolve-path.js'
|
|
5
5
|
import importModule from '../class/bajo-core/method/import-module.js'
|
|
6
6
|
import readJson from '../class/bajo-core/method/read-json.js'
|
|
7
7
|
|
|
8
|
+
const { camelCase, isFunction, isPlainObject, forOwn } = lodash
|
|
9
|
+
|
|
8
10
|
export default async function (dir, pkg = 'bajo') {
|
|
9
11
|
dir = resolvePath(dir)
|
|
10
12
|
const files = await fastGlob([`!${dir}/**/_*.{js,json}`, `${dir}/**/*.{js,json}`])
|
|
@@ -4,11 +4,13 @@ import { unflatten } from 'flat'
|
|
|
4
4
|
import isSet from '../class/bajo-core/method/is-set.js'
|
|
5
5
|
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
6
6
|
import importModule from '../class/bajo-core/method/import-module.js'
|
|
7
|
-
import
|
|
7
|
+
import lodash from 'lodash'
|
|
8
8
|
import fs from 'fs-extra'
|
|
9
9
|
import path from 'path'
|
|
10
10
|
import currentLoc from './current-loc.js'
|
|
11
11
|
|
|
12
|
+
const { find, each, set, camelCase, forOwn } = lodash
|
|
13
|
+
|
|
12
14
|
const parseItem = (data, delimiter) => {
|
|
13
15
|
return unflatten(data, {
|
|
14
16
|
delimiter,
|
package/boot/lib/parse-env.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
2
2
|
import { unflatten } from 'flat'
|
|
3
3
|
import dotEnv from 'dotenv'
|
|
4
|
-
import
|
|
4
|
+
import lodash from 'lodash'
|
|
5
|
+
|
|
6
|
+
const { each, set, camelCase, forOwn } = lodash
|
|
5
7
|
|
|
6
8
|
const parse = (data, delimiter) => {
|
|
7
9
|
return unflatten(data, {
|