rolling your own Node CLI tools
Most modern libraries and frameworks have a command line interface that will make it easier to build your own applications using the particular framework. Look at Angular CLI or Polymer CLI for an idea of what I'm talking about.
The idea is that we can create a tool like a Yeoman generator without having to learn the way Yeoman works.
Getting started #
To start create a package.json
file.
We'll install the application's dependencies next. Note that as of NPM 5.0 we don't need to indicate we're saving them as dependecies (--save
) as this is assumed when you install a package.
Edit the package.json file so that it looks like the exmaple below.
Pay special attention to the bin
section of the package. we've created a scaffold script that will run index.js
.
Buildng the app #
At a minimum Caporal requires:
- a command
- one or more arguments
- an action to execute when we use the command
The example below will take a template and, optionally, a variant of the template we want to create. It will log the arguments and options to the console.
To test the program use NPM's link to add the project to our global npm space without having to publish it to the NPM registry and download it back to our development workstation.
This will put your scripts in your path and allow you to run your script without prepending the full path.
If we run the full command:
We should get this in response:
I know, we don't really need to log the data we've just entered so we can change the action to point to a separate file (discussed below)
The create.js
file host all the application logic for the create command and is isolated from other commands and the Caporal logic itself. We're using Shelljs to run Unix commands in our project and prompt to get user input.
The commands will prompt the user, replace placeholder elements, and copy content from our template to the elements we create.
Building templates #
Now that we have the logic for creating elements, let's look at creating the templates we'll use as the original for each element.
Each element has its own template and we may have more than one set of templates for our application. The structure may look like this tree.
We'll look at the default element that will act as our default template. Each element will mirror the structure of this default element. The directory has the following structure:
- LICENSE is our element's license. MIT by default
- _varaiables.js contains the items
- llib is a directory to hold any additional files we need
- package.json
- myElement.js
_variables.js
contains the variables we want to replace in our elements. The replacement and processing is handled in the create script so we'll just present the file as is.
The package.json
files is an example of what the template looks like. Variables to be replaced match the names in _variables.js
.
myElement.js
is the core of the template. We create a V1 Custom Element and take advantage of features in the specification, working with observed attributes and reflecting changes in the attributes to the code and vice versa.
Custom Elements V1 use only ES2015 exclusively.
So now that we have it all together we can publish it to NPM if we so choose because we linked the CLI tool using NPM we can run it without uploading it.