forked from jwcooper/Gitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (56 loc) · 1.42 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
/*
* Gitty - index.js
* Author: Gordon Hall
*
* Initializes module and exposes public methods
*/
var Repository = require('./classes/repository.js')
, Command = require('./classes/command.js')
, pty = require('pty.js')
, gitty;
gitty = function(path) {
return new Repository(path);
};
////
// config(key, val, callback)
// Does global Git configuration
////
function config(key, val, callback) {
var gitConfig = new Command(__dirname, 'config', ['--global', key], '"' + val + '"');
gitConfig.exec(function(error, stdout, stderr) {
var err = error || stderr;
if (callback) {
callback.call(this, err);
}
});
};
////
// clone(path, url, callback, creds)
// Clones the repository at url into the specified path
////
function clone(path, url, callback, creds) {
var pterm = pty.spawn('git', ['clone', url, path], { cwd : path })
, repo = this
, err
, succ;
pterm.on('data', function(data) {
var prompt = data.toLowerCase();
if (prompt.indexOf('username') > -1) {
pterm.write(creds.user + '\r');
} else if (prompt.indexOf('password') > -1) {
pterm.write(creds.pass + '\r');
} else if ((prompt.indexOf('error') > -1) || (prompt.indexOf('fatal') > -1)) {
err = prompt;
} else {
succ = prompt;
}
});
pterm.on('exit', function() {
callback.call(repo, err, succ);
});
};
gitty.config = config;
gitty.clone = clone;
gitty.Repository = Repository;
gitty.Command = Command;
module.exports = gitty;