Skip to content

Instantly share code, notes, and snippets.

@xavi-
Last active December 27, 2015 07:09
Show Gist options
  • Save xavi-/7286510 to your computer and use it in GitHub Desktop.
Save xavi-/7286510 to your computer and use it in GitHub Desktop.
// First do this:
// $ curl http://isaacs.iriscouch.com/registry/_all_docs > npmdocs.json
var docs = require("./npmdocs").rows;
var request = require("request");
var fs = require("fs");
var normalized = {};
docs.forEach(function(doc) {
var lower = doc.id.toLowerCase();
(normalized[lower] || (normalized[lower] = [])).push(doc.id);
});
var collisions = [];
for(var key in normalized) {
if(normalized[key].length > 1) {
collisions.push(normalized[key]);
}
}
fs.writeFile("./collisions.json", prettyJson(collisions), function(err) {
if(err) { throw err; }
console.log("Wrote collisions.json");
});
var results = [], pendingCols = collisions.length;
collisions.forEach(function(modules) {
var pendingMods = modules.length, modAuthors = [];
modules.forEach(function(module) {
request(
"http://isaacs.iriscouch.com/registry/" + module,
function(err, res, body) {
if(err) { throw err; }
body = JSON.parse(body);
modAuthors.push({
module: module,
author: body.author || body.maintainers[0]
});
pendingMods -= 1;
if(pendingMods <= 0) {
results.push(modAuthors);
pendingCols -= 1;
if(pendingCols <= 0) { separateCols(results); }
}
}
);
});
});
function prettyJson(obj) {
return JSON.stringify(obj, null, "\t");
}
function separateCols(collisions) {
var pseudoCols = [], realCols = [];
collisions.forEach(function(modules) {
var author = modules[0].author;
var haveSameAuthor = modules.every(function(mod) {
return (author.email && author.email === mod.author.email) ||
(author.name && author.name === mod.author.name);
});
if(haveSameAuthor) { pseudoCols.push(modules); }
else { realCols.push(modules); }
});
fs.writeFile("./real-collisions.json", prettyJson(realCols), function(err) {
if(err) { throw err; }
console.log("Wrote real-collisions.json");
});
fs.writeFile("./pseudo-collisions.json", prettyJson(pseudoCols), function(err) {
if(err) { throw err; }
console.log("Wrote pseudo-collisions.json");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment