bajo 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bajo/intl/en-US.json +4 -2
- package/bajo/intl/id.json +4 -2
- package/boot/class/app.js +8 -10
- package/boot/class/bajo-core/{helper/boot-order.js → boot-order.js} +1 -2
- package/boot/class/bajo-core/boot-plugins.js +19 -0
- package/boot/class/bajo-core/{helper/build-config.js → build-config.js} +15 -17
- package/boot/class/bajo-core/{helper/build-plugins.js → build-plugins.js} +2 -3
- package/boot/class/bajo-core/{helper/collect-config-handlers.js → collect-config-handlers.js} +2 -4
- package/boot/class/bajo-core.js +736 -11
- package/boot/class/bajo-plugin/{helper/attach-method.js → attach-method.js} +1 -1
- package/boot/class/bajo-plugin.js +1 -2
- package/boot/class/log.js +4 -5
- package/boot/class/print.js +1 -2
- package/boot/lib/create-method.js +3 -5
- package/boot/lib/current-loc.js +1 -1
- package/boot/lib/import-module.js +27 -0
- package/boot/lib/log-levels.js +1 -0
- package/boot/lib/parse-args-argv.js +2 -3
- package/boot/lib/read-all-configs.js +3 -6
- package/boot/{class/bajo-core/method → lib}/resolve-path.js +1 -1
- package/package.json +1 -1
- package/boot/class/bajo-core/helper/attach-method.js +0 -31
- package/boot/class/bajo-core/helper/boot-plugins.js +0 -19
- package/boot/class/bajo-core/method/arrange-array.js +0 -19
- package/boot/class/bajo-core/method/break-ns-path-from-file.js +0 -25
- package/boot/class/bajo-core/method/break-ns-path.js +0 -31
- package/boot/class/bajo-core/method/build-collections.js +0 -48
- package/boot/class/bajo-core/method/call-handler.js +0 -31
- package/boot/class/bajo-core/method/defaults-deep.js +0 -17
- package/boot/class/bajo-core/method/each-plugins.js +0 -57
- package/boot/class/bajo-core/method/envs.js +0 -7
- package/boot/class/bajo-core/method/extract-text.js +0 -15
- package/boot/class/bajo-core/method/format.js +0 -36
- package/boot/class/bajo-core/method/generate-id.js +0 -23
- package/boot/class/bajo-core/method/get-global-module-dir.js +0 -28
- package/boot/class/bajo-core/method/get-key-by-value.js +0 -5
- package/boot/class/bajo-core/method/get-method.js +0 -12
- package/boot/class/bajo-core/method/get-module-dir.js +0 -35
- package/boot/class/bajo-core/method/get-plugin-data-dir.js +0 -11
- package/boot/class/bajo-core/method/get-plugin-file.js +0 -21
- package/boot/class/bajo-core/method/get-plugin.js +0 -23
- package/boot/class/bajo-core/method/import-module.js +0 -27
- package/boot/class/bajo-core/method/import-pkg.js +0 -48
- package/boot/class/bajo-core/method/includes.js +0 -11
- package/boot/class/bajo-core/method/is-class.js +0 -7
- package/boot/class/bajo-core/method/is-empty-dir.js +0 -9
- package/boot/class/bajo-core/method/is-log-in-range.js +0 -12
- package/boot/class/bajo-core/method/is-set.js +0 -5
- package/boot/class/bajo-core/method/is-valid-app.js +0 -12
- package/boot/class/bajo-core/method/is-valid-plugin.js +0 -12
- package/boot/class/bajo-core/method/join.js +0 -20
- package/boot/class/bajo-core/method/log-levels.js +0 -9
- package/boot/class/bajo-core/method/num-unit.js +0 -11
- package/boot/class/bajo-core/method/paginate.js +0 -28
- package/boot/class/bajo-core/method/parse-object.js +0 -62
- package/boot/class/bajo-core/method/pascal-case.js +0 -9
- package/boot/class/bajo-core/method/pick.js +0 -13
- package/boot/class/bajo-core/method/read-config.js +0 -50
- package/boot/class/bajo-core/method/read-json.js +0 -14
- package/boot/class/bajo-core/method/round.js +0 -6
- package/boot/class/bajo-core/method/run-hook.js +0 -28
- package/boot/class/bajo-core/method/save-as-download.js +0 -19
- package/boot/class/bajo-core/method/sec-to-hms.js +0 -25
- package/boot/class/bajo-core/method/titleize.js +0 -24
- package/boot/class/bajo-core/method/white-space.js +0 -3
- /package/boot/class/bajo-core/{helper/exit-handler.js → exit-handler.js} +0 -0
- /package/boot/class/bajo-core/{helper/run-as-applet.js → run-as-applet.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/build-config.js → build-config.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/check-clash.js → check-clash.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/check-dependency.js → check-dependency.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/collect-exit-handlers.js → collect-exit-handlers.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/collect-hooks.js → collect-hooks.js} +0 -0
- /package/boot/class/bajo-plugin/{helper/run.js → run.js} +0 -0
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import fs from 'fs-extra'
|
|
3
|
-
import getGlobalModuleDir from './get-global-module-dir.js'
|
|
4
|
-
import resolvePath from './resolve-path.js'
|
|
5
|
-
import { createRequire } from 'module'
|
|
6
|
-
const require = createRequire(import.meta.url)
|
|
7
|
-
|
|
8
|
-
function findDeep (item, paths) {
|
|
9
|
-
let dir
|
|
10
|
-
for (const p of paths) {
|
|
11
|
-
const d = `${p}/${item}`
|
|
12
|
-
if (fs.existsSync(d)) {
|
|
13
|
-
dir = d
|
|
14
|
-
break
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return dir
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function getModuleDir (pkgName, base) {
|
|
21
|
-
if (pkgName === 'main') return resolvePath(process.env.BAJOCWD)
|
|
22
|
-
if (base === 'main') base = process.env.BAJOCWD
|
|
23
|
-
else if (this && this.app && this.app[base]) base = this.app[base].pkgName
|
|
24
|
-
const pkgPath = pkgName + '/package.json'
|
|
25
|
-
const paths = require.resolve.paths(pkgPath)
|
|
26
|
-
const gdir = getGlobalModuleDir()
|
|
27
|
-
paths.unshift(gdir)
|
|
28
|
-
paths.unshift(resolvePath(path.join(process.env.BAJOCWD, 'node_modules')))
|
|
29
|
-
let dir = findDeep(pkgPath, paths)
|
|
30
|
-
if (base && !dir) dir = findDeep(`${base}/node_modules/${pkgPath}`, paths)
|
|
31
|
-
if (!dir) return null
|
|
32
|
-
return resolvePath(path.dirname(dir))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export default getModuleDir
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
|
|
3
|
-
function getPluginDataDir (name, ensureDir = true) {
|
|
4
|
-
const { getPlugin } = this.app.bajo
|
|
5
|
-
const plugin = getPlugin(name)
|
|
6
|
-
const dir = `${this.app.bajo.dir.data}/plugins/${plugin.name}`
|
|
7
|
-
if (ensureDir) fs.ensureDirSync(dir)
|
|
8
|
-
return dir
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default getPluginDataDir
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import breakNsPath from './break-ns-path.js'
|
|
2
|
-
import currentLoc from '../../../lib/current-loc.js'
|
|
3
|
-
import lodash from 'lodash'
|
|
4
|
-
|
|
5
|
-
const { trim } = lodash
|
|
6
|
-
|
|
7
|
-
function getPluginFile (file) {
|
|
8
|
-
if (!this) return file
|
|
9
|
-
if (file[0] === '.') file = `${currentLoc(import.meta).dir}/${trim(file.slice(1), '/')}`
|
|
10
|
-
if (!this) return file
|
|
11
|
-
if (file.includes(':')) {
|
|
12
|
-
if (file.slice(1, 2) === ':') return file // windows fs
|
|
13
|
-
const { ns, path } = breakNsPath.call(this, file)
|
|
14
|
-
if (ns !== 'file' && this && this.app && this.app[ns] && ns.length > 1) {
|
|
15
|
-
file = `${this.app[ns].dir.pkg}${path}`
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return file
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default getPluginFile
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import BajoPlugin from '../../bajo-plugin.js'
|
|
2
|
-
|
|
3
|
-
function getPlugin (name, silent) {
|
|
4
|
-
if (!this.app[name]) {
|
|
5
|
-
// alias?
|
|
6
|
-
let plugin
|
|
7
|
-
for (const key in this.app) {
|
|
8
|
-
const item = this.app[key]
|
|
9
|
-
if (item instanceof BajoPlugin && (item.alias === name || item.pkgName === name)) {
|
|
10
|
-
plugin = item
|
|
11
|
-
break
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
if (!plugin) {
|
|
15
|
-
if (silent) return false
|
|
16
|
-
throw this.error('pluginWithAliasNotLoaded%s', name)
|
|
17
|
-
}
|
|
18
|
-
name = plugin.name
|
|
19
|
-
}
|
|
20
|
-
return this.app[name]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default getPlugin
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import resolvePath from './resolve-path.js'
|
|
2
|
-
import getPluginFile from './get-plugin-file.js'
|
|
3
|
-
import lodash from 'lodash'
|
|
4
|
-
import fs from 'fs-extra'
|
|
5
|
-
|
|
6
|
-
const { isFunction, isPlainObject } = lodash
|
|
7
|
-
|
|
8
|
-
async function load (file, asDefaultImport = true, noCache = false) {
|
|
9
|
-
file = resolvePath(file, true)
|
|
10
|
-
if (noCache) file += `?_=${Date.now()}`
|
|
11
|
-
const imported = await import(file)
|
|
12
|
-
if (asDefaultImport) return imported.default
|
|
13
|
-
return imported
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
|
|
17
|
-
const me = this
|
|
18
|
-
file = getPluginFile.call(me, file)
|
|
19
|
-
if (!fs.existsSync(file)) return
|
|
20
|
-
let mod = await load(file, asDefaultImport, noCache)
|
|
21
|
-
if (!asHandler) return mod
|
|
22
|
-
if (isFunction(mod)) mod = { level: 999, handler: mod }
|
|
23
|
-
if (!isPlainObject(mod)) throw this.error.call(me.app, 'File \'%s\' is NOT a handler module', file)
|
|
24
|
-
return mod
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default importModule
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import os from 'os'
|
|
3
|
-
import getModuleDir from './get-module-dir.js'
|
|
4
|
-
import resolvePath from './resolve-path.js'
|
|
5
|
-
import readJson from './read-json.js'
|
|
6
|
-
import defaultsDeep from './defaults-deep.js'
|
|
7
|
-
import breakNsPath from './break-ns-path.js'
|
|
8
|
-
import path from 'path'
|
|
9
|
-
import fs from 'fs-extra'
|
|
10
|
-
|
|
11
|
-
const { isPlainObject, last, isEmpty, has, keys, values, get } = lodash
|
|
12
|
-
|
|
13
|
-
async function importPkg (...pkgs) {
|
|
14
|
-
const result = {}
|
|
15
|
-
const notFound = []
|
|
16
|
-
let opts = { returnDefault: true, thrownNotFound: false }
|
|
17
|
-
if (isPlainObject(last(pkgs))) {
|
|
18
|
-
opts = defaultsDeep(pkgs.pop(), opts)
|
|
19
|
-
}
|
|
20
|
-
for (const pkg of pkgs) {
|
|
21
|
-
const { ns, path: name } = breakNsPath.call(this, pkg)
|
|
22
|
-
const dir = getModuleDir.call(this, name, ns)
|
|
23
|
-
if (!dir) {
|
|
24
|
-
notFound.push(pkg)
|
|
25
|
-
continue
|
|
26
|
-
}
|
|
27
|
-
const p = readJson(`${dir}/package.json`, opts.thrownNotFound)
|
|
28
|
-
const mainFileOrg = dir + '/' + (p.main ?? get(p, 'exports.default', 'index.js'))
|
|
29
|
-
let mainFile = resolvePath(mainFileOrg, os.platform() === 'win32')
|
|
30
|
-
if (isEmpty(path.extname(mainFile))) {
|
|
31
|
-
if (fs.existsSync(`${mainFileOrg}/index.js`)) mainFile += '/index.js'
|
|
32
|
-
else mainFile += '.js'
|
|
33
|
-
}
|
|
34
|
-
if (opts.noCache) mainFile += `?_=${Date.now()}`
|
|
35
|
-
let mod = await import(mainFile)
|
|
36
|
-
if (opts.returnDefault && has(mod, 'default')) {
|
|
37
|
-
mod = mod.default
|
|
38
|
-
if (opts.returnDefault && has(mod, 'default')) mod = mod.default
|
|
39
|
-
}
|
|
40
|
-
result[name] = mod
|
|
41
|
-
}
|
|
42
|
-
if (notFound.length > 0) throw this.error('cantFind%s', this.join(notFound))
|
|
43
|
-
if (pkgs.length === 1) return result[keys(result)[0]]
|
|
44
|
-
if (opts.asObject) return result
|
|
45
|
-
return values(result)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default importPkg
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import logLevels from './log-levels.js'
|
|
2
|
-
import lodash from 'lodash'
|
|
3
|
-
|
|
4
|
-
const { keys, indexOf } = lodash
|
|
5
|
-
|
|
6
|
-
function isLogInRange (level) {
|
|
7
|
-
const levels = keys(logLevels)
|
|
8
|
-
const logLevel = indexOf(levels, this.app.bajo.config.log.level)
|
|
9
|
-
return indexOf(levels, level) >= logLevel
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isLogInRange
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
import resolvePath from './resolve-path.js'
|
|
3
|
-
|
|
4
|
-
const isValidApp = (dir) => {
|
|
5
|
-
if (!dir) dir = process.env.BAJOCWD
|
|
6
|
-
dir = resolvePath(dir)
|
|
7
|
-
const hasMainDir = fs.existsSync(`${dir}/main/plugin`)
|
|
8
|
-
const hasPackageJson = fs.existsSync(`${dir}/package.json`)
|
|
9
|
-
return hasMainDir && hasPackageJson
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isValidApp
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra'
|
|
2
|
-
import resolvePath from './resolve-path.js'
|
|
3
|
-
|
|
4
|
-
const isValidPlugin = (dir) => {
|
|
5
|
-
if (!dir) dir = process.env.BAJOCWD
|
|
6
|
-
dir = resolvePath(dir)
|
|
7
|
-
const hasPluginDir = fs.existsSync(`${dir}/plugin`)
|
|
8
|
-
const hasPackageJson = fs.existsSync(`${dir}/package.json`)
|
|
9
|
-
return hasPluginDir && hasPackageJson
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default isValidPlugin
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import isSet from './is-set.js'
|
|
3
|
-
|
|
4
|
-
const { isPlainObject } = lodash
|
|
5
|
-
|
|
6
|
-
function join (array, sep) {
|
|
7
|
-
const translate = val => {
|
|
8
|
-
if (this && this.print) return this.print.write(val).toLowerCase()
|
|
9
|
-
return val
|
|
10
|
-
}
|
|
11
|
-
if (array.length === 0) return translate('none')
|
|
12
|
-
if (array.length === 1) return array[0]
|
|
13
|
-
if (isSet(sep) && !isPlainObject(sep)) return array.join(sep)
|
|
14
|
-
let { separator = ', ', joiner = 'and' } = sep ?? {}
|
|
15
|
-
joiner = translate(joiner)
|
|
16
|
-
const last = (array.pop() ?? '').trim()
|
|
17
|
-
return array.map(a => (a + '').trim()).join(separator) + ` ${joiner} ${last}`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default join
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
|
|
3
|
-
const { isEmpty } = lodash
|
|
4
|
-
|
|
5
|
-
function numUnit (value = '', defUnit = '') {
|
|
6
|
-
const num = value.match(/\d+/g)
|
|
7
|
-
const unit = value.match(/[a-zA-Z]+/g)
|
|
8
|
-
return `${num[0]}${isEmpty(unit) ? defUnit : unit[0]}`
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default numUnit
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
|
|
3
|
-
const { isEmpty, isPlainObject, orderBy, forOwn } = lodash
|
|
4
|
-
|
|
5
|
-
function paginate (collection, { page = 1, limit = 25, sort } = {}) {
|
|
6
|
-
const count = collection.length
|
|
7
|
-
const offset = (page - 1) * limit
|
|
8
|
-
const fields = []
|
|
9
|
-
const dirs = []
|
|
10
|
-
if (isPlainObject(sort)) {
|
|
11
|
-
forOwn(sort, (v, k) => {
|
|
12
|
-
fields.push(k)
|
|
13
|
-
dirs.push(v < 0 ? 'desc' : 'asc')
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
|
|
17
|
-
const data = collection.slice(offset, offset + limit)
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
data,
|
|
21
|
-
page,
|
|
22
|
-
limit,
|
|
23
|
-
count,
|
|
24
|
-
pages: Math.ceil(collection.length / limit)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export default paginate
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
|
-
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
3
|
-
import ms from 'ms'
|
|
4
|
-
import dayjs from '../../../lib/dayjs.js'
|
|
5
|
-
import isSet from './is-set.js'
|
|
6
|
-
|
|
7
|
-
const { isPlainObject, isArray, isNumber, set, cloneDeep, isString, omit } = lodash
|
|
8
|
-
const statics = ['*']
|
|
9
|
-
|
|
10
|
-
function parseDur (val) {
|
|
11
|
-
return isNumber(val) ? val : ms(val)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function parseDt (val) {
|
|
15
|
-
const dt = dayjs(val)
|
|
16
|
-
if (!dt.isValid()) throw this.error('dtUnparsable%s', val)
|
|
17
|
-
return dt.toDate()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function parseObject (input, { silent = true, parseValue = false, lang, ns } = {}) {
|
|
21
|
-
let obj = cloneDeep(input)
|
|
22
|
-
const keys = Object.keys(obj)
|
|
23
|
-
const me = this
|
|
24
|
-
const mutated = []
|
|
25
|
-
keys.forEach(k => {
|
|
26
|
-
const v = obj[k]
|
|
27
|
-
if (isPlainObject(v)) obj[k] = parseObject(v)
|
|
28
|
-
else if (isArray(v)) {
|
|
29
|
-
v.forEach((i, idx) => {
|
|
30
|
-
if (isPlainObject(i)) obj[k][idx] = parseObject(i)
|
|
31
|
-
else if (statics.includes(i)) obj[k][idx] = i
|
|
32
|
-
else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
|
|
33
|
-
if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
|
|
34
|
-
})
|
|
35
|
-
} else if (isSet(v)) {
|
|
36
|
-
try {
|
|
37
|
-
if (statics.includes(v)) obj[k] = v
|
|
38
|
-
else if (k.startsWith('t:') && isString(v)) {
|
|
39
|
-
const newK = k.slice(2)
|
|
40
|
-
if (lang) {
|
|
41
|
-
const scope = ns ? me.app[ns] : me
|
|
42
|
-
const [text, ...args] = v.split('|')
|
|
43
|
-
obj[newK] = scope.print.write(text, ...args, { lang })
|
|
44
|
-
} else obj[newK] = v
|
|
45
|
-
mutated.push(k)
|
|
46
|
-
} else if (parseValue) {
|
|
47
|
-
obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
|
|
48
|
-
if (isArray(obj[k])) obj[k] = obj[k].map(item => typeof item === 'string' ? item.trim() : item)
|
|
49
|
-
}
|
|
50
|
-
if (k.slice(-3) === 'Dur') obj[k] = parseDur.call(me, v)
|
|
51
|
-
if (k.slice(-2) === 'Dt') obj[k] = parseDt.call(me, v)
|
|
52
|
-
} catch (err) {
|
|
53
|
-
obj[k] = undefined
|
|
54
|
-
if (!silent) throw err
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
})
|
|
58
|
-
if (mutated.length > 0) obj = omit(obj, mutated)
|
|
59
|
-
return obj
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export default parseObject
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import isSet from './is-set.js'
|
|
2
|
-
|
|
3
|
-
function pick (obj, items, excludeUnset) {
|
|
4
|
-
const result = {}
|
|
5
|
-
for (const item of items) {
|
|
6
|
-
const [k, nk] = item.split(':')
|
|
7
|
-
if (excludeUnset && !isSet(obj[k])) continue
|
|
8
|
-
result[nk ?? k] = obj[k]
|
|
9
|
-
}
|
|
10
|
-
return result
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default pick
|
|
@@ -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,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,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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/boot/class/bajo-plugin/{helper/collect-exit-handlers.js → collect-exit-handlers.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|