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
handler
key 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
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 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
freeze(obj, shallowopt)
Freeze object
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj | Object | Object to freeze | ||
shallow | boolean | <optional> | false | If |
- 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 | string | 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
- TNsPath (
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) → {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 totrue
throwNotFound
: should throw if package is not found. Defaults tofalse
noCache
: always use fresh import. Defaults tofalse
asObject
: see below. Defaults tofalse
Return value:
- if
options.asObject
istrue
(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) 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
parseDt(dt) → {Object}
Parse datetime string as Javascript date object. Please visit dayjs for valid formats and more infos
Name | Type | Description |
---|---|---|
dt | string | Datetime string |
- Source
- See
Javascript date 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 returns as is |
- Source
- See
- 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 date time 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) readAllConfigs(path) → {Object}
Read all config files by path
Name | Type | Description |
---|---|---|
path | string | Base path to start looking config files |
- 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/event
Name | Type | Attributes | Description |
---|---|---|---|
hookName | TNsPathPairs | ||
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