Node.js How to automatically approve a team's code _node.js

Source: Internet
Author: User
Tags commit git hooks

Objective

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

Here are three tools and how to use them:

Pre-git

The tool implements the functionality of Git hooks and inserts some custom behavior into Git's process, such as executing code detection before a commit, or an error if not passed.

Eslint

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

Js-beautiful

JS code collation, landscaping tools.

The three tools then work together to create the following effects:

1. The project leader defines eslint code specifications.

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

3. Automatically "git Add." If passed, and finally allow push.

Realize

One: NPM installs the above tools

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

Two: the configuration of tools

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

Note: For more testing, please go to eslint website to view

{"
  rules": {"
    comma-dangle": ["Error", "Never"],
    "Arrow-body-style": ["warn", "always"],
    " No-const-assign ": [" Error "]
    },
  " Parseroptions ": {
    " ecmaversion ": 6
  }
}

Because of the test, there is always an error when using js-beautiful recursive multi-tier files in bash, so the code is beautified by a single script:

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 ${pathN
      AME}-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 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 Ares 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, and if there is a mistake, the submission will be stopped and the submission can be continued after the modification is completed.

One thing to note is that some format problems are not enough to correct the error, the modified method will automatically modify the optimization code, and automatically add changes, the last step, execution:git push can! Can be combined with unit testing, better

Summarize

The above is for everyone to organize how to use Node.js automatic audit team Code of the entire content, there is a need to reference learning.

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.