Constructor
new Bajo(app)
| Name | Type | Description |
|---|---|---|
app | App | App instance. Usefull to call app method inside a plugin. |
- Source
Members
config :Object
Config object.
- Object
- Source
- See
- config
Methods
breakNsPath(name, checkNsopt) → {TNsPathResult}
Break name to its namespace & path infos.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
name | TNsPathPairs | | Name to break | ||
checkNs | boolean | <optional> | true | If |
- Source
- Type:
- TNsPathResult
(async) buildCollections(options) → {Array.<Object>}
Method to transform config's array or object into an array of collection.
| Name | Type | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | Options. Properties
|
- Source
The collection
- 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) → {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
handlerkey in it, this function handler will be instead
| Name | Type | Attributes | Description |
|---|---|---|---|
name | TNsPathPairs | | Method's name, function handler, plain object or plugin instance. | |
args | any | <optional> <repeatable> | One or more arguments passed as parameter to the handler. |
- Source
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 patterndir: plugin's base directory
| 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, otherwise: Properties
|
- Source
- Type:
- any
format(value, typeopt, optionsopt) → {string}
Format value.
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | any | 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
formatByType(type, value, dataTypeopt, optionsopt) → {Array|string}
Format value by type.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
type | string | Format type. See TBajoFormatType for acceptable values. | |||||||||||||||||
value | any | 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, optionsopt) → {Object}
Read and parse JavaScript file.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
file | string | File to read and parse. | ||
options | Object | <optional> | {} | Options. |
- Source
Parsed JavaScript object.
- Type:
- Object
fromJson(data, optionsopt) → {Object}
Read and parse JSON string or object.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
data | string | Filename to load from or JSON string to parse. | ||
options | Object | <optional> | {} | Options. |
- Source
Parsed JSON object.
- Type:
- Object
getGlobalModuleDir(pkgNameopt, silentopt) → {string}
Get NPM global module directory.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pkgName | string | <optional> | If provided, return this package global directory. Otherwise the npm global module directory. | |
silent | boolean | <optional> | true | Set to |
- Source
- 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, base) → {string}
Get module directory, locally and globally.
| Name | Type | Description |
|---|---|---|
pkgName | string | Package name to find. |
base | string | Provide base name if |
- 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:Lib.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
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
(async) init()
Initialization:
- Building base config
- Building plugins
- Collect all config handler
- Building extra config
- Setup boot order
- Boot loaded plugins
- Attach exit handlers
- Source
(async) isEmptyDir(dir, filterFn) → {boolean}
Check whether a directory is empty or not. More info please check here.
| Name | Type | Description |
|---|---|---|
dir | string | | Directory to check. |
filterFn | function | Filter function to filter out files that cause false positives. |
- Source
- Type:
- boolean
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(array, options) → {string}
Human friendly join array of items.
| Name | Type | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
array | Array.<any> | Array to join | |||||||||||||||
options | string | | If provided and is a string, it will be used as separator. Properties
|
- Source
- Type:
- string
numUnit(valueopt, defUnitopt) → {string}
Return its numeric portion of a value.
| 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
(async) readAllConfigs(path, optionsopt) → {Object}
Read all config files from path.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
path | string | Base path to start looking config files. | ||
options | Object | <optional> | {} | Options. |
- Source
Merged configuration object.
- Type:
- Object
(async) readConfig(file, optionsopt) → {Object}
Read and parse file as config object. Supported types: .js and .json. 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
readJson(file, thrownNotFoundopt) → {Object}
Read and parse json file.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
file | string | File to read. | ||
thrownNotFound | boolean | <optional> | false | If |
- Source
- Type:
- Object
(async) runHook(hookName, …argsopt) → {Array}
Run named hook/event.
| 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
toJson(data, optionsopt) → {string}
Convert data to JSON string.
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data | Object | Data to convert to JSON string. | |||||||||||||||||
options | Object | <optional> | {} | Options. Properties
|
- Source
JSON string
- Type:
- string