Constructor
new Bajo(app, app)
Name | Type | Description |
---|---|---|
app | Object | |
app | Object | App instance reference. Usefull to call app method inside a plugin |
- Source
Members
applets :Array
Storage for applets
- Array
- Source
config :Object
Config object. See Config Object for details
- Object
- Source
configHandlers :Array
Storage for config handlers. By default there are only two handlers available: .js
and .json
.
Use plugin to add more type - e.g: bajo-config lets you to use .yaml
and .toml
- Array
- Source
pluginPkgs :Array
List of all loaded plugin's package names
- Array
- Source
runAt :Date
Date/time when your app start
- Date
- Source
(static, constant) mainNs :string
Your main namespace. And yes, you suppose to NOT CHANGE this
- string
- Default Value
- 'main'
- Source
Methods
breakNsPath(name, checkNsopt) → {TNsPath}
Break name to its namespace & path infos
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name | string | Name to break | ||
checkNs | boolean | <optional> | true | If true (default), namespace will be checked for its validity |
- Source
- Type:
- TNsPath
(async) buildCollections(options) → {Array}
Method to transform an array or object from config into an array of collection safely.
Emitted hooks:
{ns}:beforeBuildCollection (container)
- called before collection is built{ns}:afterBuildCollection (container, items)
- called after collection is built
Name | Type | Description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | Options Properties
|
- Source
The collection
- Type:
- Array
(async) callHandler(name, …argsopt) → {any}
Calling any plugin's method by its name. Name format: ns:methodName
.
- 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
Name | Type | Attributes | Description |
---|---|---|---|
name | string | | 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
findDeep(item, paths) → {string}
Find item deep in paths
Name | Type | Description |
---|---|---|
item | string | Item to find |
paths | Array | Array of path to look for |
- Source
- Type:
- string
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 TData 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 TFormat for acceptable values | |||||||||||||||||
value | any | Value to format | |||||||||||||||||
dataType | string | <optional> | Value's data type. See TData 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
freeze(obj, shallowopt)
Freeze object
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj | Object | Object | ||
shallow | boolean | <optional> | false | If true (default), deep freeze object |
- Source
generateId(optionsopt) → {string|Object}
Generate unique random characters that can be used as ID. Use nanoid under the hood
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | boolean | | <optional> | {} | Options. If set to Properties
|
- Source
Return string or instance of nanoid
- Type:
- string |
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 | * | Provide base name if |
- Source
Return absolute package directory
- Type:
- string
getPlugin(name, silentopt) → {Object}
Get plugin by name
Name | Type | Attributes | Description |
---|---|---|---|
name | string | Plugin name/namespace or alias | |
silent | boolean | <optional> | If |
- Source
Plugin object
- Type:
- Object
getPluginDataDir(name, ensureDiropt) → {string}
Get plugin data directory
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name | string | Plugin name (namespace) or alias | ||
ensureDir | boolean | <optional> | true | Set |
- Source
- Type:
- string
getPluginFile(file) → {string}
Resolve file path from:
- local/absolute file
- ns based path (
myPlugin:/path/to/file.txt
)
Name | Type | Description |
---|---|---|
file | string | File path, see above for supported types |
- Source
Resolved file path
- Type:
- string
getUnitFormat(optionsopt) → {TObjectFormat}
Get unit format
Name | Type | Attributes | Default | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | <optional> | {} | Options Properties
|
- Source
- Returned value
- Type:
- TObjectFormat
(async) importModule(file, optionsopt) → {function|Object}
Import file/module from any loaded plugins
Your plugin structure:
|- src
| |- lib
| | |- my-module.js
|- index.js
|- package.json
Inside your app/plugin:
const { importModule } = this.app.bajo
const myModule = await importModule('myPlugin:/src/lib/my-module.js')
Name | Type | Attributes | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
file | string | File in format | ||||||||||||||||||||||
options | Object | <optional> | {} | Options Properties
|
- Source
- Type:
- function |
Object
(async) importPkg(…pkgs) → {Object|Array}
Import one or more package belongs to a plugin
Example: you want to import packages delay
and chalk
from bajo
namespace and use it inside your app/plugin
const { importPkg } from this.app.bajo
const [delay, chalk] = await importPkg('bajo:delay', 'bajo:chalk')
await delay(1000)
...
Name | Type | Attributes | Description |
---|---|---|---|
pkgs | any | <repeatable> | One or more packages in format |
- Source
Depends on how many parameters are provided, it should return the named package or an array of packages
- Type:
- Object |
Array
(async) isEmptyDir(dir, filterFn) → {boolean}
Check whether directory is empty or not. More info please check here.
Name | Type | Description |
---|---|---|
dir | string | Directory to check. Can be a ns based directory too! |
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
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
parseDt(dt) → {Object}
Parse datetime string as Javascript object. Please visit dayjs for valid formats and more infos
Name | Type | Description |
---|---|---|
dt | string | Datetime string |
- Source
Javascript object
- Type:
- Object
parseDur(dur) → {number}
Parse duration to its millisecond value. Use ms under the hood
Name | Type | Description |
---|---|---|
dur | number | | If string is given, parse this to its millisecond value. Otherwise return as is |
- Source
- Type:
- number
parseObject(input, optionsopt) → {Object}
Parse an object and optionally normalize its values recursively. Use https://github.com/ladjs/dotenv-parse-variables to parse values, so please have a visit to know how it works
If options.parseValue
is true
, any key ends with Dur
and Dt
will also be parsed as millisecond and Javascript datetime accordingly
Name | Type | Attributes | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input | Object | | If string is given, parse it first using JSON.parse | ||||||||||||||||||||||
options | Object | <optional> | {} | Options Properties
|
- Source
- 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
resolvePath(file, asFileUrlopt) → {string}
Resolve file name to filesystem's path. Windows path separator \
is normalized to Unix's /
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
file | string | File to resolve | ||
asFileUrl | boolean | <optional> | false | Return as file URL format |
- Source
- Type:
- string
(async) runHook(hookName, …argsopt) → {Array}
Run named hook
Name | Type | Attributes | Description |
---|---|---|---|
hookName | string | ns based hook name | |
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