-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
78 lines (67 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* global chmod */
'use strict';
require('shelljs/global');
var fs = require('vinyl-fs');
var map = require('map-stream');
var async = require('async');
var path = require('path');
var hooks = [
'applypatch-msg',
'commit-msg',
'post-applypatch',
'post-checkout',
'post-commit',
'post-merge',
'post-receive',
'post-rewrite',
'post-update',
'pre-applypatch',
'pre-auto-gc',
'pre-commit',
'pre-push',
'pre-rebase',
'pre-receive',
'prepare-commit-msg',
'update'
];
function install(hook, dest, cb) {
cb = cb || dest;
if (!cb) cb(new Error('Callback must be supplied.'));
if (typeof cb !== 'function') cb(new Error('Callback must be a function.'));
if (hooks.indexOf(hook) === -1) cb(new Error('Invalid hook name.'));
dest = ((typeof dest === 'function' ? null : dest) ||
exec('git rev-parse --show-toplevel')
.output.slice(0, -1) + '/.git/hooks/');
var destHook = path.join(dest, hook);
if (test('-f', destHook)) {
var bakDest = destHook + '.guppy';
if (!test('-f', bakDest)) {
mv(destHook, bakDest);
}
}
return fs.src(path.join(__dirname, 'scripts/hookfile'))
.pipe(map(function(file, next) {
file.path = file.path.replace('hookfile', hook);
next(null, file);
}))
.pipe(fs.dest(dest))
.on('finish', function () {
chmod('u+x', destHook);
cb(null, destHook);
})
.on('error', function (err) {
cb(err);
});
}
function installAll(dest, cb) {
async.parallel(
hooks.map(function (hook) {
return function (next) {
return install(hook, dest, next);
};
}),
cb
);
}
module.exports.install = install;
module.exports.installAll = installAll;