You can have your plugin authors configure the behavior of your CLI by providing a configuration file in the root of any plugin.
This is an object. Each plugin will have its own root level key.
For example, in weex-cli-plugin-logger
project, you can put an weex-cli-plugin-logger.config.js
in the root.
In weex-cli-plugin-logger.config.js
:
module.exports = { name: 'log', defaults: { data: 'Test Data' }, }
In the commands, you can use this data by:
module.exports = { name: 'log', description: 'My first Weex Toolkit Plugin', alias: 'l', run: (context) => { console.log(context.config.log) } }
The output should be an object:
{ data: 'Test Data' }
If you'd like to load your own config files, use the loadConfig
function included in the config object which is powered by cosmiconfig:
module.exports = { run: async context => { const { config: { loadConfig }, print: { info }, runtime: { brand }, } = context const myConfig = loadConfig('~/.myconfig/', 'filename') // if you want to load multiple configs and have them override: const currentFolder = process.cwd() const myConfig = { ...loadConfig('~/.myconfig/', 'filename'), ...loadConfig('~/configurations/myconfig/', 'filename'), ...loadConfig(currentFolder, 'filename'), } } }
By default, Cosmiconfig will start where you tell it to start and search up the directory tree for the following:
For example, if your module's name is “soursocks”, cosmiconfig will search up the directory tree for configuration in the following places:
Cosmiconfig continues to search up the directory tree, checking each of these places in each directory, until it finds some acceptable configuration (or hits the home directory).