This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrate-index.js
222 lines (187 loc) · 5.29 KB
/
crate-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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
var debug = require('debug')(__filename.slice(__dirname.length + 1));
var Promise = require('promise');
var _fs = require('graceful-fs'); // Need to parse many files at once
var fs = {
existsSync: _fs.existsSync,
readFile: Promise.denodeify(_fs.readFile),
writeFile: Promise.denodeify(_fs.writeFile),
};
var walk = require('walkdir');
var path = require('path');
var semver = require('semver');
var util = require('./crater-util');
var assert = require('assert');
var localIndexName = "crate-index"
var crateCacheName = "crate-cache"
var sourceCacheName = "source-cache"
/**
* Ensures the index is fresh and metadata for all crates is cached.
*/
function updateCaches(config) {
return cloneIndex(config);
}
/**
* Ensure that the crate-index repository is either present or created
*/
function cloneIndex(config) {
var indexAddr = config.crateIndexAddr;
var cacheDir = config.cacheDir;
var localIndex = path.join(cacheDir, localIndexName);
var p;
if (fs.existsSync(localIndex)) {
p = util.runCmd('git pull origin master', {cwd: localIndex});
} else {
p = util.runCmd('mkdir -p ' + localIndex);
p = util.runCmd('git clone ' + indexAddr + ' ' + localIndex);
}
p = p.then(function(x) {
debug('Repository exists');
});
return p;
}
/**
* Find all files in the repository which are not git files.
*/
function findFiles(directory) {
return new Promise(function(resolve, reject) {
var filesFound = [];
var dir = path.resolve(directory);
var dirLength = dir.length + 1; // Avoid the unnecessary .
var emitter = walk(directory);
// Store files
emitter.on('file', function(_filename, stat) {
var filename = path.resolve(_filename);
filename = filename.slice(dirLength);
// Ignore gitfiles and top level files (e.g. config.json)
if (!/^[.]git\//.test(filename) && filename.indexOf(path.sep) !== -1) {
filesFound.push(filename);
}
});
// Handle errors
emitter.on('error', function(err) {
reject(err);
});
// Fails are found files which could not be stat'd
emitter.on('fail', function(fail) {
debug('Failed to read a file! %s', fail);
});
// Resolve when all files are read
emitter.on('end', function() {
resolve(filesFound);
});
});
};
/**
* Read all versions of a given descriptor file and parse them into
* JSON
*/
function readFile(dir, filename) {
return fs.readFile(path.join(dir, filename), 'utf-8').then(function(filedata) {
var files = filedata.split('\n');
return files.filter(function(f) { return !!f }).map(function(f) {
return JSON.parse(f);
});
});
}
/**
* Load the crate index from the remote address.
*/
function loadCrates(config) {
return util.serial(function() {
var indexAddr = config.crateIndexAddr;
var cacheDir = config.cacheDir;
var localIndex = path.join(cacheDir, localIndexName);
var p = Promise.resolve();
p = p.then(function() {
debug('repos asserted');
return findFiles(localIndex)
});
p = p.then(function(filenames) {
debug('files found');
return Promise.all(filenames.map(function(filename) {
return readFile(localIndex, filename);
}));
});
p = p.then(function(res) {
debug('files read');
var flat = [];
res.forEach(function(r) {
Array.prototype.push.apply(flat, r);
});
return flat;
});
return p;
});
}
/**
* Given the resolved output from `loadCrates`, return a map from crate
* names to arrays of crate data.
*/
function getMostRecentRevs(crates) {
var map = {};
// maps in js have a default key called "constructor" and there's a crate
// called "constructor"...
map["constructor"] = null;
crates.forEach(function(c) {
if (map[c.name] == null) {
map[c.name] = c;
} else {
if (semver.lt(map[c.name].vers, c.vers)) {
map[c.name] = c;
}
}
});
return map;
}
/**
* Given the resolved output from `loadCrates`, return a map from crate
* names to arrays of dependencies, using data from the most recent crate revisions
* (so it is not perfectly accurate).
*/
function getDag(crates) {
var mostRecent = getMostRecentRevs(crates);
var map = { };
for (var k in mostRecent) {
var crate = mostRecent[k];
var deps = [];
crate.deps.forEach(function(dep) {
deps.push(dep.name);
});
map[crate.name] = deps;
}
return map;
}
/**
* Return a map from crate names to number of transitive downstream users.
*/
function getPopularityMap(crates) {
var users = { };
// Set users of every crate to 0
crates.forEach(function(crate) {
users[crate.name] = 0;
});
var dag = getDag(crates);
for (var crateName in dag) {
var depStack = dag[crateName];
while (depStack && depStack.length != 0) {
var nextDep = depStack.pop();
if (users[nextDep] == null) {
debug("dep " + nextDep + " is unknown. probably filtered out earlier");
users[nextDep] = 0;
}
assert(users[nextDep] != null);
users[nextDep] += 1;
if (dag[nextDep]) {
depStack.concat(dag[nextDep]);
}
}
}
return users;
}
exports.updateCaches = updateCaches;
exports.cloneIndex = cloneIndex;
exports.loadCrates = loadCrates;
exports.getMostRecentRevs = getMostRecentRevs;
exports.getDag = getDag;
exports.getPopularityMap = getPopularityMap;