Plugin

The root plugin class. All Bajo plugin classes inherit from this class respectfully.

This class provides the basic structure and functionality for all plugins in the Bajo framework. It includes methods for configuration management, error handling, logging, and more.

Only two direct descendants of this class are allowed:

  • Bajo - Bajo core plugin class, responsible for system wide setup and boot process. You should not touch this obviously
  • Base - Base plugin class your own plugin should inherit from

Constructor

new Plugin(pkgName, app)

Constructor.

Parameters:
NameTypeDescription
pkgNamestring

Package name (the one in package.json)

appObject

App instance reference. Usefull to call app method inside a plugin

Members

alias :string

Plugin alias. Derived plugin must provide its own, unique alias. If it left blank, Bajo will provide this automatically (by using the kebab-cased version of plugin name).

By convention, plugin alias should be all lower case, alphanumeric, and without any space or special character except -. It should be unique across all plugins in the Bajo framework, as it is used to identify the plugin in the system beside its namespace.

Type:
  • string

app :App

Reference to the app instance

Type:

config :TConfig

Configuration object.

Type:
See
  • config

log :Log

Shortcut to App#log with prefix parameter set to this plugin namespace.

Type:

ns :string

Namespace (ns) or plugin's name. It is the camel cased version of plugin's package name.

Type:
  • string

pkgName :string

Package name, the one from package.json.

Type:
  • string

Methods

bindThis(…names) → {void}

Force bind methods to this context.

Since JavaScript's this is dynamic, this method is useful to ensure that the methods always refer to the correct instance of the class.

Typically, you would call this method in the constructor of your plugin class, passing an array of method names or imported functions that you want to bind.

Parameters:
NameTypeAttributesDescription
namesstring | function<repeatable>

Method's names or function references to bind to this context

Returns:
Type: 
void

(async) dispose() → {Promise.<void>}

Dispose internal references.

Returns:
Type: 
Promise.<void>

dump(…args) → {void}

Shortcut to this.app.dump().

Parameters:
NameTypeAttributesDescription
args*<repeatable>

Arguments

Returns:
Type: 
void

error(msg, …argsopt) → {Err}

Create an instance of Err object by providing an error message and optional arguments. Error instance will then be displayed on console and returned so you can chain it with other methods if you want.

Typically, you would use this method to throw an error and the framework will handle it gracefully.

This method is a shortcut to create a new Err instance.

Parameters:
NameTypeAttributesDescription
msgstring

Error message

args*<optional>
<repeatable>

Argument variables you might want to add to the error object

Returns:

Err instance

Type: 
Err

fatal(msg, …argsopt) → {void}

Same as Plugin#error but will forcefully terminate the process after printing the error to console.

Parameters:
NameTypeAttributesDescription
msgstring

Error message

args*<optional>
<repeatable>

Argument variables you might want to add to the error object

Returns:
Type: 
void

getConfig(pathopt, optionsopt) → {Object}

Get plugin's configuration object's value.

Note: Configuration object is frozen after boot process, so you can't modify it at runtime. If you want to change its values, you need to do it in the config file, program options or via environment variables. Hooks are also available to modify the configuration before the boot process.

Parameters:
NameTypeAttributesDefaultDescription
pathstring<optional>

dot separated config path (think of lodash's 'get'). If not provided, the full config will be given

optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
defValueany<optional>
{}

Default value to use if returned object is undefined

omitArray.<string><optional>
[]

Omit these keys from returned object

noCloneboolean<optional>
false

Set true to NOT clone returned object

Returns:

Returned object. If no path provided, the whole config object is returned

Type: 
Object

getPkgInfo(diropt, keysopt) → {Object}

Get package info. Basically it reads the package.json file and returns the requested fields

Parameters:
NameTypeAttributesDefaultDescription
dirstring<optional>

Package directory. Defaults to the current plugin's package dir

keysArray<optional>
['name', 'version', 'description', 'author', 'license', 'homepage', 'bajo']

Field keys to be use. Set empty to use all keys

Returns:

Package info object

Type: 
Object

t(text, …args) → {string}

Translate text and interpolate with given args.

Shortcut to App#t with ns parameter set to this plugin namespace.

Parameters:
NameTypeAttributesDescription
textstring

Text to translate

args*<repeatable>

Arguments to interpolate to text

Returns:
Type: 
string

te(text, …args) → {boolean}

Check whether translation text (key) exists.

Shortcut to App#te with ns parameter set to this plugin namespace.

Parameters:
NameTypeAttributesDescription
textstring

Text to translate

args*<repeatable>

Arguments to interpolate to text

Returns:
Type: 
boolean