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

ls.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib/ls.js
blob: a4a0b63f0a25aa08c1b7313e7104b44b254b55c8 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

// show the installed versions of a package

module.exports = exports = ls

var npm = require("../npm")
  , readInstalled = require("./utils/read-installed")
  , registry = require("./utils/registry")
  , semver = require("semver")
  , output = require("./utils/output")
  , log = require("./utils/log")

ls.usage = "npm ls [some search terms ...]"

ls.completion = function (args, index, cb) {
  var compl = []
    , getCompletions = require("./utils/completion/get-completions")
    , name = (args.length + 1 === index) && args[args.length - 1] || ""
    , priors = name ? args.slice(0, args.length - 1) : args
  // get the batch of data that matches so far.
  // this is an example of using npm.commands.ls programmatically
  // to fetch data that has been filtered by a set of arguments.
  ls(priors, true, 3600, function (er, data) {
    if (er) return cb(er)
    Object.keys(data).forEach(function (name) {
      compl.push.apply(compl, [name].concat(data[name].words))
    })
    var last = null
    compl = compl.sort(strcmp).filter(function (c) {
      if (args.indexOf(c) !== -1) return false
      var r = c !== last
      last = c
      return r
    })
    var matches = getCompletions(name || "", compl)
    if (name && !name.match(/^=/)) {
      matches = matches.concat(getCompletions("="+name, compl))
    }
    cb(null, matches)
  })
}

function ls (args, silent, staleness, cb_) {
  if (typeof cb_ !== "function") cb_ = staleness, staleness = 600
  if (typeof cb_ !== "function") cb_ = silent, silent = false
  var listopts = npm.config.get("listopts")
  if (typeof listopts !== "string") listopts = ""
  listopts = listopts.split(/\s+/)
  getFilteredData(staleness, listopts.concat(args), function (er, data) {
    // now data is the list of data that we want to show.
    // prettify and print it, and then provide the raw
    // data to the cb.
    if (er) return cb_(er)
    function cb (er) { return cb_(er, data) }
    if (silent) return cb()
    output.write(npm.config.get("outfd"), prettify(data, args), cb)
  })
}

function getFilteredData (staleness, args, cb) {
  var processedData = {}
  getMergedData(staleness, function (er, data) {
    if (er) return cb(er)
    Object.keys(data).sort(strcmp).forEach(function (p) {
      var pkg = data[p]
      if (pkg.url && (!pkg.versions || !Object.keys(pkg.versions).length)) {
        processedData[pkg.name] = getWords(pkg)
      } else {
        Object.keys(pkg.versions).sort(semver.compare).forEach(function (v) {
          pkg.name = p
          processedData[pkg.name+"@"+v] = getWords(pkg, v)
        })
      }
    })
    var filtered = {}
    Object.keys(processedData).forEach(function (name) {
      var d = processedData[name]
        , pass = true
        , test = [name].concat(npm.config.get("description")
                               ? d.description : [])
                       .concat(d.keywords)
                       .concat(d.words)
      for (var i = 0, l = args.length; i < l; i ++) {
        pass = false
        for (var ii = 0, ll = test.length; ii < ll; ii ++) {
          if (test[ii].indexOf(args[i]) !== -1) {
            pass = true
            break
          }
        }
        if (!pass) break
      }
      if (pass) filtered[name] = d
    })
    return cb(null, filtered)
  })
}
function getWords (pkg, version) {
  var d = { data : pkg
          , words : []
          , name : pkg.name + (version ? "@" + version : "")
          }
  if (pkg.maintainers && pkg.maintainers.length) {
    d.words.push.apply(d.words, pkg.maintainers.map(function (m) {
      return "=" + m.name
    }))
  }
  var desc = pkg.description
    , kw = pkg.keywords
  if (pkg.url && !version) {
    d.words.push("<"+pkg.url+">")
  } else {
    var v = pkg.versions[version]
    if (v.description) desc = v.description
    if (v.keywords) kw = v.keywords
    if (v.installed) d.words.push("installed")
    if (v.remote) d.words.push("remote")
    if (v.tags.length) d.words.push.apply(d.words, v.tags)
    if (v.active) d.words.push("active")
  }
  if (!kw) kw = []
  if (!Array.isArray(kw)) kw = kw.split(/\s+/)
  d.keywords = kw
  d.description = npm.config.get("description") && desc || ""
  return d
}
function getMergedData (staleness, cb) {
  getData(staleness, function (er, remote, installed) {
    if (er) return cb(er)
    return cb(null, merge(installed, remote))
  })
}
function getData (staleness, cb_) {
  var installed
    , remote
    , errState
  readInstalled([], function (er, inst) {
    if (er) return cb_(errState = er)
    installed = inst
    next()
  })
  registry.get("/", null, staleness, function (er, rem) {
    if (errState) return
    if (er) rem = {}
    remote = rem
    next()
  })
  function next () {
    if (!remote || !installed) return
    cb_(null, remote, installed)
  }
}
function strcmp (a, b) {
  a = a.toLowerCase()
  b = b.toLowerCase()
  return a === b ? 0 : a > b ? 1 : -1
}
function prettify (data, args) {
  var pkgs = Object.keys(data)
    , attrs = []
    , names = []
    , pretty = []
    , beginAttrList = 28
  pkgs.forEach(function (name) {
    var pkg = data[name]
    pretty.push({name:name,attrs:data[name].words.sort(strcmp).join(" ")
                ,description:data[name].description
                ,keywords:data[name].keywords})
  })
  var colors = [36, 32, 33, 31, 35 ]
    , c = 0
    , l = colors.length
  var maxNameLen = 0
  var space = "                       "
  pretty.forEach(function (line) {
    maxNameLen = Math.min(Math.max(maxNameLen, line.name.length), space.length)
  })
  maxNameLen += 2

  // turn each line obj into a single line, only as much ws as necessary.
  try {
    var stdio = process.binding("stdio")
      , cols = stdio.isatty(stdio.stdoutFD) ?
        ( stdio.getColumns ? stdio.getColumns()
        : stdio.getWindowSize ? stdio.getWindowSize()[1]
        : Infinity )
        : Infinity
  }
  catch (ex) { cols = Infinity }
  pretty = pretty.map(function (line) {
    var addSpace = maxNameLen - line.name.length
    return (line.name + (space.substr(0, addSpace) || "") + " "
           + line.attrs + "   "
           + (line.description ? line.description + "    " : "")
           + (line.keywords.length ? " " + line.keywords.join(" ") : ""))
           .substr(0, cols)
  })

  if (args && pretty.length) args.forEach(function (arg) {
    pretty = pretty.map(function (line) {
      return line.split(arg).join("\033["+colors[c]+"m" + arg + "\033[m")
    })
    c = (c + 1) % l
  })
  if (!pretty.length) pretty = ["Nothing found"]
  pretty.push("")
  return pretty.join("\n")
}
function merge (installed, remote) {
  var merged = {}
  // first, just copy the installed stuff
  for (var p in installed) {
    merged[p] = {versions:{}}
    for (var v in installed[p]) {
      merged[p].versions[v] = { installed : true, tags : [] }
      for (var s in installed[p][v]) {
        merged[p].versions[v][s] = installed[p][v][s]
      }
    }
  }
  // now merge in the remote stuff.
  for (var p in remote) {
    merged[p] = merged[p] || {versions:{}}
    for (var d in remote[p]) if (!merged[p].hasOwnProperty(d)) {
      merged[p][d] = remote[p][d]
    }
    for (var v in remote[p].versions) {
      merged[p].versions[v] = merged[p].versions[v] || {}
      merged[p].versions[v].remote = true
      merged[p].versions[v].tags = []
      var descs = remote[p].descriptions
      if (descs && descs[v] && descs[v] !== remote[p].description) {
        merged[p].versions[v].description = descs[v]
      }
      Object.keys(remote[p]["dist-tags"]).forEach(function (tag) {
        if (remote[p]["dist-tags"][tag] === v) {
          merged[p].versions[v].tags.push(tag)
        }
      })
    }
  }
  return merged
}