Skip to main content

Reference

This is the reference document for Lithium. It outlines everything that Lithium has to offer.

While Lithium provides auto-complete in VS-Code through JS-Doc, this is just easier to find sections and functions.

Importing Lithium#

const lithium = require("lithium-cli")const app = lithium()
ParameterDescription
setConfigA function that allows you to set the configuration object.
commandA function that allows you to add a command to your CLI
exportA function that exports your commands. Use with import
importA function that can import commands. Use with export
askA function that exposes an API to ask the user a question
startA function that allows you to start your CLI
executeA function that allows you to run a bash command
spinnerA function that expores ora
colourA function that exposes chalk
boxA function that exposes boxen
logWithColourA function that allows you to log a value with colour. Use with execute
errorWithColourA function that allows you to log a value with console.error and colour. Use with execute

setConfig#

Allows you to modify the configuration object. Here's how the configuration object is shaped:

ParameterDescription
headerA synchronous function called before every command is executed
footerA synchronous function called after every command is executed
onHelpA function called when your_cli help is run
onCommandNotFoundA function called when a command that is not found is run

Please note that passing in any of these parameters only overrides the parameter that you have passed.

You can also use this to store some metadata about your application.

command#

The command function allows you to enter a command. It takes these parameters: (note that these parameters are positional)

ParameterDescription
nameThe name of the command, for example, npm i would be i
actionThe function to be run when the name is matched. Refer to the below table
inputsA list of inputs (see LithiumInput) that you can use to get user parameters
descriptionA simple string describing your command. This is used in the help function

The action that you have to provide will have to take these parameters: (the callback you provide can be asynchronous)

ParameterDescription
argsContains the values that the user has provided your command. Refer the below note
osThe operating system. Possible values: macos, windows, linux, unknown
currentWorkingDirectoryCurrent directory right from the command line

Note about args This object gets you an object. This depends heavily on the input that the user has provided. There are a couple shapes it could be in: boolean, string, or root. Boolean is quite simple. If the user says something like cli deploy --cloud, the args would have a property called cloud which is true. If the user says cli deploy --cloud aws, you'd have a property cloud saying aws. If the user says cli deploy ., you'd get root as .

export and import#

These are quite simple. They are designed to be used with each other.

// In subfolder filemodule.exports = app.export()
// In parent fileapp.import(require("your_subfolder_file.js"))

ask, LithiumInput#

ParameterDescription
questionA string, this has to be asked to the user
typestring, boolean, number, password, choices
optionsSpecify this in the format of LithiumInputChoice (see below) only if you chose choices in type

LithiumInputChoice

ParameterDescription
nameThe display name of this option
valueThe program value for this option

start#

There's no parameters or return value. Just make sure that this is run only once and in the entry point file.

execute#

This is an asynchronous function to execute shell command. Note that parameters are positional

ParameterDescription
commandThe command that needs to be run
directoryThe directory in which the command has to be executed
showOutputIf the output for that command needs to be displayed
logFunctionA function that takes a value and prints it if the command is successful.
errorFunctionA function that takes a value and prints it if the command is unsuccessful

Returns a String or throws an Error.

For logFunction and errorFunction, please refer to logWithColour and errorWithColour.

colour, box, spinner#

Please refer to the respective packages for more information. This just directly exports all their functionality without any modifications, so their documentation and community resources will be applicable.

FunctionPackage Link
colourchalk
boxboxen
spinnerora

logWithColour, errorWithColour#

This is designed to be used with execute, specifically for arguments logFunction and errorFunction. It takes an argument of a function to run which adds colour, usually app.colour.red or app.colour.blue or something to that extent. Here's a snippet displaying how you can use this.

app.execute(    "ls .",    ".",    true,    app.logWithColour(app.colour.blue),    app.errorWithColour(app.colour.red))