bajo 2.23.0 → 2.25.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 (108) hide show
  1. package/.jsdoc.conf.json +2 -1
  2. package/class/app.js +220 -107
  3. package/class/bajo.js +257 -177
  4. package/class/base.js +85 -24
  5. package/class/cache.js +8 -16
  6. package/class/err.js +31 -10
  7. package/class/log.js +46 -38
  8. package/class/plugin.js +53 -30
  9. package/class/print.js +18 -15
  10. package/class/tools.js +17 -7
  11. package/docs/App.html +51 -1
  12. package/docs/Bajo.html +1 -1
  13. package/docs/Base.html +34 -1
  14. package/docs/Cache.html +1 -1
  15. package/docs/Err.html +2 -2
  16. package/docs/Log.html +2 -2
  17. package/docs/Main.html +3 -0
  18. package/docs/Plugin.html +1 -1
  19. package/docs/Print.html +1 -1
  20. package/docs/Tools.html +1 -1
  21. package/docs/class_app.js.html +222 -109
  22. package/docs/class_bajo.js.html +260 -179
  23. package/docs/class_base.js.html +87 -26
  24. package/docs/class_cache.js.html +10 -18
  25. package/docs/class_err.js.html +33 -12
  26. package/docs/class_log.js.html +48 -40
  27. package/docs/class_plugin.js.html +55 -32
  28. package/docs/class_print.js.html +20 -17
  29. package/docs/class_tools.js.html +19 -9
  30. package/docs/data/search.json +1 -1
  31. package/docs/global.html +22 -1
  32. package/docs/index.html +1 -1
  33. package/docs/index.js.html +53 -16
  34. package/docs/lib_applet-hook.js.html +328 -0
  35. package/docs/lib_config.js.html +167 -0
  36. package/docs/lib_helper.js.html +186 -192
  37. package/docs/module-Applet.html +3 -0
  38. package/docs/module-Helper.html +1 -8
  39. package/docs/module-Hook.html +1 -1
  40. package/docs/tutorial-01_WELCOME.html +3 -0
  41. package/docs/tutorial-02_GETTING_STARTED.html +125 -0
  42. package/docs/tutorial-03_USER_GUIDE.html +54 -0
  43. package/docs/tutorial-04_DEV_GUIDE.html +178 -0
  44. package/docs/tutorial-05_ECOSYSTEM.html +3 -0
  45. package/extend/bajo/intl/en-US.json +2 -1
  46. package/extend/bajo/intl/id.json +2 -1
  47. package/index.js +51 -14
  48. package/lib/applet-hook.js +325 -0
  49. package/lib/config.js +164 -0
  50. package/lib/helper.js +185 -191
  51. package/package.json +6 -2
  52. package/test/00-global-hooks.test.js +44 -0
  53. package/test/e2e/_run.js +19 -0
  54. package/test/e2e/e2e-applet-process.test.js +66 -0
  55. package/test/e2e/e2e-boot-process.test.js +62 -0
  56. package/test/e2e/e2e-hooks-process.test.js +68 -0
  57. package/test/integration/_setup.js +55 -0
  58. package/test/integration/aspect-01-boot-instance.test.js +24 -0
  59. package/test/integration/aspect-02-plugin-loading.test.js +24 -0
  60. package/test/integration/aspect-03-config-merge.test.js +27 -0
  61. package/test/integration/aspect-04-intl-loading.test.js +29 -0
  62. package/test/integration/aspect-05-cache-roundtrip.test.js +26 -0
  63. package/test/integration/aspect-06-log-file-save.test.js +28 -0
  64. package/test/integration/aspect-07-plugin-data-dir.test.js +25 -0
  65. package/test/integration/aspect-08-plugin-lookup.test.js +25 -0
  66. package/test/integration/aspect-09-config-formats.test.js +26 -0
  67. package/test/integration/aspect-10-hooks.test.js +38 -0
  68. package/test/unit/app.test.js +115 -0
  69. package/test/unit/applet-hook.test.js +10 -0
  70. package/test/unit/bajo.test.js +151 -0
  71. package/test/unit/base.test.js +90 -0
  72. package/test/unit/cache.test.js +89 -0
  73. package/test/unit/config.test.js +14 -0
  74. package/test/unit/err.test.js +57 -0
  75. package/test/unit/helper.test.js +116 -0
  76. package/test/unit/index.test.js +40 -0
  77. package/test/unit/log.test.js +84 -0
  78. package/test/unit/plugin.test.js +95 -0
  79. package/test/unit/print.test.js +72 -0
  80. package/test/unit/tools.test.js +29 -0
  81. package/wiki/CHANGES.md +10 -0
  82. package/wiki/tutorials/01_WELCOME.md +18 -0
  83. package/wiki/tutorials/02_GETTING_STARTED.md +366 -0
  84. package/wiki/tutorials/03_USER_GUIDE.md +262 -0
  85. package/wiki/tutorials/04_DEV_GUIDE.md +386 -0
  86. package/wiki/tutorials/05_ECOSYSTEM.md +95 -0
  87. package/wiki/tutorials/tutorials.json +17 -0
  88. package/docs/lib_hook.js.html +0 -231
  89. package/docs/module-Lib.html +0 -3
  90. package/lib/hook.js +0 -228
  91. package/test/app.test.js +0 -183
  92. package/test/bajo.test.js +0 -124
  93. package/test/base.test.js +0 -75
  94. package/test/cache.test.js +0 -94
  95. package/test/e2e.test.js +0 -137
  96. package/test/err.test.js +0 -73
  97. package/test/helper.test.js +0 -39
  98. package/test/import-module.test.js +0 -138
  99. package/test/integration.test.js +0 -218
  100. package/test/log.test.js +0 -119
  101. package/test/plugin.test.js +0 -116
  102. package/test/print.test.js +0 -100
  103. package/test/tools.test.js +0 -38
  104. package/wiki/CONFIG.md +0 -36
  105. package/wiki/DEV_GUIDE.md +0 -3
  106. package/wiki/ECOSYSTEM.md +0 -93
  107. package/wiki/GETTING-STARTED.md +0 -356
  108. package/wiki/USER-GUIDE.md +0 -256
@@ -1,256 +0,0 @@
1
- # User Guide
2
-
3
- ## Overview
4
-
5
- ### Definitions
6
-
7
- Before proceeding, familiarize yourself with these terminologies used throughout this documentation:
8
-
9
- 1. ```{appDir}```: The application's project directory where all your code residese.
10
- 2. ```{dataDir}```: The directory for all data; defaults to ```{appDir}/data``` and is created automatically if missing.
11
- 3. ```{tmpDir}```: The temporary directory, defaults to the OS temporary directory.
12
- 4. ```{pkgName}```: The plugin's package name, typically matching its npm listing.
13
- 5. ```{ns}```: The plugin name or namespace, which is the camel-cased version of the package name.
14
- 6. ```{mainNs}```: The special main namespace and directory named main within your ```{appDir}```; this is where all application code should be written.
15
-
16
- ### Directory Structure
17
-
18
- Your typical Bajo app directory structure should look like this:
19
-
20
- ```
21
- .
22
- └── {appDir}
23
- ├── {dataDir}
24
- │ ├── config
25
- │ │ ├── .plugin
26
- │ │ ├── bajo.json
27
- │ │ ├── main.json
28
- │ │ └── ...
29
- │ └── plugins
30
- │ └── ...
31
- ├── main
32
- │ ├── extend
33
- │ │ └── ...
34
- │ ├── index.js
35
- │ └── ...
36
- ├── package.json
37
- ├── index.js
38
- └── ...
39
- ```
40
-
41
- 1. You can move ```{dataDir}``` out of ```{appDir}``` if you want, but you need to tell Bajo where to find it. For more on this, please follow along.
42
- 2. ```{dataDir}``` should be the only place Bajo **writes** anything. Bajo and its plugins should **never** be allowed to write anything outside ```{dataDir}``` on their own.
43
- 3. ```config``` is a special directory within ```{dataDir}``` where your configuration files should reside. Inside this directory, you should find:
44
- - a special file named ```.plugins``` that tells Bajo which plugins should be loaded
45
- - a file named ```bajo.json``` to configure global settings
46
- - all plugin-specific config files, named after their namespace
47
- 4. The ```main``` directory, or ```{mainNs}``` namespace, is the special plugin where you put your application code. And yes, it is actually a normal Bajo plugin! This means everything in there will be handled just like a regular plugin—it has the ability to extend other plugins, has its own config file, and more — with a few differences:
48
- - it's always available and can't be disabled
49
- - it's always the last one to start
50
- - if this directory is missing, it will be created automatically on startup
51
- - if the plugin's factory function is missing (```index.js```), it will be created dynamically
52
- 5. ```index.js``` is the main entry point for your app.
53
-
54
- To set your ```{dataDir}``` somewhere else, you need to tell Bajo where to find it by using an argument switch.
55
-
56
- Assuming your data directory is ```my-data-dir``` at the same level as your app directory, run your app like this:
57
-
58
- ```bash
59
- $ node index.js --dir-data=../my-data-dir
60
- ```
61
-
62
- If using program arguments seems a bit like a hassle for you, just use Bajo's [dotenv](https://github.com/motdotla/dotenv) support. Create a ```.env``` file in your app directory and put this inside:
63
-
64
- ```text
65
- DIR__DATA=../my-data-dir # double underscores!!!
66
- ```
67
-
68
- From now on, you can start the app just by typing:
69
-
70
- ```bash
71
- $ node index.js
72
- ```
73
-
74
- ### Runtime
75
-
76
- Bajo should run perfectly fine on Node.js version 20 or higher. Using the latest stable runtime is recommended. Bajo-based apps are also known to run with **Bun** without any problems. But Bajo **cannot** run on Deno due to its heavy reliance on Node.js-specific libraries and environments.
77
-
78
- Bajo is a pure ES6 framework that utilizes dynamic imports ```a lot```. Running on a system with a fast disk (e.g., SSD) and enough RAM is highly recommended, especially when you load a lot of plugins.
79
-
80
- ## Configuration
81
-
82
- ### General Rules
83
-
84
- - All configuration files must be placed in the ```{dataDir}/config``` subfolder.
85
- - Config files must be named after their plugin namespace.
86
- - The file format should be either ```.json``` or ```.js```. If a ```.js``` file is used, it must be in ES6 format and should export either a plain JavaScript object or a function. Both synchronous and asynchronous functions are supported. If it returns a function, this function will be called within its plugin scope and should return a plain JS object.
87
- - Other formats (```.yml```, ```.yaml```, and ```.toml```) can also be used by installing and loading the [bajoConfig](https://github.com/ardhi/bajo-config) plugin.
88
- - Other formats (```.yml```, ```.yaml``` and ```.toml```) can also be used by installing & loading [bajoConfig](https://github.com/ardhi/bajo-config) plugin
89
- - The order of precedence is ```.js``` > ```.json``` > ```.yml``` > ```.yaml``` > ```.toml```. This means that if a .js file exists, it will be used instead of a .json file or any other format.
90
-
91
- Example: bajo.json
92
- ```json
93
- {
94
- "env": "prod",
95
- "log": {
96
- "pretty": true,
97
- "timeTaken": true
98
- },
99
- "lang": "id"
100
- }
101
- ```
102
-
103
- ### Using Plugins
104
-
105
- Plugins are what make the Bajo Framework so great and flexible: they extend app features and functionalities!
106
-
107
- To use plugins, follow these steps:
108
-
109
- 1. Install it with ```npm install {package}```, where ```{package}``` is the plugin's package name. You can install as many plugins as you want; for a complete list of plugins, please [click here](ecosystem.md).
110
- 2. Optionally, create ```{dataDir}/config/{ns}.json``` to customize the plugin's settings, where ```{ns}``` is the namespace or plugin name.
111
- 3. Open or create ```{dataDir}/config/.plugins``` and list the plugin's ```{package}``` name in it, one per line.
112
-
113
- For example, the text below will load ```bajo-config```, ```bajo-extra```, and ```bajo-template```:
114
-
115
- ```text
116
- # .plugin file
117
- bajo-config
118
- bajo-extra
119
- bajo-template
120
- ```
121
-
122
- If you later decide to disable one or more plugins, you just need to remove them from the ```.plugins``` file or place a ```#``` hash mark in front of the package name and restart your app.
123
-
124
- > **Warning**: Please do not confuse ```{package}``` and ```{ns}```. The plugin package is the name of the JS package listed on npm, while ```{ns}``` is the namespace or plugin name, which is basically the camel-cased version of the plugin's package name.
125
-
126
- ### Environment Support
127
-
128
- Configuration file support for different environments is also available. All you need to do is create a ```{ns}-{env}.json``` file in your ```{dataDir}/config```, where:
129
-
130
- - ```{ns}```: the namespace/plugin name
131
- - ```{env}```: your desired environment (```dev``` or ```prod```)
132
- - App-wide settings with ```bajo-{env}.json``` are also possible.
133
-
134
- Bajo is smart enough to select which config file will be used based on the following order of precedence:
135
-
136
- 1. Use ```{ns}-{env}.json``` if the file exists.
137
- 2. If not, use ```{ns}.json```.
138
- 3. If that also doesn't exist, then use the plugin's default config values.
139
-
140
- ### Runtime Override
141
-
142
- You can easily override ANY key-value pair setting with environment variables and program argument switches. Bajo also supports [dotenv](https://github.com/motdotla/dotenv) with a ```.env``` file.
143
-
144
- The order of precedence is: environment variable > argument switches > config files > default, built-in values.
145
-
146
- All values (whether they come from environment variables, argument switches, or config files) will be parsed using [dotenv-parse-variables](https://github.com/ladjs/dotenv-parse-variables), so please make sure you visit the repository to fully understand how it works.
147
-
148
- #### dotenv
149
-
150
- - Create or open ```{appDir}/.env```
151
- - Use ```__``` (double underscores) as replacement for dots in an object.
152
- - ```DIR__DATA```: Sets the ```{dataDir}``` data directory.
153
- - ```DIR__TMP```: Sets ```{tmpDir}``` temporary directory.
154
- - For every key in ```{ns|bajo}.json```, use its snake-cased, upper-cased version. For example:
155
- - ```env: 'prod'``` → ```ENV=prod```
156
- - ```log.dateFormat: 'YYYY-MM-DD'``` → ```LOG__DATE_FORMAT=YYYY-MM-DD```
157
- - ```exitHandler: true``` → ```EXIT_HANDLER=true```
158
- - To override a plugin's config, prepend every key in the plugin's config with the snake-cased, upper-cased version of the namespace followed by a dot. For example:
159
- - ```key``` in ```myPlugin``` → ```MY_PLUGIN.KEY=...```
160
- - ```key.subKey.subSubKey``` in ```myPlugin``` → ```MY_PLUGIN.KEY__SUB_KEY__SUB_SUB_KEY=...```
161
-
162
- Example:
163
- ```text
164
- # .env file
165
- ENV=prod
166
- LOG__PRETTY=true
167
- LOG__TIME_TAKEN=true
168
- LANG=id
169
- ```
170
-
171
- #### Argument Switches
172
-
173
- - Use switches, for example: ```node index.js --xxx=one --yyy=two```
174
- - Use ```-``` as the replacement for dots in an object.
175
- - ```--dir-data```: Sets the ```{dataDir}``` data directory.
176
- - ```--dir-tmp```: Sets the ```{tmpDir}``` temporary directory.
177
- - For every key in ```{ns|bajo}.json```, add ```--``` prefix. For example:
178
- - ```env: 'prod'``` → ```--env=prod```
179
- - ```log.dateFormat: 'YYYY-MM-DD'``` → ```--log-dateFormat=YYYY-MM-DD```
180
- - ```exitHandler: true``` → ```--exitHandler```
181
- - To override a plugin's config, prepend every key in the plugin's config with the plugin name followed by a colon ```:```. For example:
182
- - ```key``` in ```myPlugin``` → ```--myPlugin:key=...```
183
- - ```key.subKey.subSubKey``` in ```myPlugin``` → ```--myPlugin:key-subKey-subSubKey=...```
184
-
185
- Example:
186
- ```bash
187
- $ node index.js --env=prod --log-pretty --log-timeTaken --lang=id
188
- ```
189
-
190
- ## System Hook
191
-
192
- A **hook** refers to a mechanism that allows you to inject a custom function to extend Bajo's functionality at specific points. These points are typically predefined by the framework, providing opportunities to execute code before, during, or after a particular operation.
193
-
194
- ### Usage
195
-
196
- In Bajo, hooks can be created anywhere very easily. Simply call the ```runHook``` method followed by the parameters you want to pass.
197
-
198
- The hook name is always in the form of [TNsPairs](global.html#TNsPathPairs), while its parameters are a rest parameter. This means you can pass any number of parameters to the function, or none at all.
199
-
200
- Example:
201
-
202
- 1. In your JavaScript file, add the following code snippet:
203
-
204
- ```javascript
205
- const { runHook } = this.app.bajo
206
- await runHook('main:sayHello', 'Don', 'Meri', { movie: 'Jumbo', year: 2025 })
207
- ````
208
- 2. Go to directory ```{appDir}/main/extend/bajo/hook```. Create one if it doesn't exist yet.
209
- 3. Create file ```main@say-hello.js``` in the directory above.
210
- 4. Enter these lines:
211
- ```javascript
212
- async function sayHello (...params) {
213
- const [mainChar, friend, payload] = params
214
- console.log(mainChar, friend, payload) // output: Don, Meri, { movie: 'Jumbo', year: 2025 }
215
- }
216
-
217
- export default sayHello
218
- ```
219
-
220
- Note the hook name and its associated file name:
221
-
222
- ```main:sayHello``` → ```main@say-hello.js```
223
-
224
- Because a colon (```:```) is prohibited in a file name, Bajo replaces it with the ```@``` symbol.
225
-
226
- During the boot process, Bajo will scan for hook files and load them into the hook list. When your ```runHook``` is executed, Bajo will find its related object from the list. If such a hook exists, its function handler will be called.
227
-
228
- ### Anatomy
229
-
230
- Many times, there are more than one handler listening for a particular hook name. Especially in a framework that uses plugins extensively like Bajo, many plugins can listen to one hook at the same time. This creates a problem with call order.
231
-
232
- To overcome this problem, Bajo gives you the opportunity to set a ```level```. Functions with a lower level will be called earlier. Functions with no level will be assigned level 999 by default.
233
-
234
- Now, change your ```main@say-hello.js``` file above to export an object instead of a function:
235
-
236
- ```javascript
237
- const sayHello = {
238
- level: 10, // <-- will get called early
239
- handler: async function (...params) {
240
- const [mainChar, friend, payload] = params
241
- console.log(mainChar, friend, payload) // output: Don, Meri, { movie: 'Jumbo', year: 2025 }
242
- }
243
- }
244
- ```
245
-
246
- ### Caveats
247
-
248
- Hooks give you a lot of flexibility and freedom, but you need to be aware of the following caveats:
249
-
250
- 1. You need to use an **asynchronous** function. Even if your function is synchronous, it will be called as an asynchronous one—and as you know, there is a performance degradation when using asynchronous operations
251
- 2. **Stay away** from using ```runHook``` inside a hook! Even though it's possible, your code will become unreadable and messy pretty soon.
252
- 3. It's hard to trace errors in a hook. Because of its sequential nature, if a handler that's called earlier than yours throws an error, your hook won't be called at all.
253
- 4. If you use so many plugins that use the hook system so extensively with so many files, your app's boot time can take much longer than it's supposed to.
254
-
255
- My advice is to **use it wisely**. Don't use hooks unless necessary; this will make your app or plugin clean and easy to understand.
256
-