-
Notifications
You must be signed in to change notification settings - Fork 20
/
what.common.js
78 lines (70 loc) · 2.08 KB
/
what.common.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
/**
* Prints packages most commonly used with a given one
*
* Usage:
* node what.common.js <package name>
*
* Example:
* node what.common.js react-dom
*/
var argv = require('minimist')(process.argv.slice(2));
var packageName = argv['_'][0];
if (!packageName) {
printUsage();
process.exit(101);
}
console.log('Counting packages most commonly used with ' + packageName + '...');
var inputFileName = './data/byField.in.graph';
var forEachPackage = require('./lib/forEachPackage.js');
var counts = new Map();
forEachPackage(inputFileName, countPackage, printResults);
function countPackage(data) {
var pkg = data.value;
countDependencies(pkg.dependencies, data.id);
countDependencies(pkg.devDependencies, data.id);
}
function countDependencies(dependencies, currentPackage) {
if (!dependencies) return;
var hasCurrentPackage = false;
var deps = new Set();
Object.keys(dependencies).forEach(function(dependencyName) {
if (dependencyName === packageName) {
hasCurrentPackage = true;
} else {
// add only those that do not equal to the current package
deps.add(dependencyName);
}
});
// Merge to global counter
if (hasCurrentPackage) {
deps.forEach(function(x) {
if (counts.has(x)) {
counts.set(x, counts.get(x) + 1);
} else {
counts.set(x, 1);
}
});
}
}
function printResults() {
var counter = [];
counts.forEach(function(v, key) {
counter.push({numberOfTimes: v, package: key});
});
counter = counter.sort(function(x, y) { return y.numberOfTimes - x.numberOfTimes; }).map(
function(x) {
return x.package + ' used ' + x.numberOfTimes + ' times';
}
);
console.log('Found ' + counter.length + ' packages most commonly used with ' + packageName + ':');
console.log(counter);
}
function printUsage() {
console.log('what.common.js - prints packages that most often appear with a given one');
console.log('');
console.log('Usage:');
console.log(' node what.common.js <package name>');
console.log('');
console.log('Example:');
console.log(' node what.common.js react-dom');
}