CLI Script
nodejs-script
is a NodeJS template for writing single-file command-line interface (CLI)
scripts. It's designed to be a single-file script without relying on any
external packages, making it a self-contained program.
Below are some of its main features:
-
Environment-agnostic: It starts with
#!/usr/bin/env node, which helps the script find the Node.js executable irrespective of its location. -
Usage Information: It includes a
printHelp()function that prints out how to use the script. Users can trigger this function by providinghelp,--help,-h, or-?as arguments. -
Error Handling: The template is equipped to handle errors gracefully. It throws errors that are caught at the entry point and reported with a non-zero exit code, making debugging easier.
-
Argument Checking: The
main()function looks at the number of arguments passed and throws an error if it doesn't meet the expected count. -
Verbose Output: The
verbose()function takes care of outputting messages to the console. An environment variableSCRIPT_QUIETcan suppress these messages if set to '1'. -
Environment Variables for Control:
SCRIPT_QUIETfor silencing verbose output, andSCRIPT_RUN_SKIPfor skipping the actual running of the script. These can be useful for testing or other conditional executions. -
Testability: The template is designed to be test-friendly. It allows you to skip the script's run for unit testing by setting an environment variable and requiring the file. Example tests are provided in the
tests/nodejsdirectory. -
Zero dependencies: The script relies only on the Node.js standard library, so it can be distributed and run as a single file.
Overall, this template offers a robust starting point for developing your own NodeJS CLI scripts.
Getting Started
Begin by updating the script name and file name in the script comments. This will clarify the script's functionality for anyone who reads the code.
Next, adjust the main function to validate and handle the CLI arguments
specific to your script. You should also revise the printHelp function to
include relevant usage instructions.
Your core logic will be housed in the main function. Simply replace the
placeholder comment "Add your logic here" with the code that accomplishes your
task. If you're interested in testing the script before running it, you can use
environment variables like SCRIPT_QUIET and SCRIPT_RUN_SKIP to modify its
behavior.
You can then run your script from the command line using ./nodejs-script.
Authoring tests
This template includes a tests/nodejs
directory with sample Unit and Functional tests built on the
Node.js built-in test runner.
_helper.js
provides shared helpers: runMain() requires the script and calls main()
in-process so its buffered output can be asserted without spawning a process,
while runScript() runs the script as a separate process to assert its exit
code and output.
script.test.js
is an example of a Unit test for running the script with different arguments and
options.
script.functional.test.js
is an example of a Functional test for running the script as your script users
would do.