forked from pvorb/node-git-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
78 lines (65 loc) · 1.71 KB
/
git.js
File metadata and controls
78 lines (65 loc) · 1.71 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
// imports
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
// Class Git
var Git = module.exports = function (options) {
this.binary = 'git';
if (typeof options == 'undefined')
options = {};
this.cwd = options.cwd || process.cwd();
delete options.cwd;
this.args = Git.optionsToString(options);
};
// git.exec(command [[, options], args ], callback)
Git.prototype.exec = function (command, options, args, callback) {
if (typeof options === 'undefined' || options == {}) {
options = '';
} else {
options = Git.optionsToString(options)
}
if (typeof args === 'undefined' || args == []) {
args = '';
} else {
args = args.join(' ');
}
if (typeof callback === 'undefined') {
callback = function (err, stdout) {};
}
var cmd = this.binary + ' ' + this.args + ' ' + command + ' ' + options + ' '
+ args;
var cwd = this.cwd;
return new Promise(function(fullfill, reject) {
exec(cmd, {
cwd: cwd
}, function (err, stdout, stderr) {
callback(err, stdout);
if (err) {
reject(err);
} else {
fullfill(stdout);
}
});
})
};
// converts an object that contains key value pairs to a argv string
Git.optionsToString = function (options) {
var args = [];
for (var k in options) {
var val = options[k];
if (k.length == 1) {
// val is true, add '-k'
if (val === true)
args.push('-'+k);
// if val is not false, add '-k val'
else if (val !== false)
args.push('-'+k+' '+val);
} else {
if (val === true)
args.push('--'+k);
else if (val !== false)
args.push('--'+k+'='+val);
}
}
return args.join(' ');
};