LovaCLI
node.js CLI boilerplate:
- On top of Caporal.js
- ES 2017 async/await ready
- Optional Mongoose or Sequelize as MongoDB, MySQL, PostgreSQL ORM/ODM
- Winston as logger
- OOP for commands and modules
- ❤️
Installation
npm install lovacli --save
Usage
Sample application
#!/usr/bin/env node const ProgramCommandLovaClass = ;const path = ; let program = "name": "Sample CLI Application" "debug": true "version": "1.0.0" "paths": "commands": path ; program;
Sample command class
Save it as test.js in commands/
path. It will be ready to be executed as node index.js test
const Program Command LovaClass = ; /** * Set up command, description, options, arguments * Optional. You can ommit it if you don't need any specific settings * Can be async or sync. */ async { progCommand; progCommand; progCommanddescription'Command description'; /// read more about Caporal.js's prog methods /// (options, args, validation) /// here: https://github.com/mattallty/Caporal.js#examples } /** * Handle command * Can be async or sync. */ async { // handle() runs on command execution // as we probably don't need db for every run, lets initialize it on demand here let db = await thisdb; // both logger and this.logger works for accessing Winston's methods // logger.info('info'); // this.logger.info(eventTypes); // mongoose models // let eventTypes = await db.EventType.find().exec(); // this.logger.info(eventTypes); // ... // sequelize models // let rigs = await db.Rig.findAll(); // this.logger.info(rigs); // ... logger; thisprogram; }; moduleexports = Handler;
API
Program
constructor(options)
: Program
Default options are:
"name": "LovaCLISampleApp" "debug": true "version": "1.0.0" "paths": "models": path "commands": path "tests": path
You can extend them, passing options object, const program = new Program({name: "Better name", debug: false});
async loadCommand(filename)
: Command
Loads command class from filename
, create entity of it and initialize it. Assigns name(extract it from filename, so for testfile.js it will be 'testfile'), assigns description(optional help message, you can specify it with description
method, getter or property of command class), and calls setup methods.
async init(handleImmediate = true)
- Loads all commands from options.paths.commands paths and initializes them.
- Executes application, if handleImmediate = true, defaults = true
async handle()
Executes application. Use it, after you initialize application with init(false)
program;### orawait program;await program;
async execute(name, args = [], options = {})
Executes command by name. Use it if you need to call some command from another command's handle.
exit(e = null)
Exits application. Logs error if it's passed
Command
Need to be extended in order to use. Load and initialize extended classes using program.init()
or program.loadCommand()
.
Command is also EventEmitter, so feel free to use native EventEmmiter methods: https://nodejs.org/api/events.html#events_class_eventemitter
const Program Command LovaClass = ; /** * Optional */ async { } /** * Required */ async { }; moduleexports = Handler;
async setup(progCommand)
setup(progCommand)
Optional to extend. Can be async or sync. Set up command, description, options, arguments.
async { progCommand; progCommand; progCommanddescription'Command description'; /// read more about Caporal.js's prog methods /// (options, args, validation) /// here: https://github.com/mattallty/Caporal.js#examples}
async handle(args, options, logger)
handle(args, options, logger)
Required. Can be async or sync. Run code executed for this command.
async name()
: String
Returns command name.
.logger
: Winston object
async { thislogger; thislogger; thislogger; }
.program
: Parent Program instance
async { thisprogram; thisprogram;}
How to?
Take application name, version and description from package.json?
It may be a good idea to store program's description and version name in one place. It's already in package.json, so why don't to use that values?
#!/usr/bin/env node const ProgramCommandLovaClass = ;const path = ; const pjson = ; let program = "name": pjsondescription || pjsonname "version": pjsonversion "debug": true "paths": "commands": path ; program;
Execute another command from command's handle
There's program.execute
method:
### somecommandjsconst Program Command LovaClass = ; async { }; moduleexports = Handler;
### testjsconst Program Command LovaClass = ; async { await thisprogram; }; moduleexports = Handler;