1. Use the default option in minutes to install Nodejs
2. Use sudo npm install-g yo install yeoman in minutes
3. Installing CLI development scaffolding via sudo npm install-g generator-cli-starter
OK, now we can start our CLI development tour using the Yo cli-starter command.
To create a CLI project
Copy Code code as follows:
Follow the prompts to enter the project name, command name, and complete the CLI project creation. In subsequent tutorials we will use Hi as your command name, and if you use other command names, be careful to replace
Let ' s try A
Now enter the following command on the command line (if you are not using Hi, pay attention to replace)
Copy Code code as follows:
The effect is as follows:
Try a common order?
We will then develop a similar to LS, Ls-all command, which needs to use a node module commander, first to install:
Go to the project root directory to perform npm install--save commander,
Then open the bin/hi.js with your favorite editor and replace the original code with the following:
Copy Code code as follows:
#!/usr/bin/env node
' Use strict ';
var program = require (' Commander ');
Program
. Version (' 0.0.1 ');//Declare Hi's edition number
Program
. Command (' list ')/Declaration Hi there is a command called list
. Description (' list files in current working directory ')//Give a description of the list this command
. Option (' A,--all ', ' Whether to display hidden files ')//Set the parameters of the list this command
. Action (function (options) {//list The implementation of the command
var fs = require (' FS ');
Get the file information in the current running directory
Fs.readdir (PROCESS.CWD (), function (err, files) {
var list = files;
if (!options.all) {//Check whether the user has given--all or-a parameters, if not, filter out those files that begin with.
List = Files.filter (function (file) {
Return File.indexof ('. ')!== 0;
});
}
Console.log (List.join ('));//console prints all file names
});
});
Program.parse (PROCESS.ARGV)//Start parsing user input commands
OK, now let's try the order we just wrote.
Copy Code code as follows:
Hi List
Copy Code code as follows:
How to publish
First you need to create a project on the GitHub and sync up the code we just wrote.
Then publish your CLI to NPM via the NPM Publish command.
Then other users can install your command locally using the NPM install-g [project name]