installed-shallow.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. module.exports = installedShallow
  2. var npm = require('../../npm.js')
  3. var fs = require('graceful-fs')
  4. var path = require('path')
  5. var readJson = require('read-package-json')
  6. var asyncMap = require('slide').asyncMap
  7. function installedShallow (opts, filter, cb) {
  8. if (typeof cb !== 'function') {
  9. cb = filter
  10. filter = null
  11. }
  12. var conf = opts.conf
  13. var args = conf.argv.remain
  14. if (args.length > 3) return cb()
  15. var local
  16. var global
  17. var localDir = npm.dir
  18. var globalDir = npm.globalDir
  19. if (npm.config.get('global')) {
  20. local = []
  21. next()
  22. } else {
  23. fs.readdir(localDir, function (er, pkgs) {
  24. local = (pkgs || []).filter(function (p) {
  25. return p.charAt(0) !== '.'
  26. })
  27. next()
  28. })
  29. }
  30. fs.readdir(globalDir, function (er, pkgs) {
  31. global = (pkgs || []).filter(function (p) {
  32. return p.charAt(0) !== '.'
  33. })
  34. next()
  35. })
  36. function next () {
  37. if (!local || !global) return
  38. filterInstalled(local, global, filter, cb)
  39. }
  40. }
  41. function filterInstalled (local, global, filter, cb) {
  42. var fl
  43. var fg
  44. if (!filter) {
  45. fl = local
  46. fg = global
  47. return next()
  48. }
  49. asyncMap(local, function (p, cb) {
  50. readJson(path.join(npm.dir, p, 'package.json'), function (er, d) {
  51. if (!d || !filter(d)) return cb(null, [])
  52. return cb(null, d.name)
  53. })
  54. }, function (er, local) {
  55. fl = local || []
  56. next()
  57. })
  58. var globalDir = npm.globalDir
  59. asyncMap(global, function (p, cb) {
  60. readJson(path.join(globalDir, p, 'package.json'), function (er, d) {
  61. if (!d || !filter(d)) return cb(null, [])
  62. return cb(null, d.name)
  63. })
  64. }, function (er, global) {
  65. fg = global || []
  66. next()
  67. })
  68. function next () {
  69. if (!fg || !fl) return
  70. if (!npm.config.get('global')) {
  71. fg = fg.map(function (g) {
  72. return [g, '-g']
  73. })
  74. }
  75. console.error('filtered', fl, fg)
  76. return cb(null, fl.concat(fg))
  77. }
  78. }