-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbackup.js
More file actions
53 lines (47 loc) · 1.47 KB
/
backup.js
File metadata and controls
53 lines (47 loc) · 1.47 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
"use strict";
var Promise = require("bluebird"); // jshint ignore:line
var promisify = require("../lib/promisify");
var path = require("path");
var fs = Promise.promisifyAll(require("fs"));
function backupRepo(url, destinationDir) {
var re = new RegExp("git\\@github\\.com:([^/]+)/([^/]+)");
var matches = url.match(re);
var user = matches[1];
var repoName = matches[2];
var userPath = path.join(destinationDir, user);
var repoPath = path.join(userPath, repoName);
return fs.statAsync(userPath).error(function() {
return fs.mkdirAsync(userPath);
}).then(function() {
return fs.statAsync(repoPath);
}).then(function() {
console.log("Updating", url);
return promisify.exec("git", ["remote", "update"], { cwd: repoPath });
}, function() {
console.log("Cloning", url);
return promisify.exec("git", ["clone", "--mirror", url, repoPath]);
});
}
function backupRepoSerialized(url, destinationDir, promise) {
if (promise) {
return promise.then(function() {
return backupRepo(url, destinationDir);
});
} else {
return backupRepo(url, destinationDir);
}
}
var github = require("../lib/github");
function publicUserRepos(username, destinationDir) {
return github.getPublicUserRepos(username).then(function(repos) {
var promise;
for (var i = 0; i < repos.length; i++) {
var url = repos[i].ssh_url; // jshint ignore:line
promise = backupRepoSerialized(url, destinationDir, promise);
}
return promise;
});
}
module.exports = {
publicUserRepos: publicUserRepos
};