bajo 0.2.13 → 0.2.14

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,5 +1,6 @@
1
1
  import buildHelper from './lib/build-helper.js'
2
2
  import logger from './lib/logger.js'
3
+ import print from './lib/print.js'
3
4
  import fs from 'fs-extra'
4
5
  import dayjs from 'dayjs'
5
6
  import utc from 'dayjs/plugin/utc.js'
@@ -16,6 +17,7 @@ export default async function () {
16
17
  if (shallow) Object.freeze(o)
17
18
  else deepFreeze(o)
18
19
  }
20
+ this.bajo.helper.print = print.call(this)
19
21
  this.bajo.helper.log = logger.call(this)
20
22
  this.bajo.helper.dayjs = dayjs
21
23
  this.bajo.helper.setImmediate = function () {
@@ -1,5 +1,4 @@
1
1
  import { last, isPlainObject, each, isArray, get } from 'lodash-es'
2
- import print from './print.js'
3
2
  import getPluginName from './get-plugin-name.js'
4
3
 
5
4
  /**
@@ -18,21 +17,23 @@ import getPluginName from './get-plugin-name.js'
18
17
  Error.stackTraceLimit = 15
19
18
 
20
19
  function formatErrorDetails (value, ns) {
20
+ const { print } = this.bajo.helper
21
21
  each(value, (v, i) => {
22
22
  if (!v.context) {
23
- v.error = print._format.call(this, ns, v.error, {})
23
+ v.error = print.__(v.error, { ns })
24
24
  return undefined
25
25
  }
26
26
  v.context.message = v.message
27
- if (v.type === 'any.only') v.context.ref = print._format.call(this, ns, `field.${get(v, 'context.valids.0.key')}`, {})
27
+ if (v.type === 'any.only') v.context.ref = print.__(`field.${get(v, 'context.valids.0.key')}`, { ns })
28
28
  value[i] = {
29
29
  field: get(v, 'context.key'),
30
- error: print._format.call(this, ns, `validation.${v.type}`, v.context, {})
30
+ error: print.__(`validation.${v.type}`, v.context, { ns })
31
31
  }
32
32
  })
33
33
  }
34
34
 
35
35
  function error (msg = 'Internal server error', ...args) {
36
+ const { print } = this.bajo.helper
36
37
  let payload = last(args)
37
38
  let ns
38
39
  if (isPlainObject(payload)) {
@@ -41,7 +42,7 @@ function error (msg = 'Internal server error', ...args) {
41
42
  }
42
43
  if (!ns) ns = getPluginName.call(this, 3)
43
44
  const orgMsg = msg
44
- const message = print._format.call(this, ns, msg, ...args)
45
+ const message = print.__(msg, ...args, { ns })
45
46
  let err
46
47
  if (isPlainObject(payload) && payload.class) err = payload.class(message)
47
48
  else err = Error(message)
@@ -1,23 +1,11 @@
1
- /**
2
- * test test
3
- *
4
- * @kind function
5
- * @name logger
6
- * @returns {Object}
7
- *
8
- */
9
-
10
- import print from '../helper/print.js'
11
1
  import os from 'os'
12
- import { keys, each, isEmpty, without, merge, upperFirst, isString } from 'lodash-es'
2
+ import { isEmpty, without, merge, upperFirst, isPlainObject } from 'lodash-es'
13
3
  // import pretty from 'prettyjson'
14
4
  import getPluginName from '../helper/get-plugin-name.js'
15
5
  import levels from '../helper/log-levels.js'
16
6
  import isLogInRange from '../helper/is-log-in-range.js'
17
7
  import dayjs from 'dayjs'
18
8
 
19
- const levelList = keys(levels)
20
-
21
9
  /*
22
10
  const prettyOpts = {
23
11
  noAlign: true,
@@ -26,50 +14,64 @@ const prettyOpts = {
26
14
  }
27
15
  */
28
16
 
29
- export default function logger () {
30
- const config = this.bajo.config
31
- // const format = config.log.dateFormat
32
- const format = 'YYYY-MM-DDTHH:MM:ss.SSS[Z]'
33
- const log = {}
34
- const self = this
35
- log.child = () => {
36
- if (!config.log.logger) config.log.logger = 'bajoLogger'
37
- if (self[config.log.logger] && self[config.log.logger].logger) return self[config.log.logger].logger.child()
38
- return self
17
+ class Log {
18
+ constructor (scope) {
19
+ this.scope = scope
20
+ this.config = scope.bajo.config
21
+ this.bajoLog = this.config.log.logger ?? 'bajoLogger'
22
+ this.format = 'YYYY-MM-DDTHH:MM:ss.SSS[Z]'
39
23
  }
40
- each(levelList, l => {
41
- log[l] = (...params) => {
42
- const config = this.bajo.config
43
- if (config.log.level === 'silent') return
44
- if (!isLogInRange.call(this, l)) return
45
- let [data, msg, ...args] = params
46
- if (isString(data)) {
47
- args.unshift(msg)
48
- msg = data
49
- data = null
50
- }
51
- args = without(args, undefined)
52
- const pkg = getPluginName.call(this)
53
- msg = print._format(pkg, `[%s] ${msg}`, pkg, ...args)
54
- const bajoLog = config.log.logger ?? 'bajoLogger'
55
- if (this[bajoLog] && this[bajoLog].logger) {
56
- this[bajoLog].logger[l](data, msg, ...args)
24
+
25
+ isExtLogger () {
26
+ return this.scope[this.bajoLog] && this.scope[this.bajoLog].logger
27
+ }
28
+
29
+ child () {
30
+ if (this.isExtLogger()) return this.scope[this.bajoLog].logger.child()
31
+ return this.scope
32
+ }
33
+
34
+ formatMsg (level, params) {
35
+ const { print } = this.scope.bajo.helper
36
+ let opts = {}
37
+ if (isPlainObject(params.slice(-1)[0])) opts = params.pop()
38
+ if (this.config.log.level === 'silent') return
39
+ if (!isLogInRange.call(this.scope, level)) return
40
+ let [data, msg, ...args] = params
41
+ if (typeof data === 'string') {
42
+ args.unshift(msg)
43
+ msg = data
44
+ data = null
45
+ }
46
+ args = without(args, undefined)
47
+ const pkg = opts.pkg ?? getPluginName.call(this.scope, 3)
48
+ msg = print.__(`[%s] ${msg}`, pkg, ...args)
49
+ if (this.scope[this.bajoLog] && this.scope[this.bajoLog].logger) {
50
+ this.scope[this.bajoLog].logger[level](data, msg, ...args)
51
+ } else {
52
+ let text
53
+ const dt = new Date()
54
+ if (this.config.env === 'prod') {
55
+ const json = { level: levels[level], time: dt.valueOf(), pid: process.pid, hostname: os.hostname() }
56
+ if (!isEmpty(data)) merge(json, data)
57
+ merge(json, { msg })
58
+ text = JSON.stringify(json)
57
59
  } else {
58
- let text
59
- const dt = new Date()
60
- if (config.env === 'prod') {
61
- const json = { level: levels[l], time: dt.valueOf(), pid: process.pid, hostname: os.hostname() }
62
- if (!isEmpty(data)) merge(json, data)
63
- merge(json, { msg })
64
- text = JSON.stringify(json)
65
- } else {
66
- text = `[${dayjs(dt).utc(true).format(format)}] ${upperFirst(l)}: ${msg}`
67
- // if (!isEmpty(data)) text += '\n ' + (pretty.render(data, prettyOpts).split('\n').join('\n '))
68
- if (!isEmpty(data)) text += '\n' + JSON.stringify(data)
69
- }
70
- console.log(text)
60
+ text = `[${dayjs(dt).utc(true).format(this.format)}] ${upperFirst(level)}: ${msg}`
61
+ // if (!isEmpty(data)) text += '\n ' + (pretty.render(data, prettyOpts).split('\n').join('\n '))
62
+ if (!isEmpty(data)) text += '\n' + JSON.stringify(data)
71
63
  }
64
+ console.log(text)
72
65
  }
73
- })
74
- return log
66
+ }
67
+ }
68
+
69
+ Object.keys(levels).forEach(level => {
70
+ Log.prototype[level] = function (...params) {
71
+ this.formatMsg(level, params)
72
+ }
73
+ })
74
+
75
+ export default function logger () {
76
+ return new Log(this)
75
77
  }
@@ -0,0 +1,82 @@
1
+ import bora from './bora.js'
2
+ import Sprintf from 'sprintf-js'
3
+ import { isPlainObject, get, isEmpty } from 'lodash-es'
4
+ import defaultsDeep from '../helper/defaults-deep.js'
5
+ import getPluginName from '../helper/get-plugin-name.js'
6
+
7
+ const { sprintf } = Sprintf
8
+
9
+ class Print {
10
+ constructor (scope) {
11
+ this.scope = scope
12
+ }
13
+
14
+ _prep (params) {
15
+ const [msg, ...args] = params
16
+ let opts = {}
17
+ if (isPlainObject(args.slice(-1)[0])) opts = args.pop()
18
+ opts = defaultsDeep(opts, { type: 'bora', exit: false, skipSilent: false })
19
+ this.opts = opts
20
+ this.opts.ns = this.opts.ns ?? ['bajoI18N']
21
+ this.opts.pkg = this.opts.pkg ?? getPluginName.call(this.scope, 2)
22
+ this.args = args
23
+ this.msg = msg
24
+ }
25
+
26
+ formatMsg (params) {
27
+ this._prep(params)
28
+ if (isEmpty(this.msg)) return ''
29
+ const transHandler = get(this, 'scope.bajo.transHandler')
30
+ const dayjs = get(this, 'scope.bajo.helper')
31
+ if (transHandler) this.msg = transHandler.call(this, { msg: this.msg, params: this.args, options: this.opts })
32
+ else this.msg = sprintf(this.msg, ...this.args)
33
+ if (this.opts.showDatetime && dayjs) this.msg = `[${dayjs().toISOString()}] ${this.msg}`
34
+ return this.msg
35
+ }
36
+
37
+ __ (...params) {
38
+ return this.formatMsg(params)
39
+ }
40
+
41
+ output (params, boraMethod = 'succeed', logMethod = 'info', consoleMethod = 'log') {
42
+ this.formatMsg(params)
43
+ if (isEmpty(this.msg)) return ''
44
+ switch (this.opts.type) {
45
+ case 'bora': bora.call(this.scope, this.opts.ns, this.opts)[boraMethod](this.msg, ...this.args); break
46
+ case 'log': this.scope.bajo.helper.log[logMethod](this.msg, ...this.args, this.opts); break
47
+ default: console[consoleMethod](this.msg)
48
+ }
49
+ if (this.opts.exit) process.exit()
50
+ }
51
+
52
+ fail (...params) {
53
+ this.output(params, 'fail', 'error', 'error')
54
+ }
55
+
56
+ succeed (...params) {
57
+ this.output(params, 'succeed', 'info')
58
+ }
59
+
60
+ warn (...params) {
61
+ this.output(params, 'warn', 'warn')
62
+ }
63
+
64
+ info (...params) {
65
+ this.output(params, 'info')
66
+ }
67
+
68
+ fatal (...params) {
69
+ this.output(params, 'fatal', 'error', 'error')
70
+ process.exit(1)
71
+ }
72
+
73
+ bora (...params) {
74
+ let ns = getPluginName.call(this.scope, 2)
75
+ if (ns === 'bajo') ns = 'bajoI18N'
76
+ return bora.call(this.scope, ns, ...params)
77
+ }
78
+ }
79
+
80
+ export default function () {
81
+ return new Print(this)
82
+ }
@@ -1,4 +1,4 @@
1
- import { get, camelCase, upperFirst, map } from 'lodash-es'
1
+ import { set, get, camelCase, upperFirst, map } from 'lodash-es'
2
2
 
3
3
  async function run ({ singles }) {
4
4
  const { runHook, log, eachPlugins, importModule, freeze, getConfig, print } = this.bajo.helper
@@ -18,6 +18,7 @@ async function run ({ singles }) {
18
18
  await runHook(`bajo:${camelCase(`after ${f} ${plugin}`)}`)
19
19
  }
20
20
  if (f === 'init') freeze(this[plugin].config)
21
+ set(this, `${plugin}.state.${f}`, true)
21
22
  })
22
23
  await runHook(`bajo:${camelCase(`after ${f} all plugins`)}`)
23
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {
@@ -1,102 +0,0 @@
1
- import bora from '../lib/bora.js'
2
- import Sprintf from 'sprintf-js'
3
- import { last, isPlainObject, get, merge, upperFirst } from 'lodash-es'
4
- import defaultsDeep from './defaults-deep.js'
5
- import getPluginName from './get-plugin-name.js'
6
-
7
- const { sprintf } = Sprintf
8
-
9
- function prep (args) {
10
- let opts = { type: 'bora', exit: false, skipSilent: false }
11
- const l = last(args)
12
- if (isPlainObject(l)) opts = defaultsDeep(args.pop(), opts)
13
- const [pkg, msg, ...params] = args
14
- opts.pkg = pkg
15
- opts.ns = pkg === 'bajo' ? 'bajoI18N' : pkg
16
- return { ns: opts.ns, pkg, msg, params, opts }
17
- }
18
-
19
- function format (...args) {
20
- const { ns, msg, params, opts } = prep(args)
21
- if (!msg) return ''
22
- const i18n = get(this, 'bajoI18N.instance')
23
- const dayjs = get(this, 'bajo.helper')
24
- if (i18n) {
25
- if (isPlainObject(params[0])) {
26
- const ctx = merge({}, params[0] ?? {}, { ns })
27
- if (msg.startsWith('validation') && ctx.message && !i18n.exists(msg, { ns })) {
28
- const message = ctx.message
29
- .replace(/".*?" /, '')
30
- .replace(/^is /, '')
31
- return upperFirst(message)
32
- }
33
- if (msg === 'field.username') console.log(ctx)
34
- return i18n.t(msg, ctx)
35
- }
36
- return i18n.t(msg, { ns, pkg: opts.pkg, postProcess: 'sprintf', sprintf: params })
37
- }
38
- let text = sprintf(msg, ...params)
39
- if (opts.showDatetime && dayjs) text = `[${dayjs().toISOString()}] ${text}`
40
- return text
41
- }
42
-
43
- const print = {
44
- __: function (...args) {
45
- const [msg, ...params] = args
46
- const pkg = getPluginName.call(this, 2)
47
- return format.call(this, pkg, msg, ...params)
48
- },
49
- _format: format,
50
- fail: function (...args) {
51
- args.unshift(getPluginName.call(this, 2))
52
- const { ns, opts, msg, params } = prep(args)
53
- if (msg) {
54
- if (opts.type === 'bora') bora.call(this, ns, opts).fail(msg, ...params)
55
- else console.error(format.call(this, ns, msg, ...params))
56
- }
57
- if (opts.exit) process.exit(1)
58
- },
59
- succeed: function (...args) {
60
- args.unshift(getPluginName.call(this, 2))
61
- const { ns, opts, msg, params } = prep(args)
62
- if (msg) {
63
- if (opts.type === 'bora') bora.call(this, ns, opts).succeed(msg, ...params)
64
- else console.log(format.call(this, ns, msg, ...params))
65
- }
66
- if (opts.exit) process.exit(0)
67
- },
68
- warn: function (...args) {
69
- args.unshift(getPluginName.call(this, 2))
70
- const { ns, opts, msg, params } = prep(args)
71
- if (msg) {
72
- if (opts.type === 'bora') bora.call(this, ns, opts).warn(msg, ...params)
73
- else console.log(format.call(this, ns, msg, ...params))
74
- }
75
- if (opts.exit) process.exit(0)
76
- },
77
- info: function (...args) {
78
- args.unshift(getPluginName.call(this, 2))
79
- const { ns, opts, msg, params } = prep(args)
80
- if (msg) {
81
- if (opts.type === 'bora') bora.call(this, ns, opts).info(msg, ...params)
82
- else console.log(format.call(this, ns, msg, ...params))
83
- }
84
- if (opts.exit) process.exit(0)
85
- },
86
- fatal: function (...args) {
87
- args.unshift(getPluginName.call(this, 2))
88
- const { ns, opts, msg, params } = prep(args)
89
- if (msg) {
90
- if (opts.type === 'bora') bora.call(this, ns, opts).fatal(msg, ...params)
91
- else console.error(format.call(this, ns, msg, ...params))
92
- }
93
- process.exit(1)
94
- },
95
- bora: function (...args) {
96
- let ns = getPluginName.call(this, 2)
97
- if (ns === 'bajo') ns = 'bajoI18N'
98
- return bora.call(this, ns, ...args)
99
- }
100
- }
101
-
102
- export default print