Constructor
new Bajo(app)
Constructor.
| Name | Type | Description |
|---|---|---|
app | App | App instance. Usefull to call app method inside a plugin. |
- Source
Members
alias :string
Alias. Read-only
- string
- Default Value
- 'bajo'
- Source
config :Bajo.TConfig
Configuration object
- Source
- See
- config
hooks :Array.<Bajo.THook>
Hooks container. This is where all hooks definition are stored.
- Array.<Bajo.THook>
- Source
whiteSpace :Array.<string>
Array of white space characters. Used to trim strings
- Array.<string>
- Source
Methods
(async) bootApp()
Bajo boot process. This method is called by the App class during the app's boot process:
- Ensure directories are there and exist
- Collect all plugins available plugins
- Collect all config handlers from all loaded plugins
- Build configuration object
- Building plugins listed in
package.jsonor.pluginsfile - Determining the boot order
- Ensure the uniqueness of all plugins' name and alias
- Ensure all plugins dependencies are met
- Collect all hooks from all loaded plugins
- Run all plugins according to the boot order
- And finally attaching all exit handlers it could find
- Source
breakNsPath(name, checkNsopt) → {Bajo.TNsPathResult}
Break name to their components.
If path starts with //, e.g. name is https://example.com, it will be treated as a URL and returned as an object with only path property filled with the original name.
If query string is present in the path, it will be parsed and returned as an object with qs property containing the parsed query string.
If path contains parameters in the format of :key|value or {key|value}, they will be extracted and returned as an object with params property containing the extracted parameters.
This method is one of the most used method in Bajo. It is because every files, names, resources, and even commands are all identified by their namespace and path.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name | Bajo. | Name in format | ||
checkNs | boolean | <optional> | true | If |
- Source
- Type:
- Bajo.
TNsPathResult
breakNsPathFromFile(options) → {Object}
Break file path to its namespace & path infos.
| Name | Type | Description | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | Options object Properties
|
- Source
Namespace and path information
- Type:
- Object
(async) buildCollections(optionsopt) → {Array.<Object>}
Method to transform config's array or object into a collection of objects with uniformed structure. This is useful to build a collection of items from config's array or object, e.g. a collection of commands, routes, etc.
You typically also provide a handler function to transform each item in the collection. The handler function will be called with an object containing the following keys:
item: the current item in the collectionindex: the index of the current item in the collectioncfg: the entire config object
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | <optional> | {} | Options Properties
|
- Source
Returned collection of objects
- Type:
- Array.<Object>
buildNsPath(optionsopt) → {TNsPathPairs}
Build ns/path pairs.
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | object | <optional> | {} | Options object Properties
|
- Source
Ns/path pairs
- Type:
- TNsPathPairs
(async) callHandler(name, …argsopt) → {Promise.<*>}
Calling a function handler in any plugins:
- 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
handlerkey in it, this function handler will be instead
| Name | Type | Attributes | Description |
|---|---|---|---|
name | TNsPathPairs | | Method's name, plugin instance, function handler or plain object. See above for details | |
args | * | <optional> <repeatable> | One or more arguments passed as parameter to the handler |
- Source
Returned value
- Type:
- Promise.<*>
(async) eachPlugins(handler, optionsopt) → {Promise.<Object>}
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 patterndir: plugin's base directory
Inside the handler, return false to break the loop, return undefined to skip the current item, or return any value to be saved in the result object.
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
handler | function | Function handler. Can be an async function. Scoped to the running plugin | ||||||||||||||||||||||||||||||||
options | string | | <optional> | {} | Options. If a string is provided, it serves as the glob pattern as described below, otherwise: Properties
|
- Source
Object with each plugin's name as its key and the returned value from the handler as its value
- Type:
- Promise.<Object>
format(value, typeopt, optionsopt) → {string}
Format value.
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | * | Value to format. | ||||||||||||||||||||||||||||||||
type | string | <optional> | Data type to use. See TBajoDataType for acceptable values. If not provided, return the untouched value. | |||||||||||||||||||||||||||||||
options | Object | <optional> | {} | Options. Properties
|
- Source
Formatted value.
- Type:
- string
formatByField(field, value, dataTypeopt, optionsopt) → {Array|string}
Format value by field name.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
field | string | Format field name. See TBajoFormatField for acceptable values. | |||||||||||||||||
value | * | Value to format. | |||||||||||||||||
dataType | string | <optional> | Value's data type. See TBajoDataType for acceptable values. | ||||||||||||||||
options | Object | <optional> | {} | Options. Properties
|
- Source
Return string if withUnit is true. Otherwise is an array of [value, unit, separator].
- Type:
- Array |
string
(async) fromJs(file) → {Object}
Read and parse JavaScript file.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
file | string | File to read and parse | ||
options.readFromFile | boolean | <optional> | false | Ignored for this method. Always read from file. |
options.throwNotFound | boolean | <optional> | false | If |
options.defValue | object | <optional> | {} | Default value to use if value returned empty |
options.parserOpts | object | <optional> | {} | Options to be passed if file exports a function |
- Source
Parsed JavaScript object
- Type:
- Object
(async) fromJson(text, optionsopt) → {Object}
Parse JSON string or read and parse JSON file.
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
text | string | Text to be parsed or file path to be read if | |||||||||||||||||||||||||||
options | object | | <optional> | {} | Options object. If a boolean is provided, it will be treated as Properties
|
- Source
- See
Parsed object.
- Type:
- Object
(async) fromYaml(text, optionsopt) → {object}
Parse YAML text or read and parse YAML file.
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
text | string | Text to be parsed or file path to be read if | |||||||||||||||||||||||||||
options | object | | <optional> | {} | Options object. If a boolean is provided, it will be treated as Properties
|
- Source
- See
Parsed object
- Type:
- object
(async) fromYml(text, optionsopt) → {object}
Parse YML text or read and parse YML file. Alias for Bajo#fromYaml.
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
text | string | Text to be parsed or file path to be read if | |||||||||||||||||||||||||||
options | object | | <optional> | {} | Options object. If a boolean is provided, it will be treated as Properties
|
- Source
- See
Parsed object
- Type:
- object
getDownloadDir() → {string}
Get download directory. If doesn't exist, it will be created automatically.
- Source
Absolute path to the download directory
- Type:
- string
getMethod(name, thrownopt) → {function}
Get class method by name.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name | string | Name in format | ||
thrown | boolean | <optional> | true | If |
- Source
Class method.
- Type:
- function
getModuleDir(pkgName, withGlobalDiropt) → {string}
Get module directory.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pkgName | string | Package name to find. | ||
withGlobalDir | boolean | <optional> | true | Whether to include the global module directory. |
- Source
Return absolute package directory.
- Type:
- string
getUnitFormat(optionsopt) → {TBajoFormatResult}
Get unit format.
| Name | Type | Attributes | Default | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | <optional> | {} | Options. Properties
|
- Source
Returned value.
- Type:
- TBajoFormatResult
(async) importModule()
Import file/module from any loaded plugins.
Method proxy from module:Helper.importModule
- Source
(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 totruethrowNotFound: should throw if package is not found. Defaults tofalsenoCache: always use fresh import. Defaults tofalseasObject: see below. Defaults tofalse
Return value:
- if
options.asObjectistrue(defaultfalse), 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) ... `
| Name | Type | Attributes | Description |
|---|---|---|---|
pkgs | TNsPathPairs | <repeatable> | One or more packages in format |
- Source
See above.
- Type:
- Object |
Array
isLogInRange(level) → {boolean}
Check whether log level is within log's app current level.
| Name | Type | Description |
|---|---|---|
level | string | Level to check. See TLogLevels for more. |
- Source
- Type:
- boolean
isValidApp(dir, returnPkgopt) → {boolean|Object}
Check whether directory is a valid Bajo app.
| Name | Type | Attributes | Description |
|---|---|---|---|
dir | string | Directory to check. | |
returnPkg | boolean | <optional> | Set |
- Source
- Type:
- boolean |
Object
isValidPlugin(dir, returnPkgopt) → {boolean|Object}
Check whether directory is a valid Bajo plugin.
| Name | Type | Attributes | Description |
|---|---|---|---|
dir | string | Directory to check. | |
returnPkg | boolean | <optional> | Set |
- Source
- Type:
- boolean |
Object
join(input, optionsopt) → {string}
Return human friendly joined array of items.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input | Array.<*> | Array to join | |||||||||||||||||
options | string | | <optional> | {} | If provided and is a string, it will be used as separator. Properties
|
- Source
- Type:
- string
numUnit(valueopt, defUnitopt) → {string}
Get numeric portion and its unit from a string.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
value | string | <optional> | '' | Value to get its numeric portion |
defUnit | string | <optional> | '' | Default unit if value doesn't have one |
- Source
- Type:
- string
parseConfig(input, extsopt, optionsopt) → {Object|Array|null}
Parse input using all registered config handlers. The first handler that returns a valid object or array will be used.
Use this method if you want to parse a text input that can be in any format supported by the registered config handlers.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
input | string | The input string to be processed by the config handlers. | ||
exts | Array.<string> | <optional> | Optional array of extensions to filter the config handlers. If provided, only handlers with matching extensions will be used. | |
options | object | <optional> | {} | Options to be passed to the config handlers. |
- Source
The result from the first successful config handler, or null if none succeed.
- Type:
- Object |
Array | null
(async) readAllConfigs(path, optionsopt) → {Object}
Read all forms of configuration files from file path.
Internally, it will call Bajo#readConfig twice, first for the default config file and second for the environment based config file. Then it will merge both results using defaultsDeep.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
path | string | Base path to start looking config files. | ||
options | Object | <optional> | {} | Options. See Bajo#readConfig for more. |
- Source
Merged configuration object.
- Type:
- Object
(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
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
file | string | File to read and parse. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options | Object | <optional> | {} | Options Properties
|
- Source
- Type:
- Object
(async) runHook(hookName, …argsopt) → {Array}
Run named module:Hook.
If no hook found with the given name, it will return an empty array. If a hook has noWait set to true, it will not wait for the hook to finish and will not return its result.
Note: Even though this method returns a result, it is not recommended to use the result for any purpose. Use the result only for debugging or logging purposes. Hooks are designed to be fast, lightweight and mutate arguments given to them so that the next hook can benefit from the changes.
| Name | Type | Attributes | Description |
|---|---|---|---|
hookName | TNsPathPairs | Name of the hook to run. | |
args | any | <optional> <repeatable> | Argument passed to the hook function. |
- Source
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.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
file | string | File name. | ||
item | Object | Item to save. | ||
printSaved | boolean | <optional> | true | Print info on screen. |
- Source
Full file path.
- Type:
- string
(async) toJson(data, optionsopt) → {Promise.<(string|void)>}
Convert data to JSON string, optionally write to file if options.writeToFile is provided.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data | Object | Data to convert to JSON string | |||||||||||||||||
options | Object | | <optional> | {} | Options object. If a string is provided, it will be treated as Properties
|
- Source
JSON string or void if written to file
- Type:
- Promise.<(string|void)>
(async) toYaml(object, optionsopt) → {Promise.<(string|void)>}
Convert object to YAML string, optionally write to file if options.writeToFile is provided.
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
object | object | Object to be converted | ||||||||||||||||
options | object | | <optional> | Options object. If a string is provided, it will be treated as Properties
|
- Source
YAML string or void if written to file
- Type:
- Promise.<(string|void)>
(async) toYml(object, optionsopt) → {Promise.<(string|void)>}
Convert object to YML string, optionally write to file if options.writeToFile is provided. Alias for Bajo#toYaml.
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
object | object | Object to be converted | ||||||||||||||||
options | object | | <optional> | Options object. If a string is provided, it will be treated as Properties
|
- Source
- See
YAML string or void if written to file
- Type:
- Promise.<(string|void)>
Type Definitions
TConfig
- Object
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
env | string | Environment name ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
runtime | Object | <optional> | {} | Runtime configuration Properties
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
log | Object | <optional> | {} | Logging configuration Properties
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dump | Object | <optional> | {} | Dump configuration Properties
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lang | string | <optional> | Default language for internationalization. Default is the system language or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
intl | Object | <optional> | {} | Internationalization configuration Properties
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exitHandler | boolean | <optional> | true | Whether to attach exit handlers for graceful shutdown. Default is | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cache | Object | <optional> | {} | Cache configuration Properties
|
- Source
TDataType
Supported data types.
- Object
| Name | Type | Description |
|---|---|---|
0 | string | string |
1 | string | float |
2 | string | double |
3 | string | integer |
4 | string | smallint |
5 | string | date |
6 | string | time |
7 | string | datetime |
8 | string | array |
9 | string | object |
- Source
TFormatResult
Object returned by bajo:getUnitFormat.
- Object
| Name | Type | Description |
|---|---|---|
unitSys | string | Unit system. |
format | Object | Format object. |
- Source
TFormatter
- Object
| Name | Type | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
metric | Object | Metric formatter Properties
| ||||||||||||||||||||||||||||||
imperial | Object | Imperial formatter Properties
| ||||||||||||||||||||||||||||||
nautical | Object | Nautical formatter Properties
|
- Source
TFormatterFieldMap
Formatter field mapping.
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
speed | string | <optional> | 'speed' | |
distance | string | <optional> | 'distance' | |
area | string | <optional> | 'area' | |
degree | string | <optional> | 'degree' | |
heading | string | <optional> | 'degree' | |
course | string | <optional> | 'degree' |
- Source
TNsPathPairs
Name in {ns}:{path} format.
- string
- Source
TNsPathResult
Object returned by bajo:breakNsPath().
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
ns | string | <optional> | Namespace |
subNs | string | <optional> | Sub namespace |
subSubNs | string | <optional> | Sub of sub namespace |
fullNs | string | <optional> | Full namespace, including sub namespaces |
path | string | Path without query string or hash | |
fullPath | string | <optional> | Full path, including query string and hash |
realPath | string | <optional> | Path without query string or hash, but with parameters replaced with their values |
realFullPath | string | <optional> | Full path, including query string and hash, but with parameters replaced with their values |
qs | Object | <optional> | Query string object |
params | Object | <optional> | Parameters object |
- Source