forked from jwcooper/Gitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (96 loc) · 2.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Gitty - index.js
* Author: Gordon Hall
*
* Initializes module and exposes public methods
*/
var Repository = require('./lib/repository');
var pty = require('pty.js');
/**
* Setup function for getting access to a GIT repo
* @constructor
* @param {String} path
*/
var Gitty = function(path) {
return new Repository(path);
};
/**
* Handles the global GIT configuration
* @param {String} key
* @param {String} val
* @param {Function} callback
*/
Gitty.setConfig = function(key, val, callback) {
var cmd = new Command('/', 'config', ['--global', key], '"' + val + '"');
var done = callback || new Function();
cmd.exec(function(err, stdout, stderr) {
done(err || null);
});
};
/**
* Handles the global GIT configuration
* @param {String} key
* @param {String} val
* @param {Function} callback
*/
Gitty.setConfigSync = function(key, val) {
var cmd = new Command('/', 'config', ['--global', key], '"' + val + '"');
return cmd.execSync();
};
/**
* Handles the global GIT configuration
* @param {String} key
* @param {Function} callback
*/
Gitty.getConfig = function(key, callback) {
var cmd = new Command('/', 'config', ['--global', key]);
var done = callback || new Function();
cmd.exec(function(err, stdout, stderr) {
done(err || null, stdout);
});
};
/**
* Handles the global GIT configuration
* @param {String} key
* @param {Function} callback
*/
Gitty.getConfigSync = function(key) {
var cmd = new Command('/', 'config', ['--global', key]);
return cmd.execSync();
};
/**
* Wrapper for the GIT clone function
* @param {String} path
* @param {String} url
* @param {Object} creds
* @param {Function} callback
*/
Gitty.clone = function(path, url) {
var self = this;
var args = Array.prototype.slice.apply(arguments);
var creds = args[2].username ? args[2] : {};
var done = args.slice(-1).pop() || new Function();
var pterm = pty.spawn('git', ['clone', url, path], { cwd : path });
var error = null;
pterm.on('data', function(data) {
var prompt = data.toLowerCase();
if (prompt.indexOf('username') > -1) {
return pterm.write(creds.username + '\r');
}
if (prompt.indexOf('password') > -1) {
return pterm.write(creds.password + '\r');
}
if ((prompt.indexOf('error') > -1) || (prompt.indexOf('fatal') > -1)) {
return error = prompt;
}
});
pterm.on('exit', function() {
done(error);
});
};
/**
* Export Contructor
* @constructor
* @type {Object}
*/
module.exports = Gitty;