bajo 0.0.1 → 0.2.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.
Files changed (160) hide show
  1. package/.jsdoc.conf.json +38 -0
  2. package/README.md +57 -3
  3. package/bajoBook/book/doc/.metadata.json +28 -0
  4. package/bajoBook/book/doc/How-to-Make-a-Paper-Boat-564x400@2x.jpg +0 -0
  5. package/bajoBook/book/doc/pages/guides/definition.md +7 -0
  6. package/bajoBook/book/doc/pages/guides/intro.md +3 -0
  7. package/bajoBook/book/doc/pages/guides/setup.md +22 -0
  8. package/bajoBook/book/reference/.metadata.json +152 -0
  9. package/bajoBook/book/reference/concept-leadership-business-with-paper-boats.jpg +0 -0
  10. package/bajoBook/book/reference/pages/configuration/configuration-file.md +52 -0
  11. package/bajoBook/book/reference/pages/helper/break-ns-path.md +24 -0
  12. package/bajoBook/book/reference/pages/helper/build-collections.md +19 -0
  13. package/bajoBook/book/reference/pages/helper/call-helper-or-handler.md +35 -0
  14. package/bajoBook/book/reference/pages/helper/current-loc.md +28 -0
  15. package/bajoBook/book/reference/pages/helper/dayjs.md +13 -0
  16. package/bajoBook/book/reference/pages/helper/defaults-deep.md +29 -0
  17. package/bajoBook/book/reference/pages/helper/dump.md +32 -0
  18. package/bajoBook/book/reference/pages/helper/each-plugins.md +24 -0
  19. package/bajoBook/book/reference/pages/helper/envs.md +11 -0
  20. package/bajoBook/book/reference/pages/helper/error.md +29 -0
  21. package/bajoBook/book/reference/pages/helper/fatal.md +18 -0
  22. package/bajoBook/book/reference/pages/helper/freeze.md +13 -0
  23. package/bajoBook/book/reference/pages/helper/generate-id.md +36 -0
  24. package/bajoBook/book/reference/pages/helper/get-config.md +27 -0
  25. package/bajoBook/book/reference/pages/helper/get-global-module-dir.md +13 -0
  26. package/bajoBook/book/reference/pages/helper/get-helper.md +13 -0
  27. package/bajoBook/book/reference/pages/helper/get-item-by-name.md +13 -0
  28. package/bajoBook/book/reference/pages/helper/get-key-by-value.md +13 -0
  29. package/bajoBook/book/reference/pages/helper/get-module-dir.md +13 -0
  30. package/bajoBook/book/reference/pages/helper/get-plugin-data-dir.md +13 -0
  31. package/bajoBook/book/reference/pages/helper/get-plugin-name.md +13 -0
  32. package/bajoBook/book/reference/pages/helper/get-plugin.md +13 -0
  33. package/bajoBook/book/reference/pages/helper/import-module.md +13 -0
  34. package/bajoBook/book/reference/pages/helper/import-pkg.md +13 -0
  35. package/bajoBook/book/reference/pages/helper/is-empty-dir.md +13 -0
  36. package/bajoBook/book/reference/pages/helper/is-log-in-range.md +13 -0
  37. package/bajoBook/book/reference/pages/helper/is-set.md +13 -0
  38. package/bajoBook/book/reference/pages/helper/is-valid-app.md +13 -0
  39. package/bajoBook/book/reference/pages/helper/is-valid-plugin.md +13 -0
  40. package/bajoBook/book/reference/pages/helper/log-levels.md +13 -0
  41. package/bajoBook/book/reference/pages/helper/log.md +13 -0
  42. package/bajoBook/book/reference/pages/helper/paginate.md +13 -0
  43. package/bajoBook/book/reference/pages/helper/pascal-case.md +13 -0
  44. package/bajoBook/book/reference/pages/helper/print.md +13 -0
  45. package/bajoBook/book/reference/pages/helper/read-config.md +13 -0
  46. package/bajoBook/book/reference/pages/helper/read-json.md +13 -0
  47. package/bajoBook/book/reference/pages/helper/resolve-path.md +13 -0
  48. package/bajoBook/book/reference/pages/helper/resolve-tpl-path.md +13 -0
  49. package/bajoBook/book/reference/pages/helper/run-hook.md +13 -0
  50. package/bajoBook/book/reference/pages/helper/save-as-download.md +13 -0
  51. package/bajoBook/book/reference/pages/helper/titleize.md +13 -0
  52. package/bajoBook/book/reference/pages/helper/white-space.md +13 -0
  53. package/boot/attach-helper.js +34 -0
  54. package/boot/boot-order.js +34 -0
  55. package/boot/build-config.js +99 -0
  56. package/boot/create-scope.js +36 -0
  57. package/boot/exit-handler.js +60 -0
  58. package/boot/helper/break-ns-path.js +17 -0
  59. package/boot/helper/build-collections.js +34 -0
  60. package/boot/helper/call-helper-or-handler.js +15 -0
  61. package/boot/helper/current-loc.js +11 -0
  62. package/boot/helper/defaults-deep.js +14 -0
  63. package/boot/helper/dump.js +10 -0
  64. package/boot/helper/each-plugins.js +104 -0
  65. package/boot/helper/envs.js +14 -0
  66. package/boot/helper/error.js +67 -0
  67. package/boot/helper/fatal.js +12 -0
  68. package/boot/helper/generate-id.js +20 -0
  69. package/boot/helper/get-config.js +19 -0
  70. package/boot/helper/get-global-module-dir.js +27 -0
  71. package/boot/helper/get-helper.js +12 -0
  72. package/boot/helper/get-item-by-name.js +26 -0
  73. package/boot/helper/get-key-by-value.js +5 -0
  74. package/boot/helper/get-module-dir.js +22 -0
  75. package/boot/helper/get-plugin-data-dir.js +12 -0
  76. package/boot/helper/get-plugin-name.js +39 -0
  77. package/boot/helper/get-plugin.js +14 -0
  78. package/boot/helper/import-module.js +25 -0
  79. package/boot/helper/import-pkg.js +67 -0
  80. package/boot/helper/is-empty-dir.js +9 -0
  81. package/boot/helper/is-log-in-range.js +10 -0
  82. package/boot/helper/is-set.js +5 -0
  83. package/boot/helper/is-valid-app.js +12 -0
  84. package/boot/helper/is-valid-plugin.js +12 -0
  85. package/boot/helper/log-levels.js +19 -0
  86. package/boot/helper/paginate.js +26 -0
  87. package/boot/helper/pascal-case.js +7 -0
  88. package/boot/helper/print.js +99 -0
  89. package/boot/helper/read-config.js +46 -0
  90. package/boot/helper/read-json.js +10 -0
  91. package/boot/helper/resolve-path.js +15 -0
  92. package/boot/helper/resolve-tpl-path.js +15 -0
  93. package/boot/helper/run-hook.js +46 -0
  94. package/boot/helper/save-as-download.js +17 -0
  95. package/boot/helper/titleize.js +14 -0
  96. package/boot/helper/white-space.js +3 -0
  97. package/boot/index.js +60 -0
  98. package/boot/lib/bora.js +97 -0
  99. package/boot/lib/build-helper.js +57 -0
  100. package/boot/lib/logger.js +75 -0
  101. package/boot/lib/omitted-plugin-keys.js +3 -0
  102. package/boot/lib/parse-args-argv.js +75 -0
  103. package/boot/lib/parse-env.js +36 -0
  104. package/boot/lib/shim.js +14 -0
  105. package/boot/plugins/attach-helper.js +20 -0
  106. package/boot/plugins/build-config.js +82 -0
  107. package/boot/plugins/check-clash.js +18 -0
  108. package/boot/plugins/check-dependency.js +37 -0
  109. package/boot/plugins/collect-config-handlers.js +25 -0
  110. package/boot/plugins/collect-exit-handlers.js +23 -0
  111. package/boot/plugins/collect-hooks.js +34 -0
  112. package/boot/plugins/extend-config.js +21 -0
  113. package/boot/plugins/index.js +28 -0
  114. package/boot/plugins/run.js +31 -0
  115. package/boot/run-tool.js +34 -0
  116. package/docs/boot_build-config.js.html +75 -0
  117. package/docs/boot_create-scope.js.html +25 -0
  118. package/docs/boot_index.js.html +43 -0
  119. package/docs/data/search.json +1 -0
  120. package/docs/fonts/Inconsolata-Regular.ttf +0 -0
  121. package/docs/fonts/OpenSans-Regular.ttf +0 -0
  122. package/docs/fonts/WorkSans-Bold.ttf +0 -0
  123. package/docs/helper_emit.js.html +18 -0
  124. package/docs/helper_envs.js.html +16 -0
  125. package/docs/helper_error.js.html +25 -0
  126. package/docs/helper_get-bajo.js.html +42 -0
  127. package/docs/helper_index.js.html +39 -0
  128. package/docs/helper_log-levels.js.html +20 -0
  129. package/docs/helper_set-hook.js.html +38 -0
  130. package/docs/helper_walk-bajos.js.html +51 -0
  131. package/docs/index.html +16 -0
  132. package/docs/module-boot.html +3 -0
  133. package/docs/module-boot_buildConfig.html +3 -0
  134. package/docs/module-boot_createScope.html +3 -0
  135. package/docs/module-helper.html +7 -0
  136. package/docs/module-helper_setHook.html +4 -0
  137. package/docs/module-helper_walkBajos.html +6 -0
  138. package/docs/scripts/core.js +655 -0
  139. package/docs/scripts/core.min.js +23 -0
  140. package/docs/scripts/resize.js +90 -0
  141. package/docs/scripts/search.js +265 -0
  142. package/docs/scripts/search.min.js +6 -0
  143. package/docs/scripts/third-party/Apache-License-2.0.txt +202 -0
  144. package/docs/scripts/third-party/fuse.js +9 -0
  145. package/docs/scripts/third-party/hljs-line-num-original.js +369 -0
  146. package/docs/scripts/third-party/hljs-line-num.js +1 -0
  147. package/docs/scripts/third-party/hljs-original.js +5171 -0
  148. package/docs/scripts/third-party/hljs.js +1 -0
  149. package/docs/scripts/third-party/popper.js +5 -0
  150. package/docs/scripts/third-party/tippy.js +1 -0
  151. package/docs/scripts/third-party/tocbot.js +672 -0
  152. package/docs/scripts/third-party/tocbot.min.js +1 -0
  153. package/docs/styles/clean-jsdoc-theme-base.css +975 -0
  154. package/docs/styles/clean-jsdoc-theme-dark.css +407 -0
  155. package/docs/styles/clean-jsdoc-theme-light.css +388 -0
  156. package/docs/styles/clean-jsdoc-theme.min.css +1 -0
  157. package/package.json +36 -4
  158. package/test/helper-error.js +25 -0
  159. package/test/helper-isSet.js +41 -0
  160. package/test/helper-pathResolve.js +28 -0
@@ -0,0 +1,36 @@
1
+ ---
2
+ title: function generateId()
3
+ ---
4
+
5
+ ID generator based on [nanoid](https://github.com/ai/nanoid).
6
+
7
+ By default, ```options.pattern``` is set to ```abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789``` so that resulted ID will be a full alphanumeric characters with the length of ```options.length``` (default: ```21```)
8
+
9
+ If ```options.pattern``` is an object:
10
+
11
+ - ```pattern```: characters to use
12
+ - ```lowerCase```: if ```true```, only use lowercase
13
+ - ```upperCase```: if ```true```, only use uppercase
14
+
15
+ ```lowerCase``` and ```upperCase``` is mutually exclusive, first one win.
16
+
17
+ ###### Parameters:
18
+
19
+ | Name | Type | Default Value | Description |
20
+ | ---- | ---- | ------------- | ----------- |
21
+ | ```options``` | object |||
22
+ |     ```pattern``` | string or object | all alphanumeric characters | see above |
23
+ |     ```length``` | integer | ```21``` | length of generated ID |
24
+ |     ```returnInstance``` | boolean | ```false``` | Set ```true``` to return a nanoid instance |
25
+
26
+ ###### Returns:
27
+
28
+ ```string``` or ```object```
29
+
30
+ #### Example
31
+
32
+ ```javascript
33
+ ...
34
+ const { generateId } = this.bajo.helper
35
+ console.log(generateId()) // returns: <randomly generated characters>
36
+ ```
@@ -0,0 +1,27 @@
1
+ ---
2
+ title: function getConfig()
3
+ ---
4
+
5
+ Get configuration object of **bajo** or its **plugin**
6
+
7
+ ###### Parameters:
8
+
9
+ | Name | Type | Default&nbsp;Value | Description |
10
+ | ---- | ---- | ------------- | ----------- |
11
+ | ```name``` | string | ```bajo``` | A valid plugin name |
12
+ | ```options``` | object |||
13
+ | &nbsp;&nbsp;&nbsp;&nbsp;```full``` | boolean | ```false``` | If ```true```, returns ALL config objects belonging to named plugin. Otherwise, returns only subset of objects that can be changed through configuration file |
14
+ | &nbsp;&nbsp;&nbsp;&nbsp;```clone``` | boolean | ```false``` | If ```true```, returns cloned object |
15
+
16
+ ###### Returns:
17
+
18
+ ```object```
19
+
20
+ #### Example
21
+
22
+ ```javascript
23
+ ...
24
+ const { getConfig } = this.bajo.helper
25
+ const cfg = getConfig('bajoLogger')
26
+ console.log(cfg)
27
+ ```
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getGlobalModuleDir()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getHelper()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function getItemByName()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getKeyByValue()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getModuleDir()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getPluginDataDir()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getPluginName()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function getPlugin()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function importModule()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function importPkg()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function isEmptyDir()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function isLogInRange()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function isSet()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function isValidApp()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function isValidPlugin()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: object logLevels
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: object log
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function paginate()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function pascalCase()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: object print
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function readConfig()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function readJson()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function resolvePath()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function resolveTplPath()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function runHook()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: async function saveAsDownload()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: function titleize()
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,13 @@
1
+ ---
2
+ title: array whiteSpace
3
+ ---
4
+
5
+ ###### Parameters:
6
+
7
+ | Name | Type | Default Value | Description |
8
+ | ---- | ---- | ------------- | ----------- |
9
+
10
+ ###### Returns:
11
+
12
+
13
+ #### Example
@@ -0,0 +1,34 @@
1
+ import buildHelper from './lib/build-helper.js'
2
+ import logger from './lib/logger.js'
3
+ import fs from 'fs-extra'
4
+ import dayjs from 'dayjs'
5
+ import utc from 'dayjs/plugin/utc.js'
6
+ import customParseFormat from 'dayjs/plugin/customParseFormat.js'
7
+ import deepFreeze from 'deep-freeze-strict'
8
+ import currentLoc from './helper/current-loc.js'
9
+
10
+ dayjs.extend(utc)
11
+ dayjs.extend(customParseFormat)
12
+
13
+ export default async function () {
14
+ this.bajo.helper = await buildHelper.call(this, `${currentLoc(import.meta).dir}/helper`)
15
+ this.bajo.helper.freeze = (o, shallow) => {
16
+ if (shallow) Object.freeze(o)
17
+ else deepFreeze(o)
18
+ }
19
+ this.bajo.helper.log = logger.call(this)
20
+ this.bajo.helper.dayjs = dayjs
21
+ this.bajo.helper.setImmediate = function () {
22
+ return new Promise((resolve) => {
23
+ setImmediate(() => resolve())
24
+ })
25
+ }
26
+ this.bajo.helper.freeze(this.bajo.helper, true)
27
+ // last cleanup
28
+ if (!fs.existsSync(this.bajo.config.dir.data)) {
29
+ this.bajo.helper.log.warn('Data directory \'%s\' is not set yet!', this.bajo.config.dir.data)
30
+ }
31
+ if (this.bajo.config.env === 'dev') {
32
+ this.dump = this.bajo.helper.dump
33
+ }
34
+ }
@@ -0,0 +1,34 @@
1
+ import { reduce, map, isNaN, trim, forOwn, orderBy } from 'lodash-es'
2
+ import fs from 'fs-extra'
3
+ import getModuleDir from './helper/get-module-dir.js'
4
+
5
+ async function bootOrder () {
6
+ const { log, envs, error } = this.bajo.helper
7
+ log.debug('Setup boot order')
8
+ const config = this.bajo.config
9
+ const order = reduce(config.plugins, (o, k, i) => {
10
+ const key = map(k.split(':'), m => trim(m))
11
+ if (key[1] && !isNaN(Number(key[1]))) o[key[0]] = Number(key[1])
12
+ else o[key[0]] = 10000 + i
13
+ return o
14
+ }, {})
15
+ const norder = {}
16
+ for (let n of config.plugins) {
17
+ n = map(n.split(':'), m => trim(m))[0]
18
+ const dir = n === 'app' ? (config.dir.base + '/app') : getModuleDir(n)
19
+ if (n !== 'app' && !fs.existsSync(`${dir}/bajo`)) throw error('Package \'%s\' not found or isn\'t a valid Bajo package', n)
20
+ norder[n] = NaN
21
+ try {
22
+ norder[n] = Number(trim(await fs.readFile(`${dir}/bajo/.bootorder`, 'utf8')))
23
+ } catch (err) {}
24
+ }
25
+ const result = []
26
+ forOwn(order, (v, k) => {
27
+ const item = { k, v: isNaN(norder[k]) ? v : norder[k] }
28
+ result.push(item)
29
+ })
30
+ config.plugins = map(orderBy(result, ['v']), 'k')
31
+ log.info('Run in \'%s\' environment', envs[config.env])
32
+ }
33
+
34
+ export default bootOrder
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @module boot/buildConfig
3
+ */
4
+
5
+ import os from 'os'
6
+ import fs from 'fs-extra'
7
+ import { get, set, pick, values, keys, uniq, without, filter, map, isEmpty, trim } from 'lodash-es'
8
+ import omitDeep from 'omit-deep'
9
+ import resolvePath from './helper/resolve-path.js'
10
+ import readConfig from './helper/read-config.js'
11
+ import getKeyByValue from './helper/get-key-by-value.js'
12
+ import envs from './helper/envs.js'
13
+ import defaultsDeep from './helper/defaults-deep.js'
14
+ import parseArgsArgv from './lib/parse-args-argv.js'
15
+ import parseEnv from './lib/parse-env.js'
16
+ import error from './helper/error.js'
17
+ import currentLoc from './helper/current-loc.js'
18
+
19
+ const configFilePick = ['log', 'plugins', 'env', 'run']
20
+ const configFileOmit = ['tool', 'spawn', 'cwd', 'name', 'alias']
21
+
22
+ const defConfig = {
23
+ dir: {},
24
+ log: {
25
+ dateFormat: 'YYYY-MM-DDTHH:MM:ss.SSS[Z]',
26
+ report: [],
27
+ tool: false
28
+ },
29
+ lang: Intl.DateTimeFormat().resolvedOptions().lang ?? 'en-US',
30
+ plugins: ['app'],
31
+ env: 'dev',
32
+ tool: false,
33
+ exitHandler: true
34
+ }
35
+
36
+ /**
37
+ * Building configuration object. Read configurtion file from app data directory, program
38
+ * arguments and envoronment variables with following priority: ```Env > Args > Config file >
39
+ * defaults config```
40
+ *
41
+ * If data directory is provided and doesn't exist yet, it will be automatically created.
42
+ *
43
+ * Config file must be located in: ```<data dir>/config/bajo.<format>```, and support either
44
+ * ```.json``` or ```.js``` format. JS format must be a nodejs module that wrap an async
45
+ * function and return an object
46
+ *
47
+ * If app run tool, by default log is in silent. To activate, set log.tool to true
48
+ *
49
+ * By default, if 'app' is a bajo app, it is always the last plugins to boot. To overide this
50
+ * behavior, you should configure it at plugin level (i.e. using .bootorder file)
51
+ *
52
+ * @instance
53
+ * @async
54
+ * @throws Will throw if data directory is not provided
55
+ *
56
+ * @returns {Object} config
57
+ */
58
+
59
+ async function buildConfig (cwd) {
60
+ const { args, argv } = await parseArgsArgv()
61
+ const env = parseEnv()
62
+ const envArgv = defaultsDeep({}, env.root, argv.root)
63
+ envArgv.name = 'bajo'
64
+ envArgv.alias = 'bajo'
65
+ // directories
66
+ set(envArgv, 'dir.base', cwd)
67
+ set(envArgv, 'dir.pkg', resolvePath(currentLoc(import.meta).dir + '/..'))
68
+ if (!get(envArgv, 'dir.data')) set(envArgv, 'dir.data', `${envArgv.dir.base}/data`)
69
+ envArgv.dir.data = resolvePath(envArgv.dir.data)
70
+ if (!envArgv.dir.tmp) {
71
+ envArgv.dir.tmp = resolvePath(os.tmpdir()) + '/bajo'
72
+ fs.ensureDirSync(envArgv.dir.tmp)
73
+ }
74
+ // config merging
75
+ let resp = await readConfig.call(this, `${envArgv.dir.data}/config/bajo.*`, { ignoreError: true })
76
+ resp = omitDeep(pick(resp, configFilePick), configFileOmit)
77
+ const config = defaultsDeep({}, envArgv, resp, defConfig)
78
+ // force init
79
+ config.args = args
80
+ config.env = config.env.toLowerCase()
81
+ if (values(envs).includes(config.env)) config.env = getKeyByValue(envs, config.env)
82
+ if (!keys(envs).includes(config.env)) config.env = 'dev'
83
+ process.env.NODE_ENV = envs[config.env]
84
+ if (!config.log.level) config.log.level = config.env === 'dev' ? 'debug' : 'info'
85
+ if (config.silent) config.log.level = 'silent'
86
+ // sanitize plugins
87
+ config.plugins = without(config.plugins, 'app')
88
+ // if (fs.existsSync(`${config.dir.base}/app/bajo`)) config.plugins.push('app')
89
+ config.plugins.push('app')
90
+ config.plugins = filter(uniq(map(config.plugins, b => trim(b))), b => !isEmpty(b))
91
+ if (config.tool) {
92
+ if (!config.plugins.includes('bajo-cli')) throw error('Sidetool needs to have \'bajo-cli\' package loaded first')
93
+ if (!config.log.tool) config.log.level = 'silent'
94
+ config.exitHandler = false
95
+ }
96
+ this.bajo.config = config
97
+ }
98
+
99
+ export default buildConfig
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @module boot/createScope
3
+ */
4
+
5
+ import { isFunction } from 'lodash-es'
6
+ import readJson from './helper/read-json.js'
7
+ import importModule from './helper/import-module.js'
8
+
9
+ async function defHandler (file) {
10
+ let mod = await importModule(file)
11
+ if (isFunction(mod)) mod = await mod.call(this)
12
+ return mod
13
+ }
14
+
15
+ const configHandlers = [
16
+ { ext: '.js', handler: defHandler },
17
+ { ext: '.mjs', handler: defHandler },
18
+ { ext: '.json', handler: readJson }
19
+ ]
20
+
21
+ const bajo = {
22
+ runAt: new Date(),
23
+ configHandlers
24
+ }
25
+
26
+ /**
27
+ * @instance
28
+ * @async
29
+ * @returns {Object} scope
30
+ */
31
+
32
+ function createScope () {
33
+ return { bajo }
34
+ }
35
+
36
+ export default createScope