-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitwin.js
More file actions
101 lines (86 loc) · 2.63 KB
/
gitwin.js
File metadata and controls
101 lines (86 loc) · 2.63 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var events = require('events'),
colors = require('colors');
var lineBreak = '\n- - - - - - - - - - - - - - - -';
var gitwin = function(){
events.EventEmitter.call(this);
this.path = process.cwd();
this.verbose = true;
}
gitwin.prototype.__proto__ = events.EventEmitter.prototype;
gitwin.prototype.buildCommand = function(cmd) {
return 'cd '.concat(this.path,' & git ',cmd);
}
gitwin.prototype.execute = function (cmd) {
var self = this;
var childProcess = require('child_process'), ls;
ls = childProcess.exec(cmd, function (error, stdout, stderr) {
var msg = '';
if (error) {
msg = ('\nstack...\n' + error.stack).grey+('\n failed - errors'.white.redBG);
self.emit('error',error.code,msg);
return;
}
else{
msg = stdout.grey+('\n finished - no errors'.white.greenBG);
self.emit('done',null,msg);
}
});
}
gitwin.prototype.emitStatusStart = function(action){
var startingMsg = (action+' starting').cyan;
this.emit('status',null,startingMsg);
}
gitwin.prototype.pull = function() {
this.emitStatusStart('git pull');
var cmd = this.buildCommand('pull');
return this.execute(cmd);
}
gitwin.prototype.status = function() {
this.emitStatusStart('git status');
var cmd = this.buildCommand('status');
return this.execute(cmd);
}
gitwin.prototype.log = function (error, stdout, stderr) {
var msg = 'git status: '.concat(stdout);
if (error != null) {
msg = msg.concat('\nERROR: unable to process ',error,' /n FULL ERROR: ',stderr);
}
console.log(msg);
};
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
gitwin.prototype.help = function(){
console.log('\n*****************');
console.log('git win - help');
console.log('*****************');
var events = ['pull','status','log'];
var msg = ('available events:');
events.forEach(
function(name){
if(msg.endsWith(':')){
msg+=' '; }
else {
msg+=', '}
msg+=name.cyanBG;
});
console.log(msg);
console.log('\n event example usage (log): '.grey+'\n "gitwin_instance.on(\'log\',function(error,msg){console.log(msg);});"'.cyan);
}
module.exports = function(callback){
var _gitwin = new gitwin(callback);
_gitwin.on('status',function(err,results){
if(this.showHelp) return;
if(err){ console.log(err.redBG);};
console.log(results);}
);
_gitwin.on('error',function(err,results){ console.log('error'.red); if(err){ console.log(err.redBG);}; console.log(results.red);});
_gitwin.on('done',function(err,results){
console.log(results);
console.log('done'.cyan);
console.log(lineBreak);
if(typeof callback == 'function') callback();
}
);
return _gitwin;
};