Welcome to mirror list, hosted at ThFree Co, Russian Federation.

read-installed.js « utils « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0be942ff7a9ad3423c8bf054ceac5287fb33b10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

// 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("fs")
  , path = require("path")
  , log = require("./log")
  , mkdir = require("./mkdir-p")

module.exports = readInstalled

function readInstalled (args, cb) {
  var showAll = args.length === 0
  mkdir(npm.tmp, function (er) {
    if (er) return cb(er)
    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) !== "."
      })
      // maybe nothing found
      if (!packages.length) cb(null, null)

      var p = packages.length
        , data = {}
      function listed () { if (--p === 0) cb(null, data) }
      packages.forEach(function (package) {
        var packageDir = path.join(npm.dir, package)
          , activeVersion = null
        data[package] = data[package] || {}
        fs.readdir(packageDir, function (er, versions) {
          if (er) return cb(er)
          ;(function V () {
            var version = versions.pop()
            if (activeVersion && data[package][activeVersion]) {
              data[package][activeVersion].active = true
            }
            if (!version) listed()
            if (version !== "active") {
              data[package][version] = data[package][version] || {}
              return process.nextTick(V)
            }
            var active = path.join(packageDir, "active")
            fs.lstat(active, function (er, s) {
              if (!s.isSymbolicLink()) {
                data[package][version] = data[package][version] || {}
                return process.nextTick(V)
              }
              fs.readlink(active, function (er, p) {
                if (er) return cb(er)
                activeVersion = path.basename(active)
                return process.nextTick(V)
              })
            })    
          })()
        })
      })
    })
  })
}