node. js gracefully automatically review team code

Source: Internet
Author: User

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

Here are the three tools and how to use them:

    1. Pre-git
      This tool can implement the function of Git hook, insert some custom behavior in git process, such as commit code to detect before commits, if not pass the error.

    2. Eslint
      Code Format Audit tool, you can configure a variety of styles at will, used to form the code of the team uniform specification.

    3. Js-beautiful
      JS code collation, landscaping tools.

Then the three tools work together to create the following effects:

    • The project leader defines the code specification for Eslint.

    • Use Pre-git to run Eslint code monitoring and Js-beautiful code beautification before commit

    • If you pass the automatic "git Add.", finally allow push.

Realize

One: NPM installs the above tools

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

II: Configuration of the tool

Create a new. eslintrc.json file in the root directory and configure the specification to a lite version:

Note: If you need more testing, please visit Eslint website.

{"Rules": {"Comma-dangle": ["Error", "Never"], "Arrow-body-style": ["warn", "always"], "no-const -assign ": [" Error "]}," Parseroptions ": {" ecmaversion ": 6}}

As a result of the test, bash used js-beautiful recursive multi-layered files when there is always an error, so a script is a 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 (  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 ( p athname );     }}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 ', ')  ;            } );       &nbSp; } 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: Using the above tools

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 the pre-commit will be executed, and if there is an error in the middle, the commit will be stopped, and then the commit can be completed after the modification.

One thing to note is that some format problems are not enough to error, the method will automatically modify the optimization code, and automatically add changes, the last step, execute: git push!

Can be combined with unit testing, better

Gh.dokee.cn/article/content-2292769-34004.html
Gh.dokee.cn/article/content-2292768-34004.html
Gh.dokee.cn/article/content-2292766-34004.html
Gh.dokee.cn/article/content-2292765-34004.html
Gh.dokee.cn/article/content-2292764-34004.html
Gh.dokee.cn/article/content-2292763-34004.html
Gh.dokee.cn/article/content-2292762-34004.html
Gh.dokee.cn/article/content-2292761-34004.html
Gh.dokee.cn/article/content-2292760-34004.html
Gh.dokee.cn/article/content-2292759-34004.html
Gh.dokee.cn/article/content-2292758-34004.html
Bbs.open.qq.com/thread-15334805-1-1.html
Bbs.open.qq.com/thread-15335348-1-1.html
Bbs.open.qq.com/thread-15335576-1-1.html
Bbs.open.qq.com/thread-15335715-1-1.html
Http://bbs.open.qq.com/thread-15335916-1-1.html
Http://bbs.open.qq.com/thread-15335876-1-1.html
Http://bbs.open.qq.com/thread-15336398-1-1.html
Http://bbs.open.qq.com/thread-15336484-1-1.html
Http://bbs.open.qq.com/thread-15336547-1-1.html
Http://bbs.open.qq.com/thread-15336614-1-1.html
Http://bbs.open.qq.com/thread-15336697-1-1.html
Http://bbs.open.qq.com/thread-15336806-1-1.html
Http://bbs.open.qq.com/thread-15340763-1-1.html
Www.baiyewang.com/s4209086.html
http://bl.gamebbs.qq.com/forum.php?mod=viewthread&tid=11675819
Http://bbs.open.qq.com/thread-15343974-1-1.html
Http://bbs.open.qq.com/thread-15344107-1-1.html
http://caimi68.lofter.com/post/1e3e0a7a_bbb5392
http://caimi68.lofter.com/post/1e3e0a7a_bbb539f
http://caimi68.lofter.com/post/1e3e0a7a_bbb539e
http://caimi68.lofter.com/post/1e3e0a7a_bbb539d
http://caimi68.lofter.com/post/1e3e0a7a_bbb539c
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a0
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a1
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a2
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a4
Http://bbs.open.qq.com/thread-15345770-1-1.html
Http://bbs.open.qq.com/thread-15345813-1-1.html
Http://bbs.open.qq.com/thread-15345854-1-1.html
Http://bbs.open.qq.com/thread-15345888-1-1.html
Http://bbs.open.qq.com/thread-15345937-1-1.html
Http://bbs.open.qq.com/thread-15346013-1-1.html
Http://bbs.open.qq.com/thread-15346046-1-1.html
Http://bbs.open.qq.com/thread-15346098-1-1.html
Http://bbs.open.qq.com/thread-15346138-1-1.html
Http://bbs.open.qq.com/thread-15346194-1-1.html
Http://bbs.open.qq.com/thread-15346240-1-1.html
Http://bbs.open.qq.com/thread-15346345-1-1.html
http://g.jandan.net/s/6319
http://g.jandan.net/s/6320
http://g.jandan.net/s/6321
http://g.jandan.net/s/6322
http://g.jandan.net/s/6323


node. js gracefully automatically review team code

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.