Cli-Js App Example
The app below resembles a console line application and has a few example commands in a cli-like interface:
Enter the next command to execute
- Scripts which are too complex to write with lower level scripting languages.
- Quick, one-time scripts for taking care of certain one-time actions.
- Reusable scripts to facilitate repetitive tasks and automation.
- Command line applications: CLIs, which provide an interactive interface to help the user complete some actions.
The last option: Cli, can also be leveraged to accomplish the other tasks listed above. We have created an API which takes of the lower level tasks of creating and running a Cli (which we use ourselves to help automate or facilitate certain DevOps processes).
The project is available as a consumable package and the code is available on Git:
To get started now: you can directly install via npm:
- Collection of Commands: the cli app will run based on a list of commands which includes its name, description, required params, and code to execute.
- Input and Output: the app will get user input and output contents based on the services provided. The library comes with default console line input and output services.
- Program Configurations: the program will behave differently based on input configurations.
The sections below will outline each requirement.
One of the central concepts of the app is a command, which is an encapsulation of some action to execute and its required parameters. The following are type definitions for a commmand, and a command input parameter, which is necessary for commands which require user input.
A few example commands from the example application are shown below. Each exemplifies a different type of command with different user param requirements.
Cli Command Collection
The cli command collection is an encapsulation of one or more collections which will be used by the cli app driver to run the app. In most use cases, such as the example above, a collection based on an array is suitable, as shown in the code box below. In more advanced use cases, a custom collection can be used which contains some logic to alter the commands after each execution based on some logic. If this approach is taken, the list of commands may change after each command execution.
Outputter and Input Requestor
- ConsoleOutputter: outputs data to the console window and provides separate colors for debug, message, warning, and error.
- ConsoleUserInputRequestor: prompt the user to enter or make a selection in the console window.
If desired, you can write your own outputter and/or user input requestor to encapsulate different functionality for input and output. Note: when we created our cli-app example for this article, the only thing we needed to do was provide different output and input services; we were able to directly re-use everything else.
The app runner will run with some default options if none are provided. In most cases, the defaults will be fine, but if you'd like to change some default behavior, you can do so by injecting different options. The interface below specifices all options; you may create an object which contains only the options you need to set, and the app will fill in the rest.
Putting it All Together
Once we have everything above defined, we can inject it all into the CliApplication object:
A Note on Alternative Strategies
- Inject the Alternate Services into the App: since the app was built based on dependency inversion, we can inject our alternate services into the app without any changes to the app itself.
- Console app
- Website app
- Desktop app, using something like Electron
- Mobile app
The UI itself can look like a command line interface, such as the example app, or the outputter can implement the 'output commands' method differently to show the commands as buttons or some other interface.