forked from richmarr/stagecoach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.js
executable file
·75 lines (70 loc) · 2.15 KB
/
exec.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
#!/usr/bin/env node
var argv = require('../lib/argv');
if (!argv.hub) return console.error('Specify a --hub or set a remote.');
var propagit = require('propagit');
var git = require('../lib/git');
var EventEmitter = require('events').EventEmitter;
var p = propagit(argv);
p.on('error', function (err) {
console.dir(err);
});
p.hub(function (hub) {
var opts = {
drone : argv.drone,
drones : argv.drones,
repo : argv.repo || git.repoName(),
commit : argv.commit,
command : argv._,
env : argv.env || {},
cwd : argv.cwd,
once : true
};
if (!opts.repo) {
console.error('specify --repo or navigate to a git repo');
return;
}
if (!opts.commit) git.commit(function (err, commit) {
if (err) {
console.error(err);
p.hub.close();
}
else {
opts.commit = commit;
spawn(hub, opts);
}
})
else spawn(hub, opts);
});
function spawn (hub, opts) {
var em = new EventEmitter;
hub.subscribe(em.emit.bind(em));
em.on('ready', function () {
hub.spawn(opts, function (err, procs) {
if (err) {
console.error(err)
p.hub.close();
}
else {
em.on('stdout', function (buf, proc) {
if (procs[proc.drone] !== proc.id) return;
console.log(
'[' + proc.drone + '#' + proc.id + '] '
+ buf.replace(/\n$/, '')
);
});
em.on('stderr', function (buf, proc) {
if (procs[proc.drone] !== proc.id) return;
console.log(
'[' + proc.drone + '#' + proc.id + '] '
+ buf.replace(/\n$/, '')
);
});
em.on('exit', function (code, sig, proc) {
if (procs[proc.drone] !== proc.id) return;
console.log('(' + proc.drone + '#' + proc.id + ' exited)');
p.hub.close();
});
}
});
});
}