
// Walk through the file-system "database" of installed
// packages, and create a data object related to the
// installed/active versions of each package.

var npm = require("../../npm")
  , fs = require("./graceful-fs")
  , path = require("path")
  , mkdir = require("./mkdir-p")
  , asyncMap = require("./async-map")
  , semver = require("./semver")

module.exports = readInstalled

function readInstalled (args, cb) {
  var showAll = args.length === 0
    , data = {}
  fs.readdir(npm.dir, function (er, packages) {
    if (er) return cb(er)
    packages = packages.filter(function (dir) {
      return (showAll || args.indexOf(dir) !== -1) && dir.charAt(0) !== "."
    })
    asyncMap(packages, function (package, cb) {
      data[package] = data[package] || {}
      var packageDir = path.join(npm.dir, package)
        , active = path.join(packageDir, "active")

      fs.lstat(active, function (er, s) {
        if (er || !s.isSymbolicLink()) return cb()
        fs.readlink(active, function (er, p) {
          if (er) return cb(er)
          var activeVersion = path.basename(p)
          data[package][activeVersion] = data[package][activeVersion] || {}
          data[package][activeVersion].active = true
          return cb()
        })
      })

      fs.readdir(packageDir, function (er, versions) {
        if (er) return cb() // skip over non-dirs or missing things.
        asyncMap(versions, function (version, cb) {
          if (!version) return cb()
          if (semver.valid(version)) {
            data[package][version] = data[package][version] || {}
          }
        }, cb)
      })

    // just return the data object we've created.
    }, function (er) { cb(er, data) })
  })
}

@@ -678,154 +678,186 @@
     
-asyncMap(packages, function (package, cb) %7B%0A      data%5Bpackage%5D = data%5Bpackage%5D %7C%7C %7B%7D%0A      var packageDir = path.join(npm.dir, package)%0A  
+var pkgDirs = %7B%7D%0A    packages.forEach(function (p) %7B%0A      pkgDirs%5Bp%5D = path.join(npm.dir, p)%0A      data%5Bp%5D = %7B%7D%0A    %7D)%0A%0A    asyncMap(packages, function (package, cb) %7B%0A
       
-,
+var
  act
@@ -872,26 +872,32 @@
 th.join(
+pkgDirs%5B
 package
-Dir
+%5D
 , %22activ
@@ -900,17 +900,16 @@
 ctive%22)%0A
-%0A
       fs
@@ -1286,24 +1286,55 @@
 %7D)%0A      %7D)%0A
+    %7D, function (package, cb) %7B
 %0A      fs.re
@@ -1343,18 +1343,24 @@
 dir(
+pkgDirs%5B
 package
-Dir
+%5D
 , fu
@@ -1390,32 +1390,75 @@
 %0A        if (er)
+ %7B%0A          delete data%5Bpackage%5D%0A         
  return cb() // 
@@ -1495,16 +1495,26 @@
 things.%0A
+        %7D%0A
         
@@ -1561,44 +1561,8 @@
 ) %7B%0A
-          if (!version) return cb()%0A
     
@@ -1666,32 +1666,47 @@
  %7B%7D%0A          %7D%0A
+          cb()%0A
         %7D, cb)%0A 
@@ -1709,25 +1709,49 @@
 b)%0A      %7D)%0A
-%0A
+    %7D, function (er) %7B%0A  
     // just 
@@ -1792,34 +1792,17 @@
 ed.%0A    
-%7D, function (er) %7B
+ 
  cb(er, 
@@ -1806,16 +1806,20 @@
 r, data)
+%0A   
  %7D)%0A  %7D)

Fix readInstalled regression from last asyncMap changes.