Node.js優(yōu)雅地自動(dòng)審核團(tuán)隊(duì)的代碼

在團(tuán)隊(duì)開(kāi)發(fā)中,無(wú)論是寫(xiě)前端(js,css,html) ,還是后端 ,我們需要解決一個(gè)問(wèn)題:如何統(tǒng)一團(tuán)隊(duì)代碼風(fēng)格。 這篇文章主要是使用pre-git , eslint , js-beautify 實(shí)現(xiàn)代碼風(fēng)格控制。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、沙雅網(wǎng)絡(luò)推廣、微信小程序、沙雅網(wǎng)絡(luò)營(yíng)銷、沙雅企業(yè)策劃、沙雅品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供沙雅建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:bm7419.com

下面分別介紹這三個(gè)工具和使用方式:

  1. pre-git
    該工具能實(shí)現(xiàn)git hook的功能,在git的流程中插入一些自定義行為,例如commit之前執(zhí)行代碼檢測(cè),如果不通過(guò)則報(bào)錯(cuò)。

  2. eslint
    代碼格式審核工具,可以隨意組合配置各種風(fēng)格,用于組成團(tuán)隊(duì)的代碼統(tǒng)一規(guī)范。

  3. js-beautiful
    js代碼整理、美化工具。

然后這三個(gè)工具互相配合就形成了以下效果:

  • 項(xiàng)目組長(zhǎng)定義好eslint的代碼規(guī)范。

  • 使用pre-git在commit之前運(yùn)行eslint代碼監(jiān)測(cè)和js-beautiful代碼美化

  • 如果通過(guò)則自動(dòng)"git add ." ,最后允許push。

實(shí)現(xiàn)

一:npm安裝上述工具

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

二:工具的配置

在根目錄新建.eslintrc.json文件,并且把規(guī)范配置好,一下給一個(gè)精簡(jiǎn)版:

注意:如需更多檢測(cè),請(qǐng)到eslint官網(wǎng)查看

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

因測(cè)試,bash 中使用js-beautiful遞歸多層文件的時(shí)候總出現(xiàn)錯(cuò)誤,所以由一腳本來(lái)進(jìn)行代碼美化:

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 );  
}

三:使用上述工具

在package.json文件中配置:

{
  "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"
  }
}

此時(shí)當(dāng)你修改其中一個(gè)文件,然后"git add && git commit -m 'msg' "的時(shí)候,pre-commit中的三條命令就會(huì)執(zhí)行,如果中途有錯(cuò)就會(huì)停止提交,修改完畢后再繼續(xù)提交。

有一點(diǎn)需要注意的是,有的格式問(wèn)題不足以報(bào)錯(cuò)的話,改方法會(huì)自動(dòng)修改優(yōu)化代碼,并且自動(dòng)添加修改,最后一步,執(zhí)行:git push即可!

可以結(jié)合單元測(cè)試,更佳

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

文章標(biāo)題:Node.js優(yōu)雅地自動(dòng)審核團(tuán)隊(duì)的代碼
本文URL:http://bm7419.com/article42/jdgohc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、云服務(wù)器品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)