-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
81 lines (67 loc) · 3.08 KB
/
crawler.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
var request = require("request"),
zlib = require('zlib'),
fs = require('fs'),
async = require('async'),
Download = require('download'),
progress = require('download-status');
var url = "http://s3.amazonaws.com/extend.brackets/{0}/{0}-{1}.zip";
var STORAGE_PATH = 'storage';
var VERSION_REG = /^[0-9.-_a-zA-Z]*$/
request({method: 'GET', gzip: true, uri: 'https://s3.amazonaws.com/extend.brackets/registry.json'}, function (error, response, body) {
var packages = JSON.parse(body);
if (!fs.existsSync(STORAGE_PATH))
fs.mkdir(STORAGE_PATH);
fs.writeFileSync(STORAGE_PATH + '/registry.json', JSON.stringify(packages));
var size = Object.keys(packages).length, counter = 1;
async.eachSeries(Object.keys(packages), function(pack, next){
var curr = packages[pack];
async.waterfall([
function(callback){
console.log("==============================================================================");
console.log(" " + pack + " " + counter++ + "/" + size);
console.log("==============================================================================");
fs.mkdir(formater(STORAGE_PATH + '/{0}', pack), function (err){
callback();
});
},
function(callback){
var uri = [];
async.each(curr.versions, function (item, cb){
fs.exists(formater(STORAGE_PATH + '/{0}/{0}-{1}.zip', pack,item.version), function (exist){
if (!VERSION_REG.test(item.version))
console.log(item.version + " invalid version");
else if (!exist)
uri.push(formater(url, pack, item.version));
else
console.log(formater('{0}/{0}-{1}.zip', pack,item.version) + " exists");
cb();
});
}, function(err){
callback(null, uri);
});
},
function(uri ,callback){
if (uri.length > 0){
var down = new Download({}).dest(formater(STORAGE_PATH + '/{0}', pack)).use(progress());
uri.forEach(function (url){
down.get(url);
});
down.run(function (err, files, stream){
callback();
});
}
else
callback();
}], function (err, result){
next();
});
}, function(err){
});
});
function formater(str) {
// arguments[0] is the base string, so we need to adjust index values here
var args = [].slice.call(arguments, 1);
return str.replace(/\{(\d+)\}/g, function (match, num) {
return typeof args[num] !== "undefined" ? args[num] : match;
});
}