bajo 0.2.6 → 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', '')
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.2.6",
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": {