installed-deep.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module.exports = installedDeep
  2. var npm = require('../../npm.js')
  3. var readInstalled = require('read-installed')
  4. function installedDeep (opts, cb) {
  5. var local
  6. var global
  7. var depth = npm.config.get('depth')
  8. var opt = { depth: depth, dev: true }
  9. if (npm.config.get('global')) {
  10. local = []
  11. next()
  12. } else {
  13. readInstalled(npm.prefix, opt, function (er, data) {
  14. local = getNames(data || {})
  15. next()
  16. })
  17. }
  18. readInstalled(npm.config.get('prefix'), opt, function (er, data) {
  19. global = getNames(data || {})
  20. next()
  21. })
  22. function getNames_ (d, n) {
  23. if (d.realName && n) {
  24. if (n[d.realName]) return n
  25. n[d.realName] = true
  26. }
  27. if (!n) n = {}
  28. Object.keys(d.dependencies || {}).forEach(function (dep) {
  29. getNames_(d.dependencies[dep], n)
  30. })
  31. return n
  32. }
  33. function getNames (d) {
  34. return Object.keys(getNames_(d))
  35. }
  36. function next () {
  37. if (!local || !global) return
  38. if (!npm.config.get('global')) {
  39. global = global.map(function (g) {
  40. return [g, '-g']
  41. })
  42. }
  43. var names = local.concat(global)
  44. return cb(null, names)
  45. }
  46. }