Package Exports
- @maartennnn/cli-builder
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@maartennnn/cli-builder) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
CLI-Builder
This builds cli's very quickly.
This is a WIP. As are the docs.
Todo
- Document
optionsinside a command - Document
this.argv - Document
inputinside a command - Add a way to give params to the function
- Document the
paginationfunction - Add
promptConfirmto docs - Write tests for some functions within the REPL Client
- document
noExitforsuccesLoganderrorLog - add
warningLog - FIXED
0.0.11: Interactivaly using help works only once - When command is incomplete it should throw an error
- Document usage of
--commands
Installing
npm install cli-builderTake a look at bin/example-cli.js
Instantiate CLI with new REPLClient({ ...options })
options.command- Type:stringCommand to put in logsoptions.enableInteractive- Type:booleanAllow interactive modeoptions.helpHeader- Type:stringHeader to show in helpoptions.helpFooter- Type:stringFooter to show in helpoptions.actions- typeobjectObject of functions that you can mount which will bindcliandbindActionArgson it (more onbindActionArgslater).options.bindActionArgs- typearrayArray of any you can pass to the action functions.
and
and running with
cli.run(commands);an example of commands is shown below
commands example
// DO NOT ADD HELP TO THE ROOT OBJECT.
const commands = {
test: {
execute: () => console.log('this is the test run'),
help: 'help of test',
testing: {
execute: () => console.log('executing testing'),
help: 'testing help',
},
testing2: {
execute: () => console.log('executing testing2'),
help: 'testing2 help'),
},
},
test2: {
execute: () => console.log('this is the test2 run'),
help: 'help of test2',
},
deep: {
nesting: {
works: {
as: {
// Without help
command: () => console.log('to run this type `deep nesting works as command`')
// Or with help
command: {
// this get executed as `deep nesting works as command`
execute: () => console.log('to run this type `deep nesting works as command`'),
// this get executed as `deep nesting works as command help`
help: 'help of command'
}
}
}
}
},
runSomeFunction: async () => {
// DO SOME INSANE LOGIC HERE
}
}Help is executed on the object. Eg. in this case executing test help will reveal help of test and it's help children (in this case testing.help and testing2.help)
Running a command is in a nesting way: deep nesting works as command for the example above (executes execute function case it's an object).
deep nesting works as command help (executes help function in case it's an object)
functions are added as camelCase but inside the command line you'll need to use kebab-case:
run-some-function will call function runSomeFunction.
actions can be used to integrate imported files. You can check out the example of a full-fledged cli implementation in ldpos-commander. It's basically passing the cli as this, therefore you can reference cli as this in your action and bindActionArgs will be all of the arguments you pass within that action function eg.:
const actions = {
getUserData = async (id, someFunction, aString, aNumber) => {
try {
const data = await axios.get(`user/${id}`)
// this references to the cli object as it's bound
this.successLog(data)
someFunction()
console.log(aString, aNumber)
} catch (e) {
throw new Error(e)
}
}
}
// Binding below array to `options.bindActionArgs` in `new CmdInterface({ ...options })`
const options = {
bindActionArgs = [123, () => console.log('function executed'), 'a string', 23123]
}
const cli = new CmdInterface(options)
const commands = {
// No arguments are passed here, they are mounted dynamically
anActionTest: async () => await cli.actions.getUserData()
}
cli.run(commands)using cli-builder an-action-test will execute the getUserData function with the bindActionArgs parameters bound to it.
CmdInterface API
CmdInterface.run(commands)type:function- Initiates the interface interactivaly or non-interactivaly depending on the arguments given.CmdInterface.exit(code, override)type:function- Exits the process- Param:
codetypenumber- exit code same asprocess.exit - Param:
overridetypeboolean- this is to override exit if in interactive mode
- Param:
CmdInterface.invalidCommand()type:function- This is to run and pre-definederrorLogwith aconsole.logto write help to the command lineCmdInterface.errorLog(errorMsg)type:function- Param:
errorMsgtypestring- String to log as an error (colored in red)
- Param:
CmdInterface.successLog(successMsg, prefix = '')type:function- Param:
successMsgtypestring- String to log as an success (colored in green) - Param:
prefixtypestringdefault''- String to show before the message followed by a break-line
- Param:
CmdInterface.execCmd(cmd)type:function- Executes anarrayorstringof commands- Param:
cmdtypestring|Array<string>- Executes commands (mostly used internally)
- Param:
CmdInterface.commandCmd()type:function- Executes a command non-interactivaly (mostly used internally).CmdInterface.interactiveCmd()type:function- Executes a command interactivaly (mostly used internally).
Helper functions
CmdInterface.promptInput(message, secret)type:function- Prompts an input and returns the given value- Param:
messagetypestring- Message to be prompted - Param:
secrettypeboolean- Hide input provided by user (useful for passwords) - Returns: type
Promise<string>- Returns input written by the user
- Param:
CmdInterface.kebabCaseToCamel(str)type:function- Converts string- Param:
strtypestring- String to be converted to camelCase - Returns: type
string- Returns converted string
- Param:
CmdInterface.camelCaseToKebab(str)type:function- Converts string- Param:
strtypestring- String to be converted to kebab-case - Returns: type
string- Returns converted string
- Param: