bajo 2.18.0 → 2.19.0
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/class/_helper.js +22 -7
- package/class/app.js +59 -45
- package/class/bajo.js +150 -129
- package/class/base.js +3 -3
- package/class/cache.js +60 -0
- package/class/err.js +14 -11
- package/class/log.js +41 -40
- package/class/plugin.js +35 -36
- package/class/print.js +54 -51
- package/class/tools.js +3 -4
- package/docs/App.html +7 -7
- package/docs/Bajo.html +2 -2
- package/docs/Base.html +1 -1
- package/docs/Cache.html +3 -0
- package/docs/Err.html +2 -2
- package/docs/Log.html +2 -2
- package/docs/Plugin.html +1 -1
- package/docs/Print.html +1 -1
- package/docs/Tools.html +3 -0
- package/docs/class__helper.js.html +694 -0
- package/docs/class_app.js.html +307 -149
- package/docs/class_bajo.js.html +316 -464
- package/docs/class_base.js.html +35 -32
- package/docs/class_cache.js.html +150 -0
- package/docs/class_err.js.html +144 -0
- package/docs/class_log.js.html +270 -0
- package/docs/class_plugin.js.html +98 -71
- package/docs/class_print.js.html +261 -0
- package/docs/class_tools.js.html +44 -0
- package/docs/data/search.json +1 -1
- package/docs/global.html +1 -4
- package/docs/index.html +1 -1
- package/docs/index.js.html +21 -14
- package/docs/lib_find-deep.js.html +27 -0
- package/docs/lib_formats.js.html +19 -19
- package/docs/lib_freeze.js.html +19 -0
- package/docs/lib_import-module.js.html +16 -14
- package/docs/lib_index.js.html +9 -0
- package/docs/lib_log-levels.js.html +2 -2
- package/docs/module-Helper.html +3 -0
- package/docs/module-Lib.html +3 -8
- package/docs/scripts/core.js +477 -476
- package/docs/scripts/resize.js +36 -36
- package/docs/scripts/search.js +105 -105
- package/docs/scripts/third-party/fuse.js +1 -1
- package/docs/scripts/third-party/hljs-line-num-original.js +285 -282
- package/docs/scripts/third-party/hljs-line-num.js +1 -1
- package/docs/scripts/third-party/hljs-original.js +1202 -1195
- package/docs/scripts/third-party/hljs.js +1 -1
- package/docs/scripts/third-party/popper.js +1 -1
- package/docs/scripts/third-party/tippy.js +1 -1
- package/docs/scripts/third-party/tocbot.js +509 -508
- package/index.js +8 -11
- package/lib/find-deep.js +3 -3
- package/lib/formats.js +17 -17
- package/lib/freeze.js +3 -3
- package/lib/import-module.js +8 -8
- package/package.json +1 -1
- package/test/app.test.js +183 -0
- package/test/bajo.test.js +125 -0
- package/test/base.test.js +74 -107
- package/test/cache.test.js +94 -0
- package/test/e2e.test.js +137 -0
- package/test/err.test.js +73 -0
- package/test/helper.test.js +39 -0
- package/test/import-module.test.js +138 -0
- package/test/integration.test.js +218 -0
- package/test/log.test.js +119 -0
- package/test/plugin.test.js +116 -0
- package/test/print.test.js +100 -0
- package/test/tools.test.js +38 -0
- package/wiki/CHANGES.md +5 -0
- package/.mocharc.json +0 -4
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
<!DOCTYPE html><html lang="en" style="font-size:16px"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source: class/_helper.js</title><!--[if lt IE 9]>
|
|
2
|
+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
3
|
+
<![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">Bajo API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="App.html">App</a></div><div class="sidebar-section-children"><a href="Bajo.html">Bajo</a></div><div class="sidebar-section-children"><a href="Base.html">Base</a></div><div class="sidebar-section-children"><a href="Cache.html">Cache</a></div><div class="sidebar-section-children"><a href="Err.html">Err</a></div><div class="sidebar-section-children"><a href="Log.html">Log</a></div><div class="sidebar-section-children"><a href="Plugin.html">Plugin</a></div><div class="sidebar-section-children"><a href="Print.html">Print</a></div><div class="sidebar-section-children"><a href="Tools.html">Tools</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-events"><div>Events</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#event:bajo:afterAll%257Bmethod%257D">bajo:afterAll{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterBootComplete">bajo:afterBootComplete</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterBuildCollection">bajo:afterBuildCollection</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterCollectHooks">bajo:afterCollectHooks</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:beforeAll%257Bmethod%257D">bajo:beforeAll{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:beforeBuildCollection">bajo:beforeBuildCollection</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:after%257Bmethod%257D">{ns}:after{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:beforeAppletRun">{ns}:beforeAppletRun</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:before%257Bmethod%257D">{ns}:before{method}</a></div><div class="sidebar-section-children"><a href="module-Helper%257Bns%257D_afterAppletRun.html">Helper{ns}:afterAppletRun</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-modules"><div>Modules</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="module-Helper.html">Helper</a></div><div class="sidebar-section-children"><a href="module-Lib.html">Lib</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TAppConfigHandler">TAppConfigHandler</a></div><div class="sidebar-section-children"><a href="global.html#TAppEnv">TAppEnv</a></div><div class="sidebar-section-children"><a href="global.html#TBajoDataType">TBajoDataType</a></div><div class="sidebar-section-children"><a href="global.html#TBajoFormatResult">TBajoFormatResult</a></div><div class="sidebar-section-children"><a href="global.html#TBajoFormatType">TBajoFormatType</a></div><div class="sidebar-section-children"><a href="global.html#TLogJson">TLogJson</a></div><div class="sidebar-section-children"><a href="global.html#TLogLevels">TLogLevels</a></div><div class="sidebar-section-children"><a href="global.html#TNsPathPairs">TNsPathPairs</a></div><div class="sidebar-section-children"><a href="global.html#TNsPathResult">TNsPathResult</a></div><div class="sidebar-section-children"><a href="global.html#TPrintOptions">TPrintOptions</a></div><div class="sidebar-section-children"><a href="global.html#boot">boot</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">class__helper.js</h1></header><article><pre class="prettyprint source lang-js"><code>import Print from './print.js'
|
|
4
|
+
import Log from './log.js'
|
|
5
|
+
import os from 'os'
|
|
6
|
+
import fs from 'fs-extra'
|
|
7
|
+
import lodash from 'lodash'
|
|
8
|
+
import semver from 'semver'
|
|
9
|
+
import aneka from 'aneka/index.js'
|
|
10
|
+
import outmatch from 'outmatch'
|
|
11
|
+
import fastGlob from 'fast-glob'
|
|
12
|
+
import { sprintf } from 'sprintf-js'
|
|
13
|
+
import dayjs from 'dayjs'
|
|
14
|
+
import utc from 'dayjs/plugin/utc.js'
|
|
15
|
+
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
|
|
16
|
+
import localizedFormat from 'dayjs/plugin/localizedFormat.js'
|
|
17
|
+
import weekOfYear from 'dayjs/plugin/weekOfYear.js'
|
|
18
|
+
import freeze from '../lib/freeze.js'
|
|
19
|
+
import findDeep from '../lib/find-deep.js'
|
|
20
|
+
import omitDeep from 'omit-deep'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Internal helpers called by Bajo and other classes. It should remains
|
|
24
|
+
* hidden and not to be imported by any program.
|
|
25
|
+
*
|
|
26
|
+
* @module Helper
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
merge, forOwn, groupBy, find, reduce, map, trim, keys, intersection, each,
|
|
31
|
+
camelCase, get, orderBy, isFunction, isPlainObject, pick, values, set, without, uniq, isEmpty
|
|
32
|
+
} = lodash
|
|
33
|
+
|
|
34
|
+
const omitted = ['spawn', 'cwd', 'name', 'alias', 'applet', 'a', 'plugins']
|
|
35
|
+
|
|
36
|
+
const defConfig = {
|
|
37
|
+
env: 'dev',
|
|
38
|
+
runtime: {
|
|
39
|
+
noWarning: false
|
|
40
|
+
},
|
|
41
|
+
log: {
|
|
42
|
+
timeTaken: false,
|
|
43
|
+
dateFormat: 'YYYY-MM-DDTHH:mm:ss.SSS',
|
|
44
|
+
useUtc: false,
|
|
45
|
+
pretty: false,
|
|
46
|
+
applet: false,
|
|
47
|
+
traceHook: false,
|
|
48
|
+
save: false,
|
|
49
|
+
rotation: {
|
|
50
|
+
cycle: 'none', // none, daily, weekly, monthly
|
|
51
|
+
compressOld: true,
|
|
52
|
+
byPlugin: false,
|
|
53
|
+
retain: 5
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
dump: {
|
|
57
|
+
depth: 2,
|
|
58
|
+
compact: false,
|
|
59
|
+
colors: true,
|
|
60
|
+
breakLength: 80,
|
|
61
|
+
caller: true,
|
|
62
|
+
frame: {
|
|
63
|
+
titleAllignment: 'center',
|
|
64
|
+
padding: 1,
|
|
65
|
+
margin: 1,
|
|
66
|
+
borderStyle: 'round'
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
lang: Intl.DateTimeFormat().resolvedOptions().lang ?? 'en-US',
|
|
70
|
+
intl: {
|
|
71
|
+
supported: ['en-US', 'id'],
|
|
72
|
+
fallback: 'en-US',
|
|
73
|
+
lookupOrder: [],
|
|
74
|
+
format: {
|
|
75
|
+
emptyValue: '',
|
|
76
|
+
datetime: { dateStyle: 'medium', timeStyle: 'short', timeZone: 'UTC' },
|
|
77
|
+
date: { dateStyle: 'medium', timeZone: 'UTC' },
|
|
78
|
+
time: { timeStyle: 'short', timeZone: 'UTC' },
|
|
79
|
+
float: { maximumFractionDigits: 2 },
|
|
80
|
+
double: { maximumFractionDigits: 5 },
|
|
81
|
+
smallint: {},
|
|
82
|
+
integer: {}
|
|
83
|
+
},
|
|
84
|
+
unitSys: {
|
|
85
|
+
'en-US': 'imperial',
|
|
86
|
+
id: 'metric'
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
exitHandler: true,
|
|
90
|
+
cache: {
|
|
91
|
+
purge: [],
|
|
92
|
+
purgeIntvDur: '5m'
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const defMain = `async function factory (pkgName) {
|
|
97
|
+
const me = this
|
|
98
|
+
|
|
99
|
+
return class Main extends this.app.baseClass.Base {
|
|
100
|
+
constructor () {
|
|
101
|
+
super(pkgName, me.app)
|
|
102
|
+
this.config = {}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export default factory
|
|
108
|
+
`
|
|
109
|
+
|
|
110
|
+
export function outmatchNs (source, pattern) {
|
|
111
|
+
const { breakNsPath } = this.bajo
|
|
112
|
+
const [src, subSrc] = source.split(':')
|
|
113
|
+
if (!subSrc) return pattern === src
|
|
114
|
+
try {
|
|
115
|
+
const { fullNs, path } = breakNsPath(pattern)
|
|
116
|
+
const isMatch = outmatch(path)
|
|
117
|
+
return src === fullNs && isMatch(subSrc)
|
|
118
|
+
} catch (err) {
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function parseObject (obj, options = {}) {
|
|
124
|
+
const me = this
|
|
125
|
+
const { ns = 'bajo', lang } = options
|
|
126
|
+
options.translator = {
|
|
127
|
+
lang,
|
|
128
|
+
prefix: 't:',
|
|
129
|
+
handler: val => {
|
|
130
|
+
const [text, ...args] = val.split('|')
|
|
131
|
+
args.push({ lang })
|
|
132
|
+
return me[ns].t(text, ...args)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return aneka.parseObject(obj, options)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
dayjs.extend(utc)
|
|
139
|
+
dayjs.extend(customParseFormat)
|
|
140
|
+
dayjs.extend(localizedFormat)
|
|
141
|
+
dayjs.extend(weekOfYear)
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @typedef {Object} TAppLib
|
|
145
|
+
* @property {Object} _ Access to {@link https://lodash.com|lodash}.
|
|
146
|
+
* @property {Object} fs Access to {@link https://github.com/jprichardson/node-fs-extra|fs-extra}.
|
|
147
|
+
* @property {Object} fastGlob Access to {@link https://github.com/mrmlnc/fast-glob|fast-glob}.
|
|
148
|
+
* @property {Object} sprintf Access to {@link https://github.com/alexei/sprintf.js|sprintf}.
|
|
149
|
+
* @property {Object} aneka Access to {@link https://github.com/ardhi/aneka|aneka}.
|
|
150
|
+
* @property {Object} outmatch Access to {@link https://github.com/axtgr/outmatch|outmatch}.
|
|
151
|
+
* @property {Object} dayjs Access to {@link https://day.js.org|dayjs} with utc & customParseFormat plugin already applied.
|
|
152
|
+
* @property {Object} freeze
|
|
153
|
+
* @property {Object} findDeep
|
|
154
|
+
* @see App
|
|
155
|
+
*/
|
|
156
|
+
export const lib = {
|
|
157
|
+
_: lodash,
|
|
158
|
+
fs,
|
|
159
|
+
fastGlob,
|
|
160
|
+
sprintf,
|
|
161
|
+
outmatch,
|
|
162
|
+
dayjs,
|
|
163
|
+
aneka,
|
|
164
|
+
freeze,
|
|
165
|
+
findDeep,
|
|
166
|
+
omitDeep
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Building bajo base config. Mostly dealing with directory setups:
|
|
171
|
+
* - determine base directory
|
|
172
|
+
* - check whether data directory is valid. If not exist, create one inside app dir
|
|
173
|
+
* - ensure data config directory is there
|
|
174
|
+
* - ensure tmp dir is there
|
|
175
|
+
* - read the list of plugins from ```.plugins``` file
|
|
176
|
+
*
|
|
177
|
+
* @async
|
|
178
|
+
* @method
|
|
179
|
+
* @memberof module:Helper
|
|
180
|
+
*/
|
|
181
|
+
export async function buildBaseConfig () {
|
|
182
|
+
// dirs
|
|
183
|
+
const { defaultsDeep, textToArray, currentLoc, resolvePath } = this.app.lib.aneka
|
|
184
|
+
this.config = defaultsDeep({}, this.app.argv._, this.app.envVars._)
|
|
185
|
+
set(this, 'dir.base', this.app.dir)
|
|
186
|
+
const path = currentLoc(import.meta).dir + '/..'
|
|
187
|
+
set(this, 'dir.pkg', resolvePath(path))
|
|
188
|
+
if (get(this, 'config.dir.data')) set(this, 'dir.data', this.config.dir.data)
|
|
189
|
+
if (!get(this, 'dir.data')) set(this, 'dir.data', `${this.dir.base}/data`)
|
|
190
|
+
this.dir.data = resolvePath(this.dir.data)
|
|
191
|
+
fs.ensureDirSync(`${this.dir.data}/config`)
|
|
192
|
+
if (!this.dir.tmp) {
|
|
193
|
+
this.dir.tmp = `${resolvePath(os.tmpdir())}/${this.ns}`
|
|
194
|
+
fs.ensureDirSync(this.dir.tmp)
|
|
195
|
+
}
|
|
196
|
+
this.pkg = await this.getPkgInfo()
|
|
197
|
+
let pluginPkgs = this.app.pluginPkgs
|
|
198
|
+
if (isEmpty(pluginPkgs)) {
|
|
199
|
+
// collect list of plugins
|
|
200
|
+
const mainPkg = await this.getPkgInfo(this.app.dir)
|
|
201
|
+
pluginPkgs = get(mainPkg, 'bajo.plugins', [])
|
|
202
|
+
if (isEmpty(pluginPkgs)) {
|
|
203
|
+
const pluginsFile = `${this.dir.data}/config/.plugins`
|
|
204
|
+
if (fs.existsSync(pluginsFile)) {
|
|
205
|
+
pluginPkgs = textToArray(fs.readFileSync(pluginsFile, 'utf8'))
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
this.app.pluginPkgs = without(uniq(pluginPkgs), this.app.mainNs)
|
|
210
|
+
this.app.pluginPkgs.push(this.app.mainNs)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Building all plugins:
|
|
215
|
+
* - load from app's pluginPkgs
|
|
216
|
+
* - iterate through the list and build related plugins
|
|
217
|
+
* - making sure main plugin is there. If not, create from template
|
|
218
|
+
* - attach these plugins to the app instance
|
|
219
|
+
*
|
|
220
|
+
* @async
|
|
221
|
+
* @memberof module:Helper
|
|
222
|
+
*/
|
|
223
|
+
export async function buildPlugins () {
|
|
224
|
+
const { resolvePath } = this.app.lib.aneka
|
|
225
|
+
this.log.trace('buildPluginsStart')
|
|
226
|
+
for (const pkg of this.app.pluginPkgs) {
|
|
227
|
+
const ns = camelCase(pkg)
|
|
228
|
+
let dir
|
|
229
|
+
if (ns === 'main') {
|
|
230
|
+
dir = `${this.dir.base}/${this.app.mainNs}`
|
|
231
|
+
fs.ensureDirSync(dir)
|
|
232
|
+
if (!fs.existsSync(`${dir}/index.js`)) {
|
|
233
|
+
fs.writeFileSync(`${dir}/index.js`, defMain, 'utf8')
|
|
234
|
+
}
|
|
235
|
+
} else dir = this.getModuleDir(pkg)
|
|
236
|
+
const factory = `${dir}/index.js`
|
|
237
|
+
if (!fs.existsSync(factory)) throw this.error('pluginPackageNotFound%s', pkg)
|
|
238
|
+
const { default: builder } = await import(resolvePath(factory, true))
|
|
239
|
+
const ClassDef = await builder.call(this, pkg)
|
|
240
|
+
const plugin = new ClassDef()
|
|
241
|
+
if (!(plugin instanceof this.app.baseClass.Base)) throw this.error('pluginPackageInvalid%s', pkg)
|
|
242
|
+
plugin.pkg = plugin.getPkgInfo(ns === 'main' ? this.dir.base : dir)
|
|
243
|
+
plugin.alias = ns === 'main' ? this.app.mainNs : get(plugin.pkg, 'bajo.alias', (pkg.slice(0, 5) === 'bajo-' ? pkg.slice(5) : ns).toLowerCase())
|
|
244
|
+
plugin.dependencies = get(plugin.pkg, 'bajo.dependencies', [])
|
|
245
|
+
this.app.addPlugin(plugin, ClassDef)
|
|
246
|
+
this.log.trace('- ' + pkg)
|
|
247
|
+
}
|
|
248
|
+
this.log.debug('buildPluginsComplete')
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Collect all config handlers, including the one provided by plugins
|
|
253
|
+
*
|
|
254
|
+
* @async
|
|
255
|
+
* @memberof module:Helper
|
|
256
|
+
*/
|
|
257
|
+
export async function collectConfigHandlers () {
|
|
258
|
+
for (const pkg of this.app.pluginPkgs) {
|
|
259
|
+
let dir
|
|
260
|
+
try {
|
|
261
|
+
dir = this.getModuleDir(pkg)
|
|
262
|
+
} catch (err) {}
|
|
263
|
+
if (!dir) continue
|
|
264
|
+
const file = `${dir}/extend/bajo/config-handlers.js`
|
|
265
|
+
let mod = await this.importModule(file)
|
|
266
|
+
if (!mod) continue
|
|
267
|
+
if (isFunction(mod)) mod = await mod.call(this.app[camelCase(pkg)])
|
|
268
|
+
if (isPlainObject(mod)) mod = [mod]
|
|
269
|
+
this.app.configHandlers = this.app.configHandlers.concat(mod)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Bajo extra config:
|
|
275
|
+
* - reading config file
|
|
276
|
+
* - merge config with arguments & environments values
|
|
277
|
+
* - Set environment (```dev``` or ```prod```)
|
|
278
|
+
*
|
|
279
|
+
* @async
|
|
280
|
+
* @memberof module:Helper
|
|
281
|
+
*/
|
|
282
|
+
export async function buildExtConfig () {
|
|
283
|
+
// config merging
|
|
284
|
+
const { defaultsDeep, includes } = this.app.lib.aneka
|
|
285
|
+
const { parseObject, omitDeep } = this.app.lib
|
|
286
|
+
const { isEmpty, get, isString, without } = this.app.lib._
|
|
287
|
+
|
|
288
|
+
let resp = get(this, `app.options.config.${this.ns}`, {})
|
|
289
|
+
if (isEmpty(resp)) resp = await this.readAllConfigs(`${this.dir.data}/config/${this.ns}`)
|
|
290
|
+
resp = omitDeep(pick(resp, ['log', 'exitHandler', 'env', 'runtime']), omitted)
|
|
291
|
+
const envs = this.app.envs
|
|
292
|
+
this.config = defaultsDeep({}, this.config, resp, defConfig)
|
|
293
|
+
// language
|
|
294
|
+
this.config.lang = (this.config.lang ?? '').split('.')[0]
|
|
295
|
+
this.app.loadIntl(this.ns)
|
|
296
|
+
this.print = new Print(this)
|
|
297
|
+
// environment
|
|
298
|
+
if (values(envs).includes(this.config.env)) this.config.env = this.app.lib.aneka.getKeyByValue(envs, this.config.env)
|
|
299
|
+
if (!keys(envs).includes(this.config.env)) throw this.error('unknownEnv%s%s', this.config.env, this.join(keys(envs), { lastSeparator: this.t('or') }))
|
|
300
|
+
process.env.NODE_ENV = envs[this.config.env]
|
|
301
|
+
if (!this.config.log.level) this.config.log.level = this.config.env === 'dev' ? 'debug' : 'info'
|
|
302
|
+
// misc
|
|
303
|
+
const obj = this.app.applet ? this.config : pick(this.config, keys(defConfig))
|
|
304
|
+
this.config = parseObject(obj, { parseValue: true })
|
|
305
|
+
const exts = this.app.getConfigFormats()
|
|
306
|
+
if (this.app.applet) {
|
|
307
|
+
if (!this.app.pluginPkgs.includes('bajo-cli')) throw this.error('appletNeedsBajoCli')
|
|
308
|
+
if (!this.config.log.applet) this.config.log.level = 'silent'
|
|
309
|
+
this.config.exitHandler = false
|
|
310
|
+
}
|
|
311
|
+
if (this.config.runtime.noWarning) process.removeAllListeners('warning')
|
|
312
|
+
if (isString(this.config.cache.purge)) this.config.cache.purge = [this.config.cache.purge]
|
|
313
|
+
this.config.cache.purge = without(this.config.cache.purge, '', null, undefined)
|
|
314
|
+
if (this.config.cache.purge.length > 0) {
|
|
315
|
+
if (includes(['all', '*'], this.config.cache.purge)) this.app.cache.purge('*')
|
|
316
|
+
else {
|
|
317
|
+
for (const name of this.config.cache.purge) {
|
|
318
|
+
this.app.cache.purge(name)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
this.app.log = new Log(this.app)
|
|
323
|
+
this.log.trace('dataDir%s', this.dir.data)
|
|
324
|
+
this.log.debug('configHandlers%s', this.join(exts))
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Setup plugins boot orders by reading plugin's ```.bootorder``` file if provided.
|
|
329
|
+
*
|
|
330
|
+
* @async
|
|
331
|
+
* @memberof module:Helper
|
|
332
|
+
*/
|
|
333
|
+
export async function bootOrder () {
|
|
334
|
+
const { freeze } = this.app.lib
|
|
335
|
+
const { isNumber } = this.app.lib._
|
|
336
|
+
this.log.debug('setupBootOrder')
|
|
337
|
+
let counter = 1000
|
|
338
|
+
const orders = []
|
|
339
|
+
for (const pkg of this.app.pluginPkgs) {
|
|
340
|
+
const item = { pkg }
|
|
341
|
+
const ns = camelCase(pkg)
|
|
342
|
+
const order = get(this.app[ns], 'pkg.bajo.bootorder')
|
|
343
|
+
if (isNumber(order)) item.val = order
|
|
344
|
+
else {
|
|
345
|
+
item.val = counter
|
|
346
|
+
counter++
|
|
347
|
+
}
|
|
348
|
+
orders.push(item)
|
|
349
|
+
}
|
|
350
|
+
this.app.pluginPkgs = map(orderBy(orders, ['val']), 'pkg')
|
|
351
|
+
this.log.debug('runInEnv%s', this.t(this.app.envs[this.config.env]))
|
|
352
|
+
// misc
|
|
353
|
+
freeze(this.config)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Build configurations
|
|
358
|
+
*
|
|
359
|
+
* @async
|
|
360
|
+
* @memberof module:Helper
|
|
361
|
+
*/
|
|
362
|
+
export async function buildConfigs () {
|
|
363
|
+
this.bajo.log.debug('readConfigs')
|
|
364
|
+
for (const ns of this.getAllNs()) {
|
|
365
|
+
await this[ns].loadConfig()
|
|
366
|
+
this[ns].print = new Print(this[ns])
|
|
367
|
+
this.loadIntl(ns)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Ensure for names and aliases to be unique and no clashes with other plugins
|
|
373
|
+
*
|
|
374
|
+
* @async
|
|
375
|
+
* @memberof module:Helper
|
|
376
|
+
*/
|
|
377
|
+
export async function checkNameAliases () {
|
|
378
|
+
this.bajo.log.debug('checkAliasNameClash')
|
|
379
|
+
const refs = []
|
|
380
|
+
for (const pkg of this.bajo.app.pluginPkgs) {
|
|
381
|
+
const plugin = this.bajo.app[camelCase(pkg)]
|
|
382
|
+
const { ns, alias } = plugin
|
|
383
|
+
let item = find(refs, { ns })
|
|
384
|
+
if (item) throw this.bajo.error('pluginNameClash%s%s%s%s', ns, pkg, item.ns, item.pkg, { code: 'BAJO_NAME_CLASH' })
|
|
385
|
+
item = find(refs, { alias })
|
|
386
|
+
if (item) throw this.bajo.error('pluginNameClash%s%s%s%s', alias, pkg, item.alias, item.pkg, { code: 'BAJO_ALIAS_CLASH' })
|
|
387
|
+
refs.push({ ns, alias, pkg })
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Ensure dependencies are met
|
|
393
|
+
*
|
|
394
|
+
* @async
|
|
395
|
+
* @memberof module:Helper
|
|
396
|
+
*/
|
|
397
|
+
export async function checkDependencies () {
|
|
398
|
+
const { join } = this.bajo
|
|
399
|
+
this.bajo.log.debug('checkDeps')
|
|
400
|
+
for (const pkg of this.bajo.app.pluginPkgs) {
|
|
401
|
+
const plugin = this.bajo.app[camelCase(pkg)]
|
|
402
|
+
const { ns, dependencies } = plugin
|
|
403
|
+
this.bajo.log.trace('- %s', ns)
|
|
404
|
+
const odep = reduce(dependencies, (o, k) => {
|
|
405
|
+
const item = map(k.split('@'), m => trim(m))
|
|
406
|
+
if (k[0] === '@') o['@' + item[1]] = item[2]
|
|
407
|
+
else o[item[0]] = item[1]
|
|
408
|
+
return o
|
|
409
|
+
}, {})
|
|
410
|
+
const deps = keys(odep)
|
|
411
|
+
if (deps.length > 0) {
|
|
412
|
+
if (intersection(this.bajo.app.pluginPkgs, deps).length !== deps.length) {
|
|
413
|
+
throw this.bajo.error('dependencyUnfulfilled%s%s', pkg, join(deps), { code: 'BAJO_DEPENDENCY' })
|
|
414
|
+
}
|
|
415
|
+
each(deps, d => {
|
|
416
|
+
if (!odep[d]) return
|
|
417
|
+
const ver = get(this.bajo.app[camelCase(d)], 'pkg.version')
|
|
418
|
+
if (!ver) return
|
|
419
|
+
if (!semver.satisfies(ver, odep[d])) {
|
|
420
|
+
throw this.bajo.error('semverCheckFailed%s%s', pkg, `${d}@${odep[d]}`, { code: 'BAJO_DEPENDENCY_SEMVER' })
|
|
421
|
+
}
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Collect and build hooks and push them to the bajo's hook system
|
|
429
|
+
*
|
|
430
|
+
* @async
|
|
431
|
+
* @memberof module:Helper
|
|
432
|
+
* @fires bajo:afterCollectHooks
|
|
433
|
+
*/
|
|
434
|
+
export async function collectHooks () {
|
|
435
|
+
const { eachPlugins, runHook, isLogInRange, importModule } = this.bajo
|
|
436
|
+
const { isArray, isPlainObject } = this.lib._
|
|
437
|
+
const me = this // "this" is "app"
|
|
438
|
+
me.bajo.log.trace('collecting%s', this.t('hooks'))
|
|
439
|
+
await eachPlugins(async function ({ dir, file }) {
|
|
440
|
+
let mod = await importModule(file, { asHandler: true })
|
|
441
|
+
if (!mod) return undefined
|
|
442
|
+
if (file.includes('hook.js')) mod = await mod.handler.call(this)
|
|
443
|
+
if (isArray(mod)) {
|
|
444
|
+
for (const m of mod) {
|
|
445
|
+
if (!isPlainObject(m)) continue
|
|
446
|
+
if (!m.name) throw me.bajo.error('missing%s%s', 'name', file)
|
|
447
|
+
if (isArray(m.name)) {
|
|
448
|
+
for (const name of m.name) {
|
|
449
|
+
me.bajo.hooks.push(merge({}, m, { name, src: this.ns }))
|
|
450
|
+
}
|
|
451
|
+
} else {
|
|
452
|
+
m.src = this.ns
|
|
453
|
+
me.bajo.hooks.push(m)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
} else {
|
|
457
|
+
const _file = file.replace(dir + '/hook/', '').replace('.js', '')
|
|
458
|
+
let [names, path] = _file.split('@')
|
|
459
|
+
names = names.split('$').map(n => trim(n))
|
|
460
|
+
for (let name of names) {
|
|
461
|
+
name = name.split('.').map(n => camelCase(n)).join('.')
|
|
462
|
+
const m = merge({}, mod, { name: `${name}:${camelCase(path)}`, src: this.ns })
|
|
463
|
+
me.bajo.hooks.push(m)
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}, { glob: ['hook/*.js', 'hook.js'], prefix: me.bajo.ns })
|
|
467
|
+
// for log trace purpose only
|
|
468
|
+
if (isLogInRange('trace')) {
|
|
469
|
+
const items = groupBy(me.bajo.hooks, item => item.name)
|
|
470
|
+
forOwn(items, (v, k) => {
|
|
471
|
+
const [name, path] = k.split(':')
|
|
472
|
+
me.bajo.log.trace('- %s:%s (%d)', name, path, v.length)
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Run after hooks are collected
|
|
478
|
+
*
|
|
479
|
+
* @global
|
|
480
|
+
* @event bajo:afterCollectHooks
|
|
481
|
+
* @param {Object[]} hooks - Array of hook objects
|
|
482
|
+
* @see {@tutorial hook}
|
|
483
|
+
* @see module:Helper/Base.collectHooks
|
|
484
|
+
*/
|
|
485
|
+
await runHook('bajo:afterCollectHooks', this.bajo.hooks)
|
|
486
|
+
me.bajo.log.debug('collected%s%d', this.t('hooks'), me.bajo.hooks.length)
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Finally, run all plugins
|
|
491
|
+
*
|
|
492
|
+
* @async
|
|
493
|
+
* @fires bajo:beforeAll{method}
|
|
494
|
+
* @fires {ns}:before{method}
|
|
495
|
+
* @fires {ns}:after{method}
|
|
496
|
+
* @fires bajo:afterAll{method}
|
|
497
|
+
* @memberof module:Helper
|
|
498
|
+
*/
|
|
499
|
+
export async function run () {
|
|
500
|
+
const me = this
|
|
501
|
+
const { runHook, eachPlugins, join } = me.bajo
|
|
502
|
+
const { freeze } = me.lib
|
|
503
|
+
const methods = ['init']
|
|
504
|
+
if (!me.applet) methods.push('start')
|
|
505
|
+
for (const method of methods) {
|
|
506
|
+
/**
|
|
507
|
+
* Run before all ```{method}``` executed. Accepted ```{method}```: ```Init``` or ```Start```
|
|
508
|
+
*
|
|
509
|
+
* @global
|
|
510
|
+
* @event bajo:beforeAll{method}
|
|
511
|
+
* @param {string} method - Accepted methods: ```Init```, ```Start```
|
|
512
|
+
* @see module:Helper/Base.run
|
|
513
|
+
*/
|
|
514
|
+
await runHook(`bajo:${camelCase(`before all ${method}`)}`)
|
|
515
|
+
await eachPlugins(async function () {
|
|
516
|
+
const { ns } = this
|
|
517
|
+
/**
|
|
518
|
+
* Run before ```{method}``` is executed within ```{ns}``` context
|
|
519
|
+
*
|
|
520
|
+
* - ```{ns}``` - namespace
|
|
521
|
+
* - ```{method}``` - Accepted methods: ```Init``` or ```Start```
|
|
522
|
+
*
|
|
523
|
+
* @global
|
|
524
|
+
* @event {ns}:before{method}
|
|
525
|
+
* @see module:Helper/Base.run
|
|
526
|
+
*/
|
|
527
|
+
await runHook(`${ns}:${camelCase(`before ${method}`)}`)
|
|
528
|
+
await me[ns][method]()
|
|
529
|
+
/**
|
|
530
|
+
* Run after ```{method}``` is executed within ```{ns}``` context
|
|
531
|
+
*
|
|
532
|
+
* - ```{ns}``` - namespace
|
|
533
|
+
* - ```{method}``` - Accepted methods: ```Init``` or ```Start```
|
|
534
|
+
*
|
|
535
|
+
* @global
|
|
536
|
+
* @event {ns}:after{method}
|
|
537
|
+
* @see module:Helper/Base.run
|
|
538
|
+
*/
|
|
539
|
+
await runHook(`${ns}:${camelCase(`after ${method}`)}`)
|
|
540
|
+
if (method === 'start') freeze(me[ns].config)
|
|
541
|
+
})
|
|
542
|
+
/**
|
|
543
|
+
* Run after all ```{method}``` executed. Accepted ```{method}```: ```Init``` or ```Start```
|
|
544
|
+
*
|
|
545
|
+
* @global
|
|
546
|
+
* @event bajo:afterAll{method}
|
|
547
|
+
* @see module:Helper/Base.run
|
|
548
|
+
*/
|
|
549
|
+
await runHook(`bajo:${camelCase(`after all ${method}`)}`)
|
|
550
|
+
}
|
|
551
|
+
if (me.bajo.config.log.level === 'trace') {
|
|
552
|
+
let text = join(map(me.bajo.app.pluginPkgs, b => camelCase(b)))
|
|
553
|
+
text += ` (${me.bajo.app.pluginPkgs.length})`
|
|
554
|
+
me.bajo.log.trace('loadedPlugins%s', text)
|
|
555
|
+
} else me.bajo.log.debug('loadedPlugins%s', me.bajo.app.pluginPkgs.length)
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Iterate through all plugins loaded and do:
|
|
560
|
+
*
|
|
561
|
+
* 1. {@link module:Helper/Base.buildConfigs|build configs}
|
|
562
|
+
* 2. {@link module:Helper/Base.checkNameAliases|ensure names & aliases uniqueness}
|
|
563
|
+
* 3. {@link module:Helper/Base.checkDependencies|ensure dependencies are met}
|
|
564
|
+
* 4. {@link module:Helper/Base.collectHooks|collect hooks}
|
|
565
|
+
* 5. {@link module:Helper/Base.run|run plugins}
|
|
566
|
+
*
|
|
567
|
+
* @async
|
|
568
|
+
* @memberof module:Helper
|
|
569
|
+
*/
|
|
570
|
+
export async function bootPlugins () {
|
|
571
|
+
await buildConfigs.call(this.app)
|
|
572
|
+
await checkNameAliases.call(this.app)
|
|
573
|
+
await checkDependencies.call(this.app)
|
|
574
|
+
await collectHooks.call(this.app)
|
|
575
|
+
await run.call(this.app)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Attach plugins exit handlers and make sure the app shutdowns gracefully
|
|
580
|
+
*
|
|
581
|
+
* @async
|
|
582
|
+
* @memberof module:Helper
|
|
583
|
+
*/
|
|
584
|
+
export async function exitHandler () {
|
|
585
|
+
if (!this.config.exitHandler) return
|
|
586
|
+
|
|
587
|
+
async function exit (signal) {
|
|
588
|
+
const { eachPlugins } = this
|
|
589
|
+
if (signal) this.log.warn('signalReceived%s', signal)
|
|
590
|
+
const me = this
|
|
591
|
+
await eachPlugins(async function ({ ns }) {
|
|
592
|
+
try {
|
|
593
|
+
await this.exit()
|
|
594
|
+
} catch (err) {}
|
|
595
|
+
me.log.trace('exited%s', this.ns)
|
|
596
|
+
})
|
|
597
|
+
this.log.debug('appShutdown')
|
|
598
|
+
process.exit(0)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
process.on('SIGINT', async () => {
|
|
602
|
+
await exit.call(this, 'SIGINT')
|
|
603
|
+
})
|
|
604
|
+
|
|
605
|
+
process.on('SIGTERM', async () => {
|
|
606
|
+
await exit.call(this, 'SIGTERM')
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
process.on('beforeExit', async () => {
|
|
610
|
+
await exit.call(this)
|
|
611
|
+
})
|
|
612
|
+
|
|
613
|
+
process.on('uncaughtException', (error, origin) => {
|
|
614
|
+
setTimeout(() => {
|
|
615
|
+
console.error(error)
|
|
616
|
+
// process.exit(1)
|
|
617
|
+
}, 50)
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
621
|
+
const stackFile = reason.stack.split('\n')[1]
|
|
622
|
+
let file
|
|
623
|
+
const info = stackFile.match(/\((.*)\)/) // file is in (<file>)
|
|
624
|
+
if (info) file = info[1]
|
|
625
|
+
else if (stackFile.startsWith(' at ')) file = stackFile.slice(7) // file is stackFile itself
|
|
626
|
+
if (!file) return
|
|
627
|
+
const parts = file.split(':')
|
|
628
|
+
const column = parseInt(parts[parts.length - 1])
|
|
629
|
+
const line = parseInt(parts[parts.length - 2])
|
|
630
|
+
parts.pop()
|
|
631
|
+
parts.pop()
|
|
632
|
+
file = parts.join(':')
|
|
633
|
+
this.log.error({ file, line, column }, '%s', reason.message)
|
|
634
|
+
})
|
|
635
|
+
|
|
636
|
+
process.on('warning', warning => {
|
|
637
|
+
this.log.error('%s', warning.message)
|
|
638
|
+
})
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* If app is in ```applet``` mode, this little helper should take care plugin's applet boot process
|
|
643
|
+
*
|
|
644
|
+
* @async
|
|
645
|
+
* @fires {ns}:beforeAppletRun
|
|
646
|
+
* @fires {ns}:afterAppletRun
|
|
647
|
+
* @memberof module:Helper
|
|
648
|
+
*/
|
|
649
|
+
export async function runAsApplet () {
|
|
650
|
+
const { isString, map, find } = this.app.lib._
|
|
651
|
+
await this.eachPlugins(async function ({ file }) {
|
|
652
|
+
const { ns, alias } = this
|
|
653
|
+
this.app.applets.push({ ns, file, alias })
|
|
654
|
+
}, { glob: 'applet.js', prefix: 'bajoCli' })
|
|
655
|
+
|
|
656
|
+
this.log.debug('appletModeActivated')
|
|
657
|
+
this.print.info('appRunningAsApplet')
|
|
658
|
+
if (this.app.applets.length === 0) this.print.fatal('noAppletLoaded')
|
|
659
|
+
let name = this.app.applet
|
|
660
|
+
if (!isString(name)) {
|
|
661
|
+
const select = await this.importPkg('bajoCli:@inquirer/select')
|
|
662
|
+
name = await select({
|
|
663
|
+
message: this.t('Please select:'),
|
|
664
|
+
choices: map(this.app.applets, t => ({ value: t.ns }))
|
|
665
|
+
})
|
|
666
|
+
}
|
|
667
|
+
const [ns, path] = name.split(':')
|
|
668
|
+
const applet = find(this.app.applets, a => (a.ns === ns || a.alias === ns))
|
|
669
|
+
if (!applet) this.print.fatal('notFound%s%s', this.app.t('applet'), name)
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Run before applet is run. ```[ns]``` is applet's namespace
|
|
673
|
+
*
|
|
674
|
+
* @global
|
|
675
|
+
* @event {ns}:beforeAppletRun
|
|
676
|
+
* @param {...any} params
|
|
677
|
+
* @see {@tutorial hook}
|
|
678
|
+
* @see module:Helper/Bajo.runAsApplet
|
|
679
|
+
*/
|
|
680
|
+
await this.runHook(`${applet.ns}:beforeAppletRun`, ...this.app.args)
|
|
681
|
+
await this.app.bajoCli.runApplet(applet, path, ...this.app.args)
|
|
682
|
+
/**
|
|
683
|
+
* Run after applet is run. ```[ns]``` is applet's namespace
|
|
684
|
+
*
|
|
685
|
+
* @global
|
|
686
|
+
* @event {ns}:afterAppletRun
|
|
687
|
+
* @param {...any} params
|
|
688
|
+
* @see {@tutorial hook}
|
|
689
|
+
* @see module:Helper/Bajo.runAsApplet
|
|
690
|
+
* @memberof module:Helper
|
|
691
|
+
*/
|
|
692
|
+
await this.runHook(`${applet.ns}:afterAppletRun`, ...this.app.args)
|
|
693
|
+
}
|
|
694
|
+
</code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">Bajo API</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="App.html">App</a></div><div class="sidebar-section-children"><a href="Bajo.html">Bajo</a></div><div class="sidebar-section-children"><a href="Base.html">Base</a></div><div class="sidebar-section-children"><a href="Cache.html">Cache</a></div><div class="sidebar-section-children"><a href="Err.html">Err</a></div><div class="sidebar-section-children"><a href="Log.html">Log</a></div><div class="sidebar-section-children"><a href="Plugin.html">Plugin</a></div><div class="sidebar-section-children"><a href="Print.html">Print</a></div><div class="sidebar-section-children"><a href="Tools.html">Tools</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-events"><div>Events</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#event:bajo:afterAll%257Bmethod%257D">bajo:afterAll{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterBootComplete">bajo:afterBootComplete</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterBuildCollection">bajo:afterBuildCollection</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:afterCollectHooks">bajo:afterCollectHooks</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:beforeAll%257Bmethod%257D">bajo:beforeAll{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:bajo:beforeBuildCollection">bajo:beforeBuildCollection</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:after%257Bmethod%257D">{ns}:after{method}</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:beforeAppletRun">{ns}:beforeAppletRun</a></div><div class="sidebar-section-children"><a href="global.html#event:%257Bns%257D:before%257Bmethod%257D">{ns}:before{method}</a></div><div class="sidebar-section-children"><a href="module-Helper%257Bns%257D_afterAppletRun.html">Helper{ns}:afterAppletRun</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-modules"><div>Modules</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="module-Helper.html">Helper</a></div><div class="sidebar-section-children"><a href="module-Lib.html">Lib</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TAppConfigHandler">TAppConfigHandler</a></div><div class="sidebar-section-children"><a href="global.html#TAppEnv">TAppEnv</a></div><div class="sidebar-section-children"><a href="global.html#TBajoDataType">TBajoDataType</a></div><div class="sidebar-section-children"><a href="global.html#TBajoFormatResult">TBajoFormatResult</a></div><div class="sidebar-section-children"><a href="global.html#TBajoFormatType">TBajoFormatType</a></div><div class="sidebar-section-children"><a href="global.html#TLogJson">TLogJson</a></div><div class="sidebar-section-children"><a href="global.html#TLogLevels">TLogLevels</a></div><div class="sidebar-section-children"><a href="global.html#TNsPathPairs">TNsPathPairs</a></div><div class="sidebar-section-children"><a href="global.html#TNsPathResult">TNsPathResult</a></div><div class="sidebar-section-children"><a href="global.html#TPrintOptions">TPrintOptions</a></div><div class="sidebar-section-children"><a href="global.html#boot">boot</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>
|