Start a project with your custom Node.js command-line tool.

Antoine Boulanger
May 25, 2020 · 4 min read
Image for post
Image for post

Build a simple Node.js CLI tool without any dependencies. A single command to clone and install a repository.

Don’t make me install your CLI

Npm has a great command called init. Running the command npm init <something> temporarily installs a package you named create-<something> to run its main entry. It uses npx under the hood, but with a more familiar command.

One significant advantage of init is that it always uses the latest version of your package; you don’t have to update it manually as you would have to globally. But it still works, you can install the package globally if you prefer.

The package

To get started, you need a package.json file. By default, it uses the name field to run the main file in the terminal. To use a different name than the package name, set it in the bin field.

Try it

You’re already close to being able to try it. Create an index.js file with the first line below at the top and a simple console.log.

To run your CLI locally, without having to publish it to npm yet, use npm link.

That’s it. Now you can run whatever you chose as the name of your package or bin name to see your message.

Image for post
Image for post

Congrats, you made a CLI. 👏

Questions

That’s cool, but what if you want to ask a question, like for the name of the project? Node has a module called readline for that exact reason. Add the code below to your index.js file. It’s straight out of Node’s documentation.

Image for post
Image for post

Arguments

Instead of answering the question every time, you can also accept a name as an argument. The arguments are in the process.argv array. The first two items are URLs that you don’t need, the third one (at position 2) is the one you want.

Just move the previous question in an else clause.

Starter

Before starting the cloning process, you should create another repository for your starter boilerplate. It’s a better approach to separate your CLI and your starter in two different repositories. It makes it much easier for people to see what the starter looks like and what files they’ll end up with, in contrast to hiding them in some folder somewhere inside the CLI repo.

The standard git clone still works for people who prefer that and even the “Use this template” button on GitHub if you enable template repository.

Clone

Let’s get back to coding and get that clone going. Node has a module called child_process in which you can run any terminal commands with the spawnSync method. The { stdio: ‘inherit’ } option logs the standard output of the command. Perfect for running git clone.

Since you’re starting a new project, you don’t want to download the entire Git history of the boilerplate. Add the --depth=1 option to only copy the latest revision. Same with the .git folder, you don’t need it. Run a rm -rf in the same way or use Node’s fs.rmdirSync method with the recursive option.

Git won’t clone if the directory already exists, so don’t forget to check its status before deleting or installing anything.

The url is your GitHub repo URL, and dest is the name of the project, which is the name of the directory.

Install

Another thing you can do is to install npm dependencies automatically. Same as above, inside the status == 0 condition, run the npm install command. Since the package.json file is in the project’s folder, you have to use the --prefix option to run the install command in the dest folder.

Colour

Here’s a tip you might like, you can log your messages and questions in the colour of your choice. Just add one of the colour codes below at the beginning of your console.log string.

Image for post
Image for post

Fork it

Find the final result in the GitHub repository below. You can change the URL and content in the src/data.js file. Feel free to fork it. ✌️

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store