Node. js How to Automatically Audit Team Code

  • 2021-07-04 18:07:33
  • OfStack

Preface

In team development, whether we write front-end (js, css, html) or back-end, we often need to solve one problem: how to unify the team code style. This article mainly uses pre-git, eslint, js-beautify to implement code style control.

These three tools and how to use them are described below:

pre-git

This tool can realize the function of git hook, insert some custom behaviors in the process of git, for example, execute code detection before commit, and report error if it fails.

eslint

Code Format Auditing Tool, you can configure a variety of styles at will, used to form a team of Codex 1 specifications.

js-beautiful

js code collation, beautification tools.

Then these three tools work together to form the following effect:

1. The project leader defines the code specification of eslint.

2. Use pre-git to run eslint code monitoring and js-beautiful code beautification before commit

3. "git add." Automatically if passed, and finally push is allowed.

Realization

1: npm Install the above tools

$ npm install eslint js-beautify pre-git --save-dev

2: Configuration of tools

Create a new. eslintrc. json file in the root directory, and configure the specification, and give a simplified version under 1:

Note: For more tests, please check in eslint official website


{
  "rules": {
    "comma-dangle": ["error", "never"],
    "arrow-body-style": ["warn", "always"],
    "no-const-assign": ["error"]
    },
  "parserOptions": {
    "ecmaVersion": 6
  }
}

Because of testing, bash used js-beautiful recursive multi-layer files when there are always errors, so by 1 script for code beautification:

beatufyjs.js


const fs = require( 'fs' );
const path = require( 'path' );
const child_process = require( 'child_process' );

for( let arg of process.argv.splice( 2 ) ) {
  let pathName = path.join( process.cwd(),arg );
  if( isFile( path.join( process.cwd(),arg ) ) ) {
    child_process.exec( `./node_modules/js-beautify/js/bin/js-beautify.js -P -E -j -a ${pathName} -r` , function( error, msg, stderr ) {
      console.log( msg.replace('\\\\n','') );
    } );
  } else {
    read_dir( pathName );
  }
}

function read_dir( dir ){
  let files = fs.readdirSync( dir );
  for( let file of files ) {
    let pathName = path.join( dir,file );
    if( isFile( pathName ) ) {
      child_process.exec( `./node_modules/js-beautify/js/bin/js-beautify.js -P -E -j -a ${pathName} -r` , function( error, msg, stderr ) {
        console.log( msg.replace( '\\\\n','') );
      } );
    } else {
      read_dir( pathName );
    }
  }
}

function isFile( path ){ 
  return exists( path ) && fs.statSync( path ).isFile(); 
} 

function exists( path ){ 
   return fs.existsSync( path ) || path.existsSync( path ); 
} 

3: Use the tools above

Configure in the package. json file:


{
 "name": "demo",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
  "lint": "./node_modules/.bin/eslint routes runtime utils libs --quiet",
  "lint-fix": "./node_modules/.bin/eslint routes runtime utils libs --quiet --fix",
  "js-beautify": "node --harmony --use_strict ./bin/beatufyjs.js libs middlewares index.js "
 },
 "author": "kelvv",
 "license": "ISC",
 "config": {
  "pre-git": {
   "commit-msg": "",
   "pre-commit": [
    "npm run lint-fix",
    "npm run js-beautify",
    "git add ."
   ],
   "pre-push": [],
   "post-commit": [],
   "post-checkout": [],
   "post-merge": []
  }
 },
 "devDependencies": {
  "eslint": "^2.12.0",
  "js-beautify": "^1.6.3",
  "pre-git": "^3.9.1"
 }
}

At this point, when you modify one of the files, and then "git add & & git commit-m 'msg' ", the three commands in pre-commit will be executed. If there is an error in the middle, it will stop submitting, and then continue submitting after modification.

One point should be noted that some format problems are not enough to report errors, and the modification method will automatically modify the optimization code and automatically add modifications. The last step is to execute: git push! It can be combined with unit testing, preferably

Summarize

The above is how to arrange for everyone to use Node. js to automatically audit the whole content of the team's code. If necessary, you can refer to it.


Related articles: