Home > Web Front-end > JS Tutorial > body text

Using nodejs to develop cli project example_node.js

WBOY
Release: 2016-05-16 15:56:58
Original
1161 people have browsed it

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

Copy code The code is as follows:

yo cli-starter

Follow the prompts to enter the project name and command name to complete the cli project creation. In the following tutorials, we will use hi as your command name. If you use other command names, please replace

Let's try first

Now enter the following command in the command line (if you are not using hi, please replace it)

Copy code The code is as follows:

hi

The effect is as follows:

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:

Copy code The code is as follows:

#!/usr/bin/env node

'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,

Copy code The code is as follows:

hi -V

hi list

Copy code The code is as follows:

hi list -a

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template