您好,登录后才能下订单哦!
在团队开发中,无论是写前端(js,css,html) ,还是后端 ,我们需要解决一个问题:如何统一团队代码风格。 这篇文章主要是使用pre-git , eslint , js-beautify 实现代码风格控制。
下面分别介绍这三个工具和使用方式:
pre-git
该工具能实现git hook的功能,在git的流程中插入一些自定义行为,例如commit之前执行代码检测,如果不通过则报错。
eslint
代码格式审核工具,可以随意组合配置各种风格,用于组成团队的代码统一规范。
js-beautiful
js代码整理、美化工具。
然后这三个工具互相配合就形成了以下效果:
项目组长定义好eslint的代码规范。
使用pre-git在commit之前运行eslint代码监测和js-beautiful代码美化
如果通过则自动"git add ." ,最后允许push。
一:npm安装上述工具
$ npm install eslint js-beautify pre-git --save-dev
二:工具的配置
在根目录新建.eslintrc.json文件,并且把规范配置好,一下给一个精简版:
注意:如需更多检测,请到eslint官网查看
{ "rules": { "comma-dangle": ["error", "never"], "arrow-body-style": ["warn", "always"], "no-const-assign": ["error"] }, "parserOptions": { "ecmaVersion": 6 } }
因测试,bash 中使用js-beautiful递归多层文件的时候总出现错误,所以由一脚本来进行代码美化:
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" } }
此时当你修改其中一个文件,然后"git add && git commit -m 'msg' "的时候,pre-commit中的三条命令就会执行,如果中途有错就会停止提交,修改完毕后再继续提交。
有一点需要注意的是,有的格式问题不足以报错的话,改方法会自动修改优化代码,并且自动添加修改,最后一步,执行:git push即可!
可以结合单元测试,更佳
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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。