App

App class. This is the root. This is where all plugins call it home.

Boot process:

  1. Parsing program arguments, options and environment values
  2. Create Bajo instance
  3. Building base config
  4. Building plugins
  5. Collect all config handler
  6. Building extra config
  7. Setup boot order
  8. Boot loaded plugins
  9. Attach exit handlers
  10. Run in applet mode if -a or --applet is given

After boot process is completed, event bajo:afterBootComplete is emitted.

If app mode is applet, it runs your choosen applet instead.

Constructor

new App(cwd)

Parameters:
NameTypeDescription
cwdstring

Current working dirctory

Members

applet :string

If app runs in applet mode, this will be the applet's name

Type:
  • string

applets :Array

Applets

Type:
  • Array

args :Array.<string>

Program arguments

$ node index.js arg1 arg2
...
console.log(this.args) // it should print: ['arg1', 'arg2']
Type:
  • Array.<string>

argv :Object

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.

$ 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 } }
// }
Type:
  • Object

configHandlers :Array.<TAppConfigHandler>

Config handlers.

By default, there are two built-in handlers available: .js and .json. Use plugins to add more, e.g bajo-config lets you to use .yaml/.yml and .toml

Type:

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.

Example:

  • MY_KEY=secret{ _: { myKey: 'secret' } }
  • MY_KEY__SUB_KEY=supersecret{ _: { myKey: { subKey: 'supersecret' } } }
  • MY_NS.MY_NAME=John{ myNs: { myName: 'John' } }
Type:
  • Object

getConfigFormats

Helper method to list all supported config formats

lib :TAppLib

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!

Example:

const { camelCase, kebabCase } = this.app.lib._
console.log(camelCase('Elit commodo sit et aliqua'))
Type:

log :Log

Instance of system log

Type:

pluginClass

All plugin's class definitions are saved here as key-value pairs with plugin name as its key. The special key base is for Base's class so anytime you want to create your own plugin, just use something like this:

class MyPlugin extends this.app.pluginClass.base {
  ... your class
}

pluginPkgs :Array

List of all loaded plugin's package names

Type:
  • Array

runAt :Date

Date/time when your app start

Type:
  • Date

(static, constant) envs :TAppEnv

App environments

Type:

(static, constant) mainNs :string

Your main namespace. And yes, you suppose to NOT CHANGE this

Type:
  • string
Default Value
  • 'main'

Methods

addPlugin(plugin, pluginClassopt)

Add and save plugin and it's class definition (if provided)

Parameters:
NameTypeAttributesDescription
pluginTPlugin

A valid bajo plugin

pluginClassObject<optional>

Plugin's class definition

(async) boot() → {App}

Booting the app.

Returns:
Type: 
App

dump(…args)

Dumping variable on screen. Like console.log but with max 10 depth.

Parameters:
NameTypeAttributesDescription
argsany<repeatable>

any arguments passed will be displayed on screen. If the last argument is a boolean 'true', app will quit rightaway

exit(signalopt)

Terminate the app and back to console

Parameters:
NameTypeAttributesDefaultDescription
signalstring<optional>
SIGINT

Signal to send

getAllNs() → {Array.<string>}

Get all loaded plugin namesspaces

Returns:
Type: 
Array.<string>

loadIntl(ns)

Load internationalization & languages files for particular plugin

Parameters:
NameTypeDescription
nsstring

Plugin name

t(ns, text, …params) → {string}

Translate text and interpolate with given args.

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 name already

... within your main plugin
const translated = this.app.t('main', 'My cute cat is %s', 'purring')
// or
const translated = this.t('My cute cat is %s', 'purring')
Parameters:
NameTypeAttributesDescription
nsstring

Namespace

textstring

Text to translate

paramsany<repeatable>

Arguments

Returns:
Type: 
string