Using Nodejs to develop a CLI project instance _node.js

Source: Internet
Author: User

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:

Yo Cli-starter

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:

Hi

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-v

Hi List

Copy Code code as follows:

Hi list-a

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.