-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
67 lines (57 loc) · 1.65 KB
/
fetch.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
var path = require('path'),
fs = require('fs'),
spawn = require('child_process').spawn;
exports.detect = function (uri) {
if (uri.match(/^(git@|(git|https):\/\/)/)) {
parts = uri.match(/^(.+?)(?:#([0-9a-f]+))?$/i);
return {
method: 'git',
basename: path.basename(parts[1].replace(':', path.sep), '.git'),
uri: parts[1],
commit: parts[2]
};
} else if (uri.match(/^\/.+\.zip$/i)) {
return {
method: 'unzip',
basename: path.basename(uri, '.zip'),
uri: uri
};
} else {
console.log('I have no idea how to grab ' + uri);
return null;
}
}
exports.fetchIntoStore = function (info, store, callback) {
if (!info.basename) info = exports.detect(info);
var target = path.join(store, info.basename);
exports.fetchInto(info, target, function (success) {
callback(success, info.basename);
});
}
exports.fetchInto = function (info, target, callback) {
if (!info.method) info = exports.detect(info);
console.log('Fetching', info);
switch (info.method) {
case 'git':
spawn('git', ['clone', info.uri, target]).on('exit', function () {
if (!info.commit) return callback(true);
var args = ['checkout', '-f', info.commit];
spawn('git', args, {cwd: target}).on('exit', function () {
callback(true);
});
});
break;
case 'unzip':
fs.mkdir(target, function () {
var args = [info.uri, '-d', target];
spawn('unzip', args).on('exit', function () {
callback(true);
});
});
break;
default:
console.log('I have no idea how to grab ' + uri);
callback(false);
return false;
}
}