1. Installed in minutes using default optionsnodejs
2. Use sudo npm install -g yo to install in minutes yeoman
3. Install the cli development scaffolding through sudo npm install -g generator-cli-starter
OK, now we can use the yo cli-starter command to start our cli development journey
Create cli project
Let's try first
Now enter the following command in the command line (if you are not using hi, please replace it)
Try a common command?
We will next develop a command similar to ls, ls -all, which requires a node module commander. Let’s install it first:
Enter the root directory of the project and execute npm install --save commander,
Then open bin/hi.js with your favorite editor and replace the original code with the following:
'use strict';
var program = require('commander');
program
.version('0.0.1');//Declare the version number of hi
program
.command('list')//Declare that there is a command under hi called list
.description('list files in current working directory')//Give a description of the list command
.option('-a, --all', 'Whether to display hidden files')//Set the parameters of the list command
.action(function(options) {//Implementation body of list command
var fs = require('fs');
//Get file information in the current running directory
fs.readdir(process.cwd(), function(err, files) {
var list = files;
Check whether the user has given the --all or -a parameter, if not, filter out those files starting with.
list = files.filter(function(file) {
return file.indexOf('.') !== 0;
});
}
console.log(list.join(' '));//The console prints out all file names
});
});
program.parse(process.argv);//Start parsing the command entered by the user
OK, now let’s try the command we just wrote,
hi list
How to publish
First we need to create a project on Github and synchronize the code we just wrote.
Then publish your cli to npm via npm publish command.
Then other users can use npm install -g [project name] to install your command locally and use it