Preface
In team development, whether it is writing front-end (js, css, html) or back-end, we often need to solve a problem: how to unify the team's code style. This article mainly uses pre-git, eslint, and js-beautify to implement code style control.
The following are the three tools and usage methods:
pre-git
This tool can implement the function of git hook, insert some custom behaviors into the git process, such as executing code detection before commit, and if it does not pass, an error will be reported.
eslint
Code format audit tool, you can combine and configure various styles at will, and use it to form a team's unified code specifications.
js-beautiful
js code organization and beautification tools.
Then the three tools cooperate with each other to create the following effects:
1. The project leader defines the code specifications of eslint.
2. Use pre-git to run eslint code monitoring and js-beautiful code beautification before commit
3. If passed, automatically "git add." will be allowed to push.
accomplish
1: npm install the above tools
$ npm install eslint js-beautify pre-git --save-dev
Two: Tool configuration
Create a new .eslintrc.json file in the root directory and configure the specifications, and give it a simplified version:
Note: If you need more testing, please visit eslint official website
{ "rules": { "comma-dangle": ["error", "never"], "arrow-body-style": ["warn", "always"], "no-const-assign": ["error"] }, "parserOptions": { "ecmaVersion": 6 }}Due to testing, there are always errors when using js-beautiful recursive multi-layer files in bash, so a script is used to beautify the code:
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 ); }Three: Use the above tools
Configure in 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 time, 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, the submission will be stopped and the submission will be continued after the modification is completed.
One thing to note is that if some format problems are not enough to report an error, the modification method will automatically modify the optimization code and automatically add and modify it. In the last step, just execute: git push ! Can be combined with unit tests, better
Summarize
The above is all the content of how to automatically review the team's code using Node.js. If you need it, you can refer to it.