App

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

Constructor

new App(optionsopt)

Parameters:
NameTypeAttributesDescription
optionsObject<optional>

App options.

Properties
NameTypeAttributesDescription
cwdstring<optional>

Set current working directory. Defaults to the script directory.

pluginsArray.<string><optional>

Array of plugins to load. If provided, it override the list in package.json and .plugins file.

configObject<optional>

Plugin's config object. If provided, plugin configs will no longer be read from its config files.

Members

applet :string

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

Type:
  • string

applets :Array

Applets container.

Type:
  • Array

args :Array.<string>

Program arguments.

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

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
See
  • module:Lib.parseArgsArgv

baseClass

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 anytime you want to create your own plugin, just use something like this:

class MyPlugin extends this.app.baseClass.Base {
  ... your class
}

boxen

Placeholder for boxen that will get imported from bajoCli later during boot process.

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
See
  • module:Lib.parseEnv

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:
  • TAppLib

log :Log

Instance of system log.

Type:

options :Object

Copy of provided options.

Type:
  • Object

pluginPkgs :Array

Plugin's package names container. This is the list of plugins to load. It is read from package.json and .plugins file by default, but you can override it by providing options.plugins at constructor.

Type:
  • Array

runAt :Date

Date/time when your app start.

Type:
  • Date

startPlugin

Start a plugin.

stopPlugin

Stop a plugin.

(static, constant) this.envs :TAppEnv

App environments.

Type:

(static, constant) this.mainNs :string

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

Type:
  • string
Default Value
  • 'main'

Methods

addPlugin(plugin, baseClassopt)

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

Parameters:
NameTypeAttributesDescription
pluginTPlugin

A valid bajo plugin.

baseClassObject<optional>

Base class definition.

(async) boot() → {App}

Boot process:

  • Parsing program arguments, options and environment values
  • Create Bajo instance & initialize it
  • Run in applet mode if -a or --applet is given

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

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

Fires:
  • bajo:afterBootCompleted
Returns:
Type: 
App

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.

Parameters:
NameTypeAttributesDescription
argsany<repeatable>

Variables to dump.

exit(signalopt)

Terminate the app and back to console.

Parameters:
NameTypeAttributesDefaultDescription
signalstring<optional>
SIGINT

Signal to send.

getAllNs() → {Array.<string>}

Get all loaded plugin namespaces.

Returns:
Type: 
Array.<string>

getAllPlugins() → {Array.<TPlugin>}

Get all plugins loaded plugins.

Returns:
Type: 
Array.<TPlugin>

getPlugin(name, silentopt) → {Object}

Get plugin by its namespace.

Parameters:
NameTypeAttributesDescription
namestring

Plugin name/namespace or alias.

silentboolean<optional>

If true, silently return undefined even on error.

Returns:

Plugin object.

Type: 
Object

getPluginDataDir(name, ensureDiropt) → {string}

Get plugin data directory

Parameters:
NameTypeAttributesDefaultDescription
namestring

Plugin name (namespace) or alias.

ensureDirboolean<optional>
true

Set true (default) to ensure directory is existed.

Returns:
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
Parameters:
NameTypeDescription
filestring

File path, see above for supported types.

Returns:

Resolved file path.

Type: 
string

getPlugins(nssopt) → {Array.<TPlugin>}

Get loaded plugins.

Parameters:
NameTypeAttributesDescription
nssArray.<string><optional>

Array of namespaces. If empty, it returns all loaded plugins.

Returns:
Type: 
Array.<TPlugin>

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

te(ns, text) → {boolean}

Check whether translation text/key exists

Parameters:
NameTypeDescription
nsstring

Namespace.

textstring

Text to translate.

Returns:
Type: 
boolean