Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

简介

在团队开发中,无论是写前端(js,css,html) ,还是后端 ,我们需要解决一个问题:如何统一团队代码风格。 这篇文章主要是使用pre-git , eslint , js-beautify 实现代码风格控制。

下面分别介绍这三个工具和使用方式:

  1. pre-git

    该工具能实现git hook的功能,在git的流程中插入一些自定义行为,例如commit之前执行代码检测,如果不通过则报错。

  2. eslint

    代码格式审核工具,可以随意组合配置各种风格,用于组成团队的代码统一规范。

  3. 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即可!

可以结合单元测试,更佳

zui后:感谢阅读,本人github , 我是kelvv

Node.js 优雅地自动审核团队的代码的更多相关文章

  1. KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情

    KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...

  2. Node.js 部署免费/自动续订 HTTPS

    随着互联网快速发展,互联网信息安全越来越受到大家重视,HTTPS 应该是近两年各大厂商都在尽力普及的技术之一.国内大厂基本上已经全面普及了 HTTPS. 本文首发于我的个人网站:听说 - https: ...

  3. KoaHub.JS用于Node.js的可移植Unix shell命令程序代码

    shelljs Portable Unix shell commands for Node.js ShellJS - Unix shell commands for Node.js     Shell ...

  4. KoaHub平台基于Node.js开发的Koa JWT认证插件代码信息详情

    koa-jwt Koa JWT authentication middleware. koa-jwt Koa middleware that validates JSON Web Tokens and ...

  5. node.js 远程调试debug产线环境代码

    一.背景: 产线机器出bug,不能重启服务,需要保留现场,问题不好排查,只能靠远程debug. 二.实现步骤 1. 登录远程机器执行如下命令,nodePid为node服务的pid kill -usr1 ...

  6. KoaHub平台基于Node.js开发的Koa的skip插件代码详情

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  7. KoaHub.JS基于Node.js开发的处理和显示日期代码

    moment Parse, validate, manipulate, and display dates      A lightweight JavaScript date library for ...

  8. KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情

    koa-router Router middleware for koa. Provides RESTful resource routing. koa-router       Router mid ...

  9. KoaHub平台基于Node.js开发的Koa EJS渲染插件代码信息详情

    koa-ejs ejs render middleware for koa koa-ejs Koa ejs view render middleware. support all feature of ...

随机推荐

  1. 腾讯云服务器安装宝塔面板快速配置LNMP/LAMP网站系统

    我们在选择购买腾讯云服务器之后,有部分用户肯定是用来建站用途的.毕竟云服务器的性能和功能比虚拟主机优秀很多.腾讯云服务器拥有香港.北京.广州.上海.美国等多个机房,可以安装Linux和Windows系 ...

  2. JSONP-跨域读取数据

    页面代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  3. github上关于campbell数据采集的一些代码。

    数据自动采集: https://github.com/USGS-OWI/deployer-campbell   program that reads loggernet files and refor ...

  4. [转] flume使用(六):后台启动及日志查看

    [From] https://blog.csdn.net/maoyuanming0806/article/details/80807087 处理的问题flume 普通方式启动会有自己自动停掉的问题,这 ...

  5. Python3 print()函数sep,end,file参数用法练习

    来自builtins.py:def print(self, *args, sep=' ', end='\n', file=None): # known special case of print &q ...

  6. (转)CentOS 7 安装 Docker

    原文:http://www.cnblogs.com/stulzq/p/7743073.html http://www.cnblogs.com/stulzq/p/8629165.html-------- ...

  7. (转)MySql数据库4【命令行赋权操作】

    MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 原文:http://www.cnblogs.com/zhuyibo/p/3980328.html 一.g ...

  8. spring boot快速入门 9: 单元测试

    进行单元测试: service第一种方式: 第一步:在指定service中创建一个方法进行测试 /** * 通过ID查询一个女生的信息 * @param id * @return */ public ...

  9. jquery的animate关于background-position属性

    jQuery 的 animate 虽然能直接使用 CSS 的方式来进行动画,但有些属性其实是不支持的,例如:background-position. 谷歌支持 background-position- ...

  10. Golang真言

    Don't communicate by sharing memory, share memory by communicating. Concurrency is not parallelism. ...