cli-dispatch v1.3.0
Dispatches or looks up actions in a direactory from the given parameters.
Install
npm install cli-dispatch
Usage
First you need to set up the main script and actions/
directoty in the following structure:
bin/
├── main.js
└── actions
├── version.js
├── foo.js
└── bar.js
In main.js:
const dispatch = const minimisted =
actions/version.js
module console
actions/foo.js
module console
actions/bar.js
module console
Then, main.js works as the following:
$ node main.js -v
my-command v1.0.0
$ node main.js foo
foo!
$ node main.js bar --name John
Hello, John!
$ node main.js baz
No such action: baz
API
const dispatch =
dispatch(action, argv, options)
- @param {string} action The name of the action
- @param {object} argv The arguments which is passed to
- @param {object} options The options
- @param {string} [options.actions] The directory where this function look for the actions. Default is
[the caller's directory]/actions
. For example, if your main.js is in/foo/bar
and you call dispatch in main.js, then it look for the actions under/foo/bar/actions
- @return {CliDispatch} custom object
This dispatches the action by the given action name and returns the custom CliDispatch class instance. If the action file is not found, then the instance emits the no-action
event.
argv
is passed to the action function.
lookup(action, options)
- @param {string} action The name of the action
- @param {object} options The options
- @param {string} [options.actions] The directory where this function look for the actions. Default is
[the caller's directory]/actions
. For example, if your main.js is in/foo/bar
and you call dispatch in main.js, then it look for the actions under/foo/bar/actions
This looks up the action function by the given action name and returns that function.
License
MIT