-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
141 lines (121 loc) · 3.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Finds all dependencies save them into `outputFileName`
*/
var Crawler = require("crawler");
var outputFileName = 'composer_packages.json';
var fs = require('fs');
var semver = require('semver');
// How many packages did we process so far?
var total = 0;
// All packages are stored here:
var results = [];
// For some reason crawler fails with out of memory exception if you try load
// all 60k requests. Crawling it in chunks, where each chunk is 3000 packages.
// We will still do only 10 concurrent requests below. The chunk size is only
// used for queueing.
var chunkSize = 3000;
var getAllPackages = require('./lib/getAllPackages');
console.log('Loading list of all packages...');
getAllPackages().then(crawlDependencies);
return; // We are done here.
function crawlDependencies(index) {
console.log('Loaded ' + index.length + ' packages to query');
var c = new Crawler({
jQuery: false,
maxConnections: 10,
// This will be called for each crawled page
callback: indexPackage,
});
c.on('drain', saveAndExit);
queueChunk();
function queueChunk() {
if (!index.length) return;
if (index.length < chunkSize) {
console.log('Queueing last ' + index.length + ' packages');
c.queue(index.splice(0, index.length));
} else {
console.log('Queueing next ' + chunkSize + ' packages');
c.queue(index.splice(0, chunkSize));
}
}
function indexPackage(error, result, done) {
total += 1;
if (total % 100 === 0) {
console.log('Processed ' + total + ' packages');
}
if (total % chunkSize === 0) {
queueChunk();
}
var url = getUrl(result);
if (error) {
console.log('!! Error: ' + error + '; ' + url);
done();
return;
}
var body;
try {
body = JSON.parse(result.body);
} catch (e) {
console.log('Could not parse json response: ' + result.body + '; ' + url);
done();
return;
}
processBody(body);
done();
function processBody(body) {
if (body.package) {
processPackage(body.package);
} else if (body.packages) {
for (var name in body.packages) {
if (body.packages.hasOwnProperty(name)) {
processVersion(body.packages[name]);
}
}
} else {
console.log('Something is wrong with body. No package or packages for ' + JSON.stringify(body));
}
}
function processPackage(pkg) {
var versions = pkg.versions;
if (!versions) {
console.log(result);
console.log('Could not find versions for ' + url);
return;
}
processVersion(versions);
}
function processVersion(versions) {
var latest = versions['dev-master'] || findLatest(versions);
if (latest) results.push(latest);
else {
console.log(result, versions);
console.log('Could not find latest version for ' + url);
}
}
}
}
function getUrl(response) {
if (!response) return '';
return response.uri;
}
function saveAndExit() {
console.log('saving...');
fs.writeFileSync(outputFileName, JSON.stringify(results), 'utf8');
console.log('Done!');
console.log('Saved ' + results.length + ' packages into ' + outputFileName);
console.log('Now run layout algorithm using:');
console.log(' node layout.js');
process.exit(0);
}
function findLatest(versions) {
var mostRecent = Object.keys(versions).sort(bySemver);
return versions[mostRecent[0]];
}
function bySemver(x, y) {
var xValid = semver.valid(x);
var yValid = semver.valid(y);
if (xValid && yValid) return semver.rcompare(x, y);
if (xValid && !yValid) return 1;
if (yValid && !xValid) return 1;
return 0;
}