0.1.0 • Published 2 years ago
ff-reddit v0.1.0
Create a CLI Command (program)
CLI frameworks
- Keep in mind that there are entire Node frameworks to help with building out a CLI (just like there are server and frontend frameworks).
- Commander - the defacto standard
- oclif
- Caporal
Interpreter
#! /usr/bin/env node
- That
hashbang
tells the machine where to find theinterpreter
, which is Node. - I believe it works b/c
PATH
is in theenv
and somewhere in the path it'll find Node (since Node's path location can/will be different on different machines depending on how it was installed).
package.json
- Register our new command (CLI) in the
bin
folder.- This instructs
npm
to do that (install our command in the user'sbin
folder) when a user usesnpm
to globally install our command.
- This instructs
- Need to tell Node.js what the
name
of our CLI is so when can actually use it in our terminal. Just have to add this section to our package.json:
{
...
"bin": {
"reddit": "./reddit.mjs"
}
}
reddit
is the name of theCLI
."./reddit.mjs"
is the path to the entry file for the command's code.- Once installed, this package will have its
bin
command installed into your machine'sbin
folder allowing us to use thereddit
command globally in the terminal.- All global commands exist in your machine's
bin
directory.
- All global commands exist in your machine's
- The
bin
property (in package.json) is used when your package is used as aCLI
. - The
main
property (in package.json) is used when someone isimporting
your package (usingimport
orrequire
) into their project's code.- It points to the entry point for your package's code.
Install package globally
- We must install our own package globally on our machine so we can test out the CLI. We could just execute the file with the node runtime, but we want to see the CLI actually work.
npm install -g .
- We can simply install with no args (or
.
) which tells npm to install the current director. The-g
flag means we want to globally install this package vs in a local project'snode_modules
folder. - Can confirm it's been installed, and its location, with:
which reddit
Run command
# Will print to the console a random post's Title and Link
reddit --print
# Will open that random post in your default browser
reddit
0.1.0
2 years ago