Bajo

The Core. The main engine. The one and only plugin that control app's boot process and making sure all other plugins working nicely.

Constructor

new Bajo(app)

Constructor.

Parameters:
NameTypeDescription
appApp

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

Members

alias :string

Alias

Type:
  • string

config :Bajo.TObject

Config object.

Type:
  • Bajo.TObject
See
  • config

hooks :Array.<Bajo.THook>

Hooks container.

Type:
  • Array.<Bajo.THook>

whiteSpace :Array.<string>

White space characters

Type:
  • Array.<string>

Methods

breakNsPath(name, checkNsopt) → {TNsPathResult}

Break name to its namespace & path infos

Parameters:
NameTypeAttributesDefaultDescription
nameTNsPathPairs | string

Name to break

checkNsboolean<optional>
true

If true (default), namespace will be checked for its validity

Returns:
Type: 
TNsPathResult

breakNsPathFromFile(options) → {Object}

Break file path to its namespace & path infos.

Parameters:
NameTypeDescription
optionsObject

Options object

Properties
NameTypeAttributesDescription
filestring<optional>

File path to break

dirstring<optional>

Base directory to remove from file path

nsstring<optional>

Namespace to use. If not provided, will be extracted from file path

suffixstring<optional>

Suffix to remove from file path

getTypeboolean<optional>

Whether to extract type from file path

Returns:

Namespace and path information

Type: 
Object

(async) buildCollections(options) → {Array.<Object>}

Method to transform config's array or object into an array of collection.

Parameters:
NameTypeDescription
optionsObject

Options.

Properties
NameTypeAttributesDefaultDescription
nsstring<optional>

Namespace. If not provided, defaults to bajo

handlerfunction<optional>

Handler to call while building the collection item.

dupChecksArray.<string><optional>
[]

Array of keys to check for duplicates.

containerstring

Key used as container name.

useDefaultNameboolean<optional>
true

If true (default) and name key is not provided, returned collection will be named default.

Fires:
  • bajo:beforeBuildCollection
  • bajo:afterBuildCollection
Returns:

The collection

Type: 
Array.<Object>

buildNsPath(optionsopt) → {TNsPathPairs}

Build ns/path pairs.

Parameters:
NameTypeAttributesDefaultDescription
optionsobject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
nsstring<optional>
''

Namespace

subNsstring<optional>

Sub namespace

subSubNsstring<optional>

Sub sub namespace

pathstring<optional>

Path

Returns:

Ns/path pairs

Type: 
TNsPathPairs

(async) callHandler(name, …argsopt) → {any}

Calling any plugin's method by its name:

  • If name is a string, the corresponding plugin's method will be called with passed args as its parameters
  • If name is a plugin instance, this will be used as the scope instead. The first args is now the handler name and the rest are its parameters
  • If name is a function, this function will be run under scope with the remaining args
  • If name is an object and has handler key in it, this function handler will be instead
Parameters:
NameTypeAttributesDescription
nameTNsPathPairs | Object | function

Method's name, function handler, plain object or plugin instance.

argsany<optional>
<repeatable>

One or more arguments passed as parameter to the handler.

Returns:

Returned value.

Type: 
any

(async) eachPlugins(handler, optionsopt) → {any}

This function iterates through all loaded plugins and call the provided handler scoped as the running plugin. And an object with the following key serves as its parameter:

  • file: file matched the glob pattern
  • dir: plugin's base directory
Parameters:
NameTypeAttributesDefaultDescription
handlerfunction

Function handler. Can be an async function. Scoped to the running plugin.

optionsstring | Object<optional>
{}

Options. If a string is provided, it serves as the glob pattern, otherwise:

Properties
NameTypeAttributesDefaultDescription
globstring | Array.<string><optional>

Glob pattern. If provided,

useBajoboolean<optional>
false

If true, add bajo to the running plugins too.

prefixstring<optional>
''

Prepend glob pattern with prefix.

noUnderscoreboolean<optional>
true

If true (default), matched file with name starts with underscore is ignored.

returnItemsany<optional>

If true, each value of returned handler call will be saved as an object with running plugin name as its keys.

Returns:
Type: 
any

format(value, typeopt, optionsopt) → {string}

Format value.

Parameters:
NameTypeAttributesDefaultDescription
valueany

Value to format.

typestring<optional>

Data type to use. See TBajoDataType for acceptable values. If not provided, return the untouched value.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
emptyValuestring<optional>
''

Empty value to use if function resulted empty. Defaults to the one from your config.

withUnitboolean<optional>
true

Return with its unit appended.

langstring<optional>

Format value according to this language. Defaults to the one you set in config.

latitudestring<optional>

If Bajo Spatial is loaded and data type is a double or float, then format it as latitude in degree, minute, second.

longitudestring<optional>

If Bajo Spatial is loaded and data type is a double or float, then format it as longitude in degree, minute, second.

Returns:

Formatted value.

Type: 
string

formatByType(type, value, dataTypeopt, optionsopt) → {Array|string}

Format value by type.

Parameters:
NameTypeAttributesDefaultDescription
typestring

Format type. See TBajoFormatType for acceptable values.

valueany

Value to format.

dataTypestring<optional>

Value's data type. See TBajoDataType for acceptable values.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
withUnitboolean<optional>
true

Return with its unit appended.

langstring<optional>

Format value according to this language. Defaults to the one you set in config.

Returns:

Return string if withUnit is true. Otherwise is an array of [value, unit, separator].

Type: 
Array | string

(async) fromJs(file, optionsopt) → {Object}

Read and parse JavaScript file.

Parameters:
NameTypeAttributesDefaultDescription
filestring

File to read and parse.

optionsObject<optional>
{}

Options.

Returns:

Parsed JavaScript object.

Type: 
Object

fromJson(data, optionsopt) → {Object}

Read and parse JSON string or object.

Parameters:
NameTypeAttributesDefaultDescription
datastring

Filename to load from or JSON string to parse.

optionsObject<optional>
{}

Options.

Returns:

Parsed JSON object.

Type: 
Object

fromYaml(text, options) → {object}

Parse YAML text

Parameters:
NameTypeDescription
textstring

Text to be parsed

optionsobject

Options object

Returns:

Parsed object

Type: 
object

fromYml(text, options) → {object}

Parse YML text. Alias for fromYaml.

Parameters:
NameTypeDescription
textstring

Text to be parsed

optionsobject

Options object

Returns:

Parsed object

Type: 
object

getMethod(name, thrownopt) → {function}

Get class method by name.

Parameters:
NameTypeAttributesDefaultDescription
namestring

Name in format ns:methodName.

thrownboolean<optional>
true

If true (default), throw exception in case of error.

Returns:

Class method.

Type: 
function

getModuleDir(pkgName, withGlobalDiropt) → {string}

Get module directory.

Parameters:
NameTypeAttributesDefaultDescription
pkgNamestring

Package name to find.

withGlobalDirboolean<optional>
true

Whether to include the global module directory.

Returns:

Return absolute package directory.

Type: 
string

getUnitFormat(optionsopt) → {TBajoFormatResult}

Get unit format.

Parameters:
NameTypeAttributesDefaultDescription
optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDescription
langstring<optional>

Language to use. Defaults to the one you set in config.

unitSysstring<optional>

Unit system to use. Defaults to language's unit system or metric if unspecified.

Returns:

Returned value.

Type: 
TBajoFormatResult

(async) importModule()

Import file/module from any loaded plugins.

Method proxy from module:Helper.importModule

(async) importPkg(…pkgs) → {Object|Array}

Import one or more packages belongs to a plugin.

If the last arguments passed is an object, this object serves as options object:

  • returnDefault: should return package's default export. Defaults to true
  • throwNotFound: should throw if package is not found. Defaults to false
  • noCache: always use fresh import. Defaults to false
  • asObject: see below. Defaults to false

Return value:

  • if options.asObject is true (default false), return as object with package's names as it's keys
  • Otherwise depends on how many parameters are provided, it should return the named package or an array of packages

Example: you want to import delay and chalk from bajo plugin because you want to use it in your code `javascript const { importPkg } from this.app.bajo const [delay, chalk] = await importPkg('bajo:delay', 'bajo:chalk')

await delay(1000) ... `

Parameters:
NameTypeAttributesDescription
pkgsTNsPathPairs<repeatable>

One or more packages in format {ns}:{packageName}.

Returns:

See above.

Type: 
Object | Array

(async) init()

Initialization:

  1. Building base config
  2. Building plugins
  3. Collect all config handler
  4. Building extra config
  5. Setup boot order
  6. Boot loaded plugins
  7. Attach exit handlers

isLogInRange(level) → {boolean}

Check whether log level is within log's app current level.

Parameters:
NameTypeDescription
levelstring

Level to check. See TLogLevels for more.

Returns:
Type: 
boolean

isValidApp(dir, returnPkgopt) → {boolean|Object}

Check whether directory is a valid Bajo app.

Parameters:
NameTypeAttributesDescription
dirstring

Directory to check.

returnPkgboolean<optional>

Set true to return its package.json content.

Returns:
Type: 
boolean | Object

isValidPlugin(dir, returnPkgopt) → {boolean|Object}

Check whether directory is a valid Bajo plugin.

Parameters:
NameTypeAttributesDescription
dirstring

Directory to check.

returnPkgboolean<optional>

Set true to return its package.json content.

Returns:
Type: 
boolean | Object

join(array, options) → {string}

Human friendly join array of items.

Parameters:
NameTypeDescription
arrayArray.<any>

Array to join

optionsstring | Object

If provided and is a string, it will be used as separator.

Properties
NameTypeAttributesDefaultDescription
separatorstring<optional>
', '

Separator to use.

lastSeparatorstring<optional>
and

Text to use as the last separator.

Returns:
Type: 
string

numUnit(valueopt, defUnitopt) → {string}

Return its numeric portion of a value.

Parameters:
NameTypeAttributesDefaultDescription
valuestring<optional>
''

Value to get its numeric portion.

defUnitstring<optional>
''

Default unit if value doesn't have one.

Returns:
Type: 
string

(async) readAllConfigs(path, optionsopt) → {Object}

Read all config files from path.

Parameters:
NameTypeAttributesDefaultDescription
pathstring

Base path to start looking config files.

optionsObject<optional>
{}

Options.

Returns:

Merged configuration object.

Type: 
Object

readAsConfig(input, extsopt, optionsopt) → {Object|Array|null}

Read config using all registered config handlers. The first handler that returns a valid object or array will be used.

Parameters:
NameTypeAttributesDefaultDescription
inputstring

The input string to be processed by the config handlers.

extsArray.<string><optional>

Optional array of extensions to filter the config handlers. If provided, only handlers with matching extensions will be used.

optionsobject<optional>
{}

Options to be passed to the config handlers.

Returns:

The result from the first successful config handler, or null if none succeed.

Type: 
Object | Array | null

(async) readConfig(file, optionsopt) → {Object}

Read and parse file as config object. Supported types: .js, .json and .yml/.yaml. More supports can be added using plugin. bajo-config gives you additional supports for .yml, .yaml and .toml file.

If file extension is .*, it will be auto detected and parsed accordingly

Parameters:
NameTypeAttributesDefaultDescription
filestring

File to read and parse.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
ignoreErrorboolean<optional>

Any exception will be silently discarded.

nsstring<optional>

If given, use this as the scope.

patternstring<optional>

If given and auto detection is on (extension is .*), it will be used for instead the default one.

defValueObject<optional>
{}

Default value to use if value returned empty.

parserOptsObject<optional>
{}

Parser setting.

globOptsObject<optional>
{}

fast-glob options.

Returns:
Type: 
Object

readJson(file, thrownNotFoundopt) → {Object}

Read and parse json file.

Parameters:
NameTypeAttributesDefaultDescription
filestring

File to read.

thrownNotFoundboolean<optional>
false

If true, silently ignore if file is not found.

Returns:
Type: 
Object

(async) runHook(hookName, …argsopt) → {Array}

Run named hook/event.

Parameters:
NameTypeAttributesDescription
hookNameTNsPathPairs

Name of the hook to run.

argsany<optional>
<repeatable>

Argument passed to the hook function.

Returns:

Array of hook execution results.

Type: 
Array

(async) saveAsDownload(file, item, printSavedopt) → {string}

Save item as file in Bajo's download directory. That is a directory inside your Bajo plugin's data directory.

If file exists already, file will automatically be renamed incrementally.

Parameters:
NameTypeAttributesDefaultDescription
filestring

File name.

itemObject

Item to save.

printSavedboolean<optional>
true

Print info on screen.

Returns:

Full file path.

Type: 
string

toJson(data, optionsopt) → {string}

Convert data to JSON string.

Parameters:
NameTypeAttributesDefaultDescription
dataObject

Data to convert to JSON string.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
writeToFileboolean<optional>
false

If true, write the JSON string to a file.

saveAsFilestring<optional>

The file path to save the JSON string if writeToFile is true.

Returns:

JSON string

Type: 
string

toYaml(object, options) → {string}

Convert object to YAML string

Parameters:
NameTypeDescription
objectobject

Object to be converted

optionsobject

Options object

Returns:

YAML string

Type: 
string

toYml(object, options) → {string}

Convert object to YML string. Alias for toYaml.

Parameters:
NameTypeDescription
objectobject

Object to be converted

optionsobject

Options object

Returns:

YML string

Type: 
string

Type Definitions

TDataType

Supported data types.

Type:
  • Object
Properties
NameTypeDescription
0string

string

1string

float

2string

double

3string

integer

4string

smallint

5string

date

6string

time

7string

datetime

8string

array

9string

object

10string

auto

TFormatResult

Object returned by bajo:getUnitFormat.

Type:
  • Object
Properties
NameTypeDescription
unitSysstring

Unit system.

formatObject

Format object.

TFormatType

General format types,

Type:
  • Object
Properties
NameTypeDescription
0string

speed

1string

distance

2string

area

3string

degree

TNsPathPairs

Name based {ns}:{path} format.

Type:
  • string

TNsPathResult

Object returned by bajo:breakNsPath.

Type:
  • Object
Properties
NameTypeAttributesDescription
nsstring

Namespace

subNsstring<optional>

Sub namespace

subSubNsstring<optional>

Sub of sub namespace

pathstring

Path without query string or hash

fullPathstring

Full path, including query string and hash