-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 1.96 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
var fs = require('fs');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var dnode = require('dnode');
var seq = require('seq');
var hook = require('./lib/hook');
var hookFile = __dirname + '/bin/hook.js';
module.exports = function (repoDir, cb) {
var hookDir = path.resolve(repoDir) + '/hooks';
var emitter = new EventEmitter;
var port = Math.floor(Math.random() * ((1<<16) - 1e4) + 1e4);
emitter.close = function () {
server.close();
};
var server = emitter.server = dnode(function (remote, conn) {
this.emit = function (hookName, args, finish) {
var xs = emitter.listeners(hookName);
if (xs.length === 0) finish(true)
else if (!hook.canAbort[hookName]) {
finish(true);
emitter.emit(hookName, hook(hookName, args));
}
else {
var pending = xs.length;
var allOk = true;
emitter.emit(hookName, hook(hookName, args, function (ok) {
allOk = allOk && ok;
if (--pending === 0) {
finish(allOk);
}
}));
}
};
}).listen(port);
seq()
.seq(function () {
fs.writeFile(hookDir + '/.git-emit.port', port.toString(), this);
})
.set(hook.names)
.parEach_(function (next, name) {
var file = hookDir + '/' + name;
fs.lstat(file, function (err, s) {
if (err && err.code === 'ENOENT') {
fs.symlink(hookFile, file, next)
}
else if (err) next(err)
else if (s.isSymbolicLink()) next()
else next('hook file already exists: ' + file)
});
})
.seq(function () {
if (cb) cb(null, emitter)
})
.catch(cb || console.error)
;
return emitter;
};