Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed May 3, 2017
1 parent d44769e commit 6625d0e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 171 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# fecs-git-hooks [![Version](https://badge.fury.io/js/fecs-git-hooks.svg)](https://www.npmjs.com/package/fecs-git-hooks)

## 自动方式(推荐)
## Usage

项目根目录下运行
### install

$ npm i fecs-git-hooks
```
npm i fecs-git-hooks
```

## 命令行方式
### uninstall

安装:

$ npm install fecs-git-hooks -g

使用:

$ fecs-hook init
$ fecs-hook disable
```
npm uni fecs-git-hooks
```
4 changes: 0 additions & 4 deletions bin/fecs-hook

This file was deleted.

101 changes: 44 additions & 57 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,69 @@ var fs = require('fs');
var path = require('path');

var IGNORE_FILENAME = '.fecsignore';
var name = 'fecs-git-precommit-check';

var ROOT = path.resolve(__dirname, '../..');

var fecs;
var minimatch;

console.log('要提交代码先要通过fecs审查哦!\n');

exec('npm root -g', function (error, result, stderr) {

if (error !== null) {
console.log('exec error: ' + error);
process.exit(1);
}

console.time(name);

var fecsRoot = result.trim() + '/fecs';
try {
require(fecsRoot);
}
catch (e) {
console.log('还没有安装fecs,请先运行\'npm install fecs -g\'~~');
process.exit(1);
}

fecs = require(fecsRoot);
minimatch = require(fecsRoot + '/node_modules/minimatch');
var fecsRoot = path.resolve(ROOT, 'node_modules/fecs');
try {
require(fecsRoot);
}
catch (e) {
console.log('还没有安装fecs,请先运行\'npm install fecs\'~~');
process.exit(1);
}

// console.log('开始检查...');
fecs = require(fecsRoot);
minimatch = require(path.resolve(ROOT, 'node_modules/minimatch'));

var stagedFiles;
var stagedFiles;

exec('git diff-index --cached --name-only HEAD', function (error, result, stderr) {
if (error !== null) {
console.log('' + error);
exec('git diff-index --cached --name-only HEAD', function (error, result, stderr) {
if (error !== null) {
console.log('' + error);

if (('' + error).indexOf('unknown revision') > 0) {
console.log('仓库还没有提交记录,默认对所有文件进行检查');
stagedFiles = ['./**/*.{js,css,less,html}']
}
else {
process.exit(1);
}
if (('' + error).indexOf('unknown revision') > 0) {
console.log('仓库还没有提交记录,默认对所有文件进行检查');
stagedFiles = ['./**/*.{js,css,less,html,vue,styl}']
}
// 还有文件未添加
if (result.indexOf('Changes not staged for commit:') >= 0) {
console.log(result);
else {
process.exit(1);
}

console.log('提交的文件列表:');
}
// 还有文件未添加
if (result.indexOf('Changes not staged for commit:') >= 0) {
console.log(result);
process.exit(1);
}

stagedFiles = stagedFiles || result.trim().split('\n');
exitWhenEmpty(stagedFiles);
console.log('提交的文件列表:');
console.log(result);

// 去掉删除的(不存在)文件
stagedFiles = stagedFiles.filter(function (file) {
var filePath = path.resolve(__dirname, '../../', file);
stagedFiles = stagedFiles || result.trim().split('\n');
exitWhenEmpty(stagedFiles);

return fs.existsSync(filePath);
});
// 去掉删除的(不存在)文件
stagedFiles = stagedFiles.filter(function (file) {
var filePath = path.resolve(__dirname, '../../', file);

return fs.existsSync(filePath);
});

stagedFiles = ignore(stagedFiles);
exitWhenEmpty(stagedFiles);
stagedFiles = ignore(stagedFiles);
exitWhenEmpty(stagedFiles);

var patterns = require(fecsRoot + '/lib/util').buildPattern(stagedFiles, 'js,css,less,html');
patterns.specials && delete patterns.specials;
var patterns = require(fecsRoot + '/lib/util').buildPattern(stagedFiles, 'js,css,less,html');
patterns.specials && delete patterns.specials;

// console.log(patterns);
// console.log(patterns);

exitWhenEmpty(patterns);
fecsCheck(stagedFiles);
});
exitWhenEmpty(patterns);
fecsCheck(stagedFiles);
});

function exitWhenEmpty(files) {
Expand Down Expand Up @@ -143,14 +132,12 @@ function fecsCheck(files) {
};

var done = function (success, json) {
console.timeEnd(name);

success = success && json.length === 0;
if(!success) {
if (!success) {
console.log('看来规范还有问题,请修改以后再提交吧!');
}
console.log('');
process.exit(success ? 0 : 1);
process.exit(success ? 0 : 1);
};

fecs.check(options, done);
Expand Down
88 changes: 0 additions & 88 deletions lib/cli.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file npm install 时自动运行的脚本
* @author chenxiao07
* @author cxtom
*/

/* eslint-disable no-console */
Expand Down
15 changes: 15 additions & 0 deletions lib/uninstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file npm uninstall 时自动运行的脚本
* @author cxtom
*/

/* eslint-disable no-console */

var Util = require('./util');

try {
Util.unInstallHooks('pre-commit');
}
catch (e) {
console.trace(e);
}
26 changes: 25 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ exports.installHooks = function (hooks, root) {
}
};

exports.unInstallHooks = function (hooks, root) {

hooks = Array.isArray(hooks) ? hooks : [hooks];
var gitRoot = exports.findGitRoot(root);
var hookRoot = Path.join(gitRoot, '.git', 'hooks');

if (!exports.isDir(hookRoot)) {
return;
}

for (var i = 0, il = hooks.length; i < il; ++i) {
var hook = hooks[i];
var dest = Path.join(hookRoot, hook);

if (Fs.existsSync(dest)) {
Fs.unlinkSync(dest);

if (Fs.existsSync(dest + '.backup')) {
Fs.renameSync(dest + '.backup', dest);
}
}
}

};


// Given a starting directory, find the root of a git repository.
// In this case, the root is defined as the first directory that contains
Expand Down Expand Up @@ -186,4 +211,3 @@ exports.isDir = function (path) {
return false;
}
};

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "fecs-git-hooks",
"version": "0.1.1",
"description": "Fecs git pre-commit hook",
"version": "1.0.0-beta.1",
"description": "fecs git pre-commit hook",
"repository": {
"type": "git",
"url": "https://github.com/cxtom/fecs-git-hooks"
},
"scripts": {
"install": "fecs -v && node ./lib/install.js"
"install": "fecs -v && node ./lib/install.js",
"uninstall": "node ./lib/uninstall.js"
},
"keywords": [
"fecs"
Expand All @@ -20,10 +21,8 @@
"bugs": {
"url": "https://github.com/cxtom/fecs-git-hooks/issues"
},
"bin": {
"fecs-hook": "./bin/fecs-hook"
},
"dependencies": {
"minimist": "^1.1.0"
"peerDependencies": {
"fecs": "^1.2.0",
"minimatch": "^3.0.2"
}
}

0 comments on commit 6625d0e

Please sign in to comment.