Riker
Riker is an opinionated node.js cli library for handling the business of writing that makes it optimizes for composability, testability, and unixy best practices. It is is designed to allow authors to create fluent interfaces with subcommands that are all classes where the commands themselves are unit testable.
Beliefs
- All code should be easily testable and tested, even command line scripts.
- Dependency injection is a Good Idea™.
- Configuration should be injecteable inheritable.
- Configuration should be specifiable in configuration files, environment variables, and command like arguments. In that order.
- Streams are the one true Node.js abstraction
- Composable
But why is it called "Riker"?
Because the only thing that should ever come after "commander" is "Riker".
Architecture
- Riker
- Main entry point
- Provides helpers for things like loading a directory of commands
- CommandCollection
- Is a collection of subcommands
- Resolves command aliases and loads child commands
- Child commands can, themselves, be command collections
- Command
- Is an individual command
Usage
Creating a custom command.
const Riker = ;const Command = RikerCommand;{super;thisshortDescription = 'Sends any input to standard out';thishelp = 'This command streams input coming to standard in and pipes it to standard out.';}{thissomeOption = optionssomeOption;}{thisinput;if donethisinput;}
Creating a custom command with config parameters.
const Riker = ;const Command = RikerCommand;{super;thisshortDescription = '';thishelp = 'Help placeholder';const parameter = this;parametertypeNumber // Defines the type - this will cast the type if necessary to ensure numbers, strings, etc are handled appropriately.// Mappings// Provides a default value for this parameter.// Marks this parameter required, incompatible with `default`.// Provide help text related to this option, wrapped behavior from yargs.// An alias for this command, wrapped behavior from yargs.this// Provide a set of allowable options.}{thissomeOption = optionssomeOption;}{thisinput;if donethisinput;}
Writing a unit test
Unit testing our class by mocking input using through2 and should.
Creating a set of subcommands in a command collection
var Riker = ;var Cat = ;var Command = RikerCommand;var command = ;var riker = ;riker;var baz = ;bazshortDescription = 'This is baz.';bazhelp = 'Baz knows about stuff…';baz;riker;riker;riker;