Constructor
new App(optionsopt)
Constructor.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
options | App. | <optional> | {} | Options object. |
- Source
Members
applet :string
If app runs in applet mode, this will be the applet's name.
- string
- Source
applets :Array
Applets container.
- Array
- Source
args :Array.<string>
Parsed program arguments.
- Array.<string>
- Source
- See
- module:Helper.parseArgsArgv
$ node index.js arg1 arg2
...
console.log(this.args) // it should print: ['arg1', 'arg2']argv :Object
Parsed program options.
- Dash (
-) breaks the string into object keys - While colon (
:) is used as namespace separator. If no namespace found, it is saved under_key.
Values are parsed automatically. See dotenv-parse-variables for details.
- Object
- Source
- See
- module:Helper.parseArgsArgv
$ node index.js --my-name-first=John --my-name-last=Doe --my-birthDay=secret --nameSpace:path-subPath=true
...
// {
// _: {
// my: {
// name: { first: 'John', last: 'Doe' },
// birthDay: 'secret'
// }
// },
// nameSpace: { path: { subPath: true } }
// }baseClass :Object
All plugin's base class are saved here as key-value pairs with plugin name as its key. The special key Base && Tools is for Base & Tools class so that anytime you want to create your own plugin, you can just write something like this:
`javascript class MyPlugin extends this.app.baseClass.Base { ... your class }
- Object
- Source
boxen :Object
Placeholder for boxen that will get imported from bajoCli later during boot process.
- Object
- Source
cache :Cache
Cache instance. This is used to store temporary data in memory for faster access.
- Source
configHandlers :Array.<App.TConfigHandler>
Config handlers.
By default, there are three built-in handlers: .js, .json and .yml/.yaml. Use plugins to add more, e.g bajo-config lets you to use .toml.
- Array.<App.TConfigHandler>
- Source
envVars :Object
Environment variables. Support dotenv (.env) file too!
- Underscore (
_) translates key to camel-cased one - Double underscores (
__) breaks the key into object keys - While dot (
.) is used as namespace separator. If no namespace found, it is saved under_key.
Values are also parsed automatically using dotenv-parse-variables.
E.g.:
MY_KEY=secret→{ _: { myKey: 'secret' } }MY_KEY__SUB_KEY=supersecret→{ _: { myKey: { subKey: 'supersecret' } } }MY_NS.MY_NAME=John→{ myNs: { myName: 'John' } }
- Object
- Source
- See
- module:Helper.parseEnv
envs :App.TEnv
App environments.
- Source
lib :App.TLib
Gives you direct access to the most commonly used 3rd party library in a Bajo based app. No manual import necessary, always available, anywhere, anytime!
- Source
const { camelCase, kebabCase } = this.app.lib._
console.log(camelCase('Elit commodo sit et aliqua'))log :Log
Instance of system log.
- Source
mainNs :string
Main namespace.
- string
- Source
options :App.TOptions
Copy of provided options.
- Source
pluginPkgs :Array.<string>
Plugin's package names container. This is the list of plugins to load.
- Array.<string>
- Source
runAt :Date
Date/time when your app start.
- Date
- Source
Methods
addPlugin(plugin, baseClassopt)
Add and save plugin and it's base class definition (if provided).
| Name | Type | Attributes | Description |
|---|---|---|---|
plugin | TPlugin | A valid bajo plugin. | |
baseClass | Object | <optional> | Base class definition. |
- Source
dump(…args)
Dumping variable on screen. Like console.log with configurable options. Useful for quick debugging and testing. You can also use it to dump variables in production without worrying about performance because it is using Bajo's built-in cache to store the result of util's inspect, so it will only be processed once for each unique variable.
Any argument passed to this method will be displayed on screen. If the last argument is a boolean true, app will quit rightaway after dumping.
If you have bajoCli plugin installed, variables will be displayed in a nice box using boxen package. Otherwise, it will fallback to console.log with util's inspect result.
To have more control on how the variable is displayed, you can set options in Bajo's config under dump key. See Bajo#config for details.
| Name | Type | Attributes | Description |
|---|---|---|---|
args | any | <repeatable> | Variables to dump. |
- Source
exit(signalopt)
Terminate the app and back to console.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
signal | string | | <optional> | SIGINT | Signal to send. Set to |
- Source
getAllNs() → {Array.<string>}
Get all loaded plugin namespaces.
- Source
- Type:
- Array.<string>
getAllPlugins() → {Array.<TPlugin>}
Get all plugins loaded plugins.
- Source
- Type:
- Array.<TPlugin>
getConfigFormats(noDotopt) → {Array.<string>}
Helper method to list all supported config formats.
| Name | Type | Attributes | Description |
|---|---|---|---|
noDot | boolean | <optional> | If |
- Source
- Type:
- Array.<string>
getPlugin(name, silentopt) → {Object}
Get plugin by its namespace.
| Name | Type | Attributes | Description |
|---|---|---|---|
name | string | Plugin name/namespace or alias. | |
silent | boolean | <optional> | If |
- Source
Plugin object.
- Type:
- Object
getPluginDataDir(name, ensureDiropt) → {string}
Get plugin data directory
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name | string | Plugin name (namespace) or alias. | ||
ensureDir | boolean | <optional> | true | Set |
- Source
- Type:
- string
getPluginFile(file) → {string}
Resolve file path from:
- local/absolute file
- TNsPath (
myPlugin:/path/to/file.txt) - file under node_modules, e.g.
myPlugin:node_modules/some-package/file.txt
| Name | Type | Description |
|---|---|---|
file | string | File path, see above for supported types. |
- Source
Resolved file path.
- Type:
- string
getPlugins(nssopt) → {Array.<TPlugin>}
Get loaded plugins.
| Name | Type | Attributes | Description |
|---|---|---|---|
nss | Array.<string> | <optional> | Array of namespaces. If empty, it returns all loaded plugins. |
- Source
- Type:
- Array.<TPlugin>
loadIntl(ns)
Load internationalization & languages files for particular plugin.
| Name | Type | Description |
|---|---|---|
ns | string | Plugin name (namespace) |
- Source
(async) run() → {Promise.<App>}
Run application:
- Parsing program arguments, options and environment values
- Create Bajo instance & initialize it
- Run in applet mode if
-aor--appletis given
After boot process is completed, event bajo:afterBootCompleted is emitted.
If app mode is applet, it runs your choosen applet instead.
- Source
App instance.
- Type:
- Promise.<App>
startPlugin(ns, …args)
Start a plugin.
| Name | Type | Attributes | Description |
|---|---|---|---|
ns | string | Plugin namespace. | |
args | any | <repeatable> | Arguments to pass to the plugin's start method |
- Source
stopPlugin(ns, …args)
Stop a plugin.
| Name | Type | Attributes | Description |
|---|---|---|---|
ns | string | Plugin namespace. | |
args | any | <repeatable> | Arguments to pass to the plugin's stop method |
- Source
t(ns, text, …params) → {string}
Translate text and interpolate with given params.
If the last parameter is an object with lang key, it will use that language instead of the default one.
There is a shortcut to this method attached on all plugins. You'll normally call that shorcut instead of this method, because it is bound to plugin's namespace already
| Name | Type | Attributes | Description |
|---|---|---|---|
ns | string | Namespace | |
text | string | Text to translate | |
params | any | <repeatable> | Arguments |
- Source
- Type:
- string
... within your main plugin
const translated = this.app.t('main', 'My cute cat is %s', 'purring')
// or within your plugin
const translated = this.t('My cute cat is %s', 'purring')te(ns, text) → {boolean}
Check whether translation text/key exists.
| Name | Type | Description |
|---|---|---|
ns | string | Namespace |
text | string | Text to translate |
- Source
- Type:
- boolean
Type Definitions
TConfigHandler
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
ns | string | Namespace | |
ext | string | File extension | |
readHandler | App. | <optional> | Function to call for reading |
writeHandler | App. | <optional> | Function to call for writing |
- Source
TEnv
- Object
| Name | Type | Default | Description |
|---|---|---|---|
dev | string | development | |
prod | string | production |
- Source
TLib
- Object
| Name | Type | Description |
|---|---|---|
_ | Object | Access to lodash. |
fs | Object | Access to fs-extra. |
fastGlob | Object | Access to fast-glob. |
sprintf | Object | Access to sprintf. |
aneka | Object | Access to aneka. |
outmatch | Object | Access to outmatch. |
dayjs | Object | Access to dayjs with utc & customParseFormat plugin already applied. |
freeze | Object | |
findDeep | Object |
- Source
- See
TOptions
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
cwd | string | <optional> | Set current working directory. Defaults to the script directory |
plugins | Array.<string> | <optional> | Array of plugins to load. If provided, it override the list in |
config | Object | <optional> | Plugin's config object. If provided, plugin configs will no longer be read from its config files |
- Source
readHandler(text, options) → {object}
| Name | Type | Description |
|---|---|---|
text | string | Text to be parsed |
options | object | Options object |
- Source
Parsed object
- Type:
- object
writeHandler(data, optionsopt) → {string}
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data | Object | Data to be stringified | |||||||||||||||||
options | Object | <optional> | {} | Options Properties
|
- Source
Stringified result
- Type:
- string