background

Command line tools are very familiar to us, and some command line operations greatly simplify our daily work. This article is based on a Node command line counter I wrote.

For some ps,grep,cp,mv… Commands should be easy to use, which is why I want to develop a convenient command line. Second, I want to record the whole process of a small open source toy.

Features of the command line:

  • Easy to operate
  • Visibility is strong

A look at some of the current command lines has the following problems

  • Less species github.com/Towtow10/li…
  • The appearance level is not enough github.com/AlDanial/cl…
  • Statistics are not very convenient github.com/ryanfowler/…

Hence the birth of a convenient statistical tool with a high level of appearance.

High yan figure

Toy source

Github.com/hua1995116/…

To prepare

Third-party libraries

  • cli-table
  • colors
  • commander
  • ignore

Dev library (for testing)

  • chai
  • mocha
  • codecov
  • istanbu

The Node compatibility

  • babel

Static files

  • Language mapping library
  • Color library

Train of thought

Use Commander to get some of the user’s custom configurations

program
    .version('0.1.0 from')
    .option('-i, --ignore [dir]'.'ignore dir')
    .option('-p, --path [dir]'.'ignore dir')
    .parse(process.argv);

Copy the code

Node traverses the file with the number of lines in each language

function getFile(dirPath) {

    const files = fs.readdirSync(dirPath);

    files.forEach((item) = >{... })}Copy the code

Ignore Filters output to the cache

function handleIgnode(cPath) {
    try {
        const currentPath = path.join(ROOTPATH, '.gitignore');
        const fileData = fs.readFileSync(currentPath, 'utf-8');
        const ignoreList = fileData.split('\n');
        const filterList = filterData(ignoreList);
        const ig = ignore().add(filterList);
        return ig.ignores(cPath);
    } catch (e) {
        return false; }}Copy the code

Traversing the cache, counting the max-line, and passing colors

function hanldeTable(){...if(maxCount < langInfo[item].totalLines) { maxCount = langInfo[item].totalLines; maxIndex = index; }... }Copy the code

Cli-table Displays the command output

function outputTbale() {
    const {
        header,
        content,
        bottom
    } = initTable();

    const{ totalFiles, totalCode, totalBlank, tablesContent } = hanldeTable(); .console.log(`T=${totalTime} s`.` (${fileSpeed} files/s`.`${lineSpeed} lines/s)`)
    console.log(header.toString())
    console.log(content.toString())
    console.log(bottom.toString())
}
Copy the code

To improve the

loading

For multi-file directories, loading is supported

lass StreamLoad {
    constructor(option) {
        this.stream = option.stream;
        this.text = option.text;
        this.clearLine = 0;
    }
    setValue(value) {
        this.text = value;
        this.render();
    }
    render() {
        this.clear();
        this.clearLine++;
        this.stream.write(`read The ${this.text} file\n`);
    }
    clear() {
        if(!this.stream.isTTY) {
            return this;
        }
        
        for (let i = 0; i < this.clearLine; i++) {
            this.stream.moveCursor(0, -1);
            this.stream.clearLine();
			this.stream.cursorTo(0);
        }
        this.clearLine = 0; }}const progress = new StreamLoad({
    stream: process.stderr,
    text: 0
})

Copy the code

Create a class that implements Loading. The main processing method is in readline, see nodejs.org/dist/latest…

babel

Compatibility with earlier versions of Node

cnpm i babel-cli
Copy the code

package.json

"build": "./node_modules/.bin/babel src --out-dir lib"
Copy the code

The test case

Chai, mocha

Used to test whether the traversal file is correct

const path = require('path');
const assert = require('chai').assert;

const {getFileData} = require('.. /src/linec');

describe('linec files test'.() = > {
    it('can linec dir'.() = > {
        const url = path.join(__dirname, '.. /example');
        console.log(url);

        const dirObj = JSON.stringify(getFileData(url));

        const expectData = '{"CSS":{"file":1,"blankLines":0,"totalLines":4,"color":"#563d7c"},"JavaScript":{"file":1,"blankLines":0,"totalLines":1, "color":"#f1e05a"},"JSON":{"file":1,"blankLines":0,"totalLines":3,"color":"#fff"},"Markdown":{"file":1,"blankLines":0,"t otalLines":1,"color":"#fff"}}'; assert.equal(dirObj, expectData); })})Copy the code

run

./node_modules/mocha/bin/mocha
Copy the code

Code coverage tests have also been added to this project, so this is it

"test": "./node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha && ./node_modules/.bin/codecov",
Copy the code

release

Step1

Open www.npmjs.com/signup

Set up an account

step2

If you have an account, go straight to this step

npm login
Copy the code

step3

Add version to package.json

{" name ":" linec ", "version" : "1", "description" : "line count", "main" : "index. Js",... }Copy the code

step4

npm publish
Copy the code

Note that you need to change the version in package.json every time you release, otherwise the release will fail.

The command line

package.json

"bin": {
	"linec": "./lib/index.js"
},
Copy the code

Local project command line

npm link
Copy the code

You can use the linec command to soft connect the linec command to the local. The name linec can be customized.

Remote command line

The default is the package name, but if bin defines a name, ditto, you can change the name. That is, the package name can be different from the command, but for convenience, I recommend that the package name and command be unified.

Details you can refer to www.ruanyifeng.com/blog/2015/0…

Continuous integration testing & automatic coverage statistics

travis-ci.org/

Configuration. Travis. Yml

language: node_js

node_js:
  - "stable"

sudo: false

before_script:
  - npm install
Copy the code

This is my configuration. Every time you submit a file that contains the NPM run test command, Travis will call it and automatically detect it.

Travis also has the benefit of running test cases automatically when someone submits a PR to you, avoiding some low-level errors. Here’s how it looks.

codecov.io/gh

This is a tool to measure code coverage. Add it in the NPM run test and you can see the coverage statistics during PR

Installation & Use

$ npm install -g linec / cnpm install -g linec 
Copy the code

Basic usage

$ linec
Copy the code

Export to HTML

$ linec -o
Copy the code

An output.html file will appear in the current directory

function

  • Output blank lines, number of actual lines, total lines
  • Supports 400+ languages
  • Display traversal speed
  • Display multiple colors
  • Support for exporting HTML

Tool source (welcome star) github.com/hua1995116/…

rendering

Basic mode

Open the HTML after the export

At the end

The above is all content, may be for Node tool development I may still be in the fledgling stage, there is a more standardized operation, welcome big guys to give me correct.