Created
October 15, 2014 21:37
-
-
Save cgbystrom/3c6c50fdf4072e32817b to your computer and use it in GitHub Desktop.
Get a list of all NPM linked packages
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
var util = require('util'); | |
var exec = require('child_process').exec; | |
function getNpmLinkedPackages (callback) { | |
exec('npm list --global', function (error, stdout, stderr) { | |
if (error) return callback(error); | |
if (stderr.length > 0) return callback(stderr); | |
var pkgs = stdout | |
.split('\n') | |
.filter(function (line) { return line.split(' ')[0].length == 3; }) | |
.map(function (line) { var l = line.split(' '); return [l[1], l[3]]; }) | |
.reduce(function(pkgs, pkg, i) { | |
pkgs[pkg[0]] = pkg[1]; | |
return pkgs; | |
}, {}); | |
callback(undefined, pkgs); | |
}); | |
} | |
// Test | |
getNpmLinkedPackages(function (err, pkgs) { | |
if (err) return console.error(err); | |
console.log("Linked packages", pkgs); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment