Skip to main content
Dublin Library

The Publishing Project

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. ```bash npm init ``` 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. ```bash npm install caporal colors prompt shelljs ``` 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`. ```json { "name": "scaffold", "version": "1.0.0", "main": "index.js", "bin": { "scaffold": "index.js" }, "dependencies": { "caporal": "^0.3.0", "colors": "^1.1.2", "prompt": "^1.0.0", "shelljs": "^0.7.7" } } ``` ## 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. ```javascript #!/usr/bin/env node const prog = require('caporal'); prog .version('1.0.0') .command('create', 'Create a new application') .argument('