bajo 0.2.5 → 0.2.7

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.
@@ -1,20 +1,19 @@
1
- import { cloneDeep, isPlainObject } from 'lodash-es'
2
1
  import { customAlphabet } from 'nanoid'
3
2
 
4
3
  const generateId = (options = {}) => {
5
- let { pattern, length = 21, returnInstance } = options
6
- let opts = {}
7
- if (isPlainObject(pattern)) {
8
- opts = cloneDeep(pattern)
9
- returnInstance = opts.returnInstance
10
- length = opts.length
11
- pattern = opts.pattern
4
+ let type
5
+ if (options === 'int') {
6
+ type = options
7
+ options = { pattern: '0123456789', length: 15 }
12
8
  }
9
+ let { pattern, length = 13, returnInstance } = options
13
10
  pattern = pattern ?? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
14
- if (opts.lowerCase) pattern = pattern.toLowerCase()
15
- else if (opts.upperCase) pattern = pattern.toUpperCase()
11
+ if (options.case === 'lower') pattern = pattern.toLowerCase()
12
+ else if (options.case === 'upper') pattern = pattern.toUpperCase()
16
13
  const nid = customAlphabet(pattern, length)
17
- return returnInstance ? nid : nid()
14
+ if (returnInstance) return nid
15
+ const value = nid()
16
+ return type === 'int' ? parseInt(value) : value
18
17
  }
19
18
 
20
19
  export default generateId
@@ -0,0 +1,15 @@
1
+ // taken from: https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript
2
+
3
+ function secToHms (secs) {
4
+ const secNum = parseInt(secs, 10)
5
+ const hours = Math.floor(secNum / 3600)
6
+ const minutes = Math.floor(secNum / 60) % 60
7
+ const seconds = secNum % 60
8
+
9
+ return [hours, minutes, seconds]
10
+ .map(v => v < 10 ? '0' + v : v)
11
+ .filter((v, i) => v !== '00' || i > 0)
12
+ .join(':')
13
+ }
14
+
15
+ export default secToHms
package/boot/lib/bora.js CHANGED
@@ -13,11 +13,13 @@ class Bora {
13
13
  this.opts = opts
14
14
  this.ora = ora(this.opts)
15
15
  this.args = args
16
+ this.startTime = null
16
17
  }
17
18
 
18
19
  setScope (scope) {
19
20
  this.scope = scope
20
- const { getConfig } = this.scope.bajo.helper
21
+ const { getConfig, dayjs } = this.scope.bajo.helper
22
+ this.startTime = dayjs()
21
23
  const config = getConfig()
22
24
  let silent = !!config.silent
23
25
  if (this.opts.skipSilent) silent = false
@@ -27,12 +29,15 @@ class Bora {
27
29
  }
28
30
 
29
31
  setText (text, ...args) {
32
+ const { dayjs, secToHms } = this.scope.bajo.helper
30
33
  if (isString(text)) {
31
34
  const i18n = get(this, 'scope.bajoI18N.instance')
32
35
  if (i18n) {
33
36
  if (isPlainObject(args[0])) text = i18n.t(text, args[0])
34
37
  else text = i18n.t(text, { ns: this.ns, postProcess: 'sprintf', sprintf: args })
35
38
  } else text = sprintf(text, ...args)
39
+ const elapsed = dayjs().diff(this.startTime, 'second')
40
+ if (this.opts.showCounter) text = `[${secToHms(elapsed)}] ${text}`
36
41
  this.ora.text = text
37
42
  }
38
43
  return this
@@ -1,5 +1,5 @@
1
1
  import fastGlob from 'fast-glob'
2
- import { map, without, camelCase, isFunction, isPlainObject, forOwn } from 'lodash-es'
2
+ import { camelCase, isFunction, isPlainObject, forOwn } from 'lodash-es'
3
3
  import resolvePath from '../helper/resolve-path.js'
4
4
  import importModule from '../helper/import-module.js'
5
5
 
@@ -30,11 +30,9 @@ const wrapAsyncFn = function (name, handler, bind) {
30
30
  }
31
31
  }
32
32
 
33
- export default async function (dir, { pkg = 'bajo', exclude = [] } = {}) {
33
+ export default async function (dir, pkg = 'bajo') {
34
34
  dir = resolvePath(dir)
35
- exclude = map(exclude, e => `${dir}/${e}`)
36
- let files = await fastGlob(`${dir}/**/*.js`)
37
- files = without(files, ...exclude)
35
+ const files = await fastGlob([`!${dir}/**/_*.js`, `${dir}/**/*.js`])
38
36
  const helper = {}
39
37
  for (const f of files) {
40
38
  const base = f.replace(dir, '').replace('.js', '')
@@ -6,6 +6,7 @@ import dotenvParseVariables from 'dotenv-parse-variables'
6
6
  import importModule from '../helper/import-module.js'
7
7
  import { find, each, set, camelCase, forOwn } from 'lodash-es'
8
8
  import fs from 'fs-extra'
9
+ import path from 'path'
9
10
  import currentLoc from '../helper/current-loc.js'
10
11
 
11
12
  const { unflatten } = flat
@@ -14,7 +15,7 @@ const parseItem = (data, delimiter) => {
14
15
  return unflatten(data, {
15
16
  delimiter,
16
17
  safe: true,
17
- overwrite: true,
18
+ overwrite: true
18
19
  })
19
20
  }
20
21
 
@@ -28,10 +29,8 @@ const parseWithParser = async () => {
28
29
 
29
30
  const parseWithYargs = async () => {
30
31
  const parser = './app/bajo/argv-parser.js'
31
- if (fs.existsSync(parser)) {
32
- const mod = await importModule(parser)
33
- return await mod(yargs)
34
- }
32
+ const mod = await importModule(parser)
33
+ if (mod) return await mod(yargs)
35
34
  const pkg = fs.readJSONSync(`${currentLoc(import.meta).dir}/../../package.json`)
36
35
  let name = `node ${pkg.main}`
37
36
  if (pkg.bin) name = path.basename(pkg.bin, '.js')
@@ -47,7 +46,7 @@ const parseWithYargs = async () => {
47
46
  .version().alias('version', 'v')
48
47
  .help().alias('help', 'h')
49
48
  .alias('tool', 't')
50
- if (pkg.homepage) cli.epilog(`For more information please visit ${pkg.homepage}`)
49
+ if (pkg.homepage) cli.epilog(`For more information please visit ${pkg.homepage}`)
51
50
  return cli.argv
52
51
  }
53
52
 
@@ -4,7 +4,7 @@ import { keys } from 'lodash-es'
4
4
  async function runner (plugin, pkg) {
5
5
  const { log, freeze } = this.bajo.helper
6
6
  const dir = pkg === 'app' ? (this.bajo.config.dir.base + '/app') : this.bajo.helper.getModuleDir(pkg)
7
- this[plugin].helper = await buildHelper.call(this, `${dir}/bajo/helper`, { pkg })
7
+ this[plugin].helper = await buildHelper.call(this, `${dir}/bajo/helper`, pkg)
8
8
  freeze(this[plugin].helper, true)
9
9
  log.trace('Attach helper: %s (%d)', plugin, keys(this[plugin].helper).length)
10
10
  }
@@ -1,5 +1,4 @@
1
1
  import { isFunction, isPlainObject, map } from 'lodash-es'
2
- import fs from 'fs-extra'
3
2
 
4
3
  async function collectConfigHandlers (pkg) {
5
4
  const { getModuleDir, importModule, log } = this.bajo.helper
@@ -10,13 +9,11 @@ async function collectConfigHandlers (pkg) {
10
9
  } catch (err) {}
11
10
  if (!dir) continue
12
11
  const file = `${dir}/bajo/extend/read-config.js`
13
- if (!fs.existsSync(file)) continue
14
- try {
15
- let mod = await importModule(file)
16
- if (isFunction(mod)) mod = await mod.call(this)
17
- if (isPlainObject(mod)) mod = [mod]
18
- this.bajo.configHandlers.concat(mod)
19
- } catch (err) {}
12
+ let mod = await importModule(file)
13
+ if (!mod) continue
14
+ if (isFunction(mod)) mod = await mod.call(this)
15
+ if (isPlainObject(mod)) mod = [mod]
16
+ this.bajo.configHandlers.concat(mod)
20
17
  }
21
18
  const exts = map(this.bajo.configHandlers, 'ext')
22
19
  log.trace('Config handlers: %s', exts.join(', '))
@@ -1,5 +1,4 @@
1
1
  import {} from 'lodash-es'
2
- import fs from 'fs-extra'
3
2
 
4
3
  async function collectExitHandlers () {
5
4
  const { importModule, log, eachPlugins, getConfig, print } = this.bajo.helper
@@ -9,13 +8,10 @@ async function collectExitHandlers () {
9
8
  const names = []
10
9
  await eachPlugins(async function ({ plugin, dir }) {
11
10
  const file = `${dir}/bajo/exit.js`
12
- if (!fs.existsSync(file)) return undefined
13
- try {
14
- const mod = await importModule(file)
15
- this.bajo.exitHandler[plugin] = mod
16
- names.push(plugin)
17
- } catch (err) {
18
- }
11
+ const mod = await importModule(file)
12
+ if (!mod) return undefined
13
+ this.bajo.exitHandler[plugin] = mod
14
+ names.push(plugin)
19
15
  })
20
16
  log.trace('Exit handlers: %s', names.length === 0 ? print.__('none') : names.join(', '))
21
17
  }
@@ -1,5 +1,4 @@
1
1
  import { get, camelCase, upperFirst, map } from 'lodash-es'
2
- import fs from 'fs-extra'
3
2
 
4
3
  async function run ({ singles }) {
5
4
  const { runHook, log, eachPlugins, importModule, freeze, getConfig, print } = this.bajo.helper
@@ -10,12 +9,12 @@ async function run ({ singles }) {
10
9
  await runHook(`bajo:${camelCase(`before ${f} all plugins`)}`)
11
10
  await eachPlugins(async function ({ plugin, dir }) {
12
11
  const file = `${dir}/bajo/${f}.js`
13
- if (fs.existsSync(file)) {
12
+ const mod = await importModule(file)
13
+ if (mod) {
14
14
  log.debug('%s: %s', print.__(upperFirst(f)), plugin)
15
15
  await runHook(`bajo:${camelCase(`before ${f} ${plugin}`)}`)
16
- const item = await importModule(file)
17
16
  const params = f === 'start' ? ['all', true] : []
18
- await item.call(this, ...params)
17
+ await mod.call(this, ...params)
19
18
  await runHook(`bajo:${camelCase(`after ${f} ${plugin}`)}`)
20
19
  }
21
20
  if (f === 'init') freeze(this[plugin].config)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {