Created
March 15, 2014 01:23
-
-
Save deoxxa/9560443 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var _ = require("underscore"), | |
async = require("async"), | |
exec = require("child_process").exec, | |
ProgressBar = require("progress"), | |
treeify = require("treeify"); | |
var keep = []; | |
console.warn("Getting installed packages..."); | |
exec("brew list", function(err, installedPackages) { | |
if (err) { | |
return console.warn(err); | |
} | |
installedPackages = installedPackages.trim().split("\n").reduce(function(i, v) { | |
i[v] = { | |
name: v, | |
dependencies: [], | |
dependents: [], | |
}; | |
return i; | |
}, {}); | |
console.warn("Got %d installed packages, resolving dependencies...", _.size(installedPackages)); | |
var bar = new ProgressBar(":bar :percent, :eta seconds left", { | |
total: _.size(installedPackages), | |
width: 50, | |
}); | |
async.eachLimit(_.toArray(installedPackages), 5, function(installedPackage, done) { | |
exec("brew deps " + installedPackage.name, function(err, dependencies) { | |
bar.tick(); | |
if (err) { | |
return done(err); | |
} | |
dependencies = dependencies.trim().split("\n").filter(function(e) { | |
return !!e; | |
}); | |
dependencies.forEach(function(dependency) { | |
installedPackage.dependencies.push(dependency); | |
if (installedPackages[dependency]) { | |
installedPackages[dependency].dependents.push(installedPackage.name); | |
} | |
}); | |
return done(); | |
}); | |
}, function(err) { | |
if (err) { | |
return console.warn(err); | |
} | |
console.warn("Done, building dependency tree..."); | |
var bare = {}, | |
root = {}; | |
_.each(installedPackages, function(installedPackage) { | |
bare[installedPackage.name] = installedPackage.dependencies.reduce(function(i, v) { | |
i[v] = null; | |
return i; | |
}, {}); | |
}); | |
_.each(installedPackages, function(installedPackage) { | |
installedPackage.dependencies.forEach(function(k) { | |
bare[installedPackage.name][k] = bare[k]; | |
}); | |
}); | |
_.each(installedPackages, function(installedPackage) { | |
if (installedPackage.dependents.length === 0) { | |
root[installedPackage.name] = bare[installedPackage.name]; | |
} | |
}); | |
console.log(treeify.asTree(root)); | |
// populate the array `keep' with the names of packages that you've manually | |
// installed, then uncomment this line to get a list of packages that you | |
// can safely remove without breaking anything you care about. this is | |
// basically ghetto garbage collection with multiple user-initiated passes. | |
// console.log(_.difference(_.keys(root), keep).join(" ")); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment