Extends the CLI
As the rest of our ecosystem, the CLI is modular and you can extends it as you want.
The CLI features are declared inside a sugar.json
file at the root of your package. When I say package it can be a private project as a published one on NPM for example.
The idea behind that is to let you extends the CLI with features you need or features you think can be useful for others.
How it works?
When you launch the sugar
CLI, it will search for all the sugar.json
files at all the packages root, then by reading each of them, build his list of available commands.
Your project structure can be something like:
| src
| package.json
| sugar.json
Simple example
Consider implementing a wonderful command that will simply display hello world
on the screen.
First
Create the sugar.jcon
file at your project root and fill it like so:
Note that you can make uses of the
%moduleSystem
token in yourprocess
property that will be replaced by eitheresm
, orcjs
depending on the environment you're in.
This will tell the CLI that a mySoCool
stack exists with a command(s) prettyCommand
.
Second
Then you need to create the src/node/prettyCommand.cli.js
file with this content:
This example will work fine and display hello world
on the screen.
Using your command
To make use of your command, make sure to launch it either through your project where the sugar.json
that declare it stands, or in a project where your package has been installed.
Then, simply launch:
As your prettyCommand
has been declared as the "defaultAction", you can also launch it like so:
Going further
Some additional features can be applied like a documentation when the users enters sugar mySoCool.prettyCommand -h
.
For that, simply add the interface
property inside your sugar.json
file like so:
Then create the src/node/prettyCommandInterface.ts
with this as content:
This make use of the @coffeekraken/s-interface package that let you define interfaces to describe things like a parameter object, etc...
Note that this SInterface
class can be used as well for other things and works very well with our SProcess
class from the @coffeekraken/s-process
package.
I let you check the @coffeekraken/s-interface documentation inside his own package.
Now when you launch your command with sugar mySoCool.prettyCommand --help
you will have a nicely displayed documentation.