• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var ms = require('mississippi')
2var allPackageMetadata = require('./all-package-metadata')
3var packageFilter = require('./package-filter.js')
4
5module.exports = allPackageSearch
6function allPackageSearch (opts) {
7  var searchSection = (opts.unicode ? '�� ' : '') + 'search'
8
9  // Get a stream with *all* the packages. This takes care of dealing
10  // with the local cache as well, but that's an internal detail.
11  var allEntriesStream = allPackageMetadata(opts)
12
13  // Grab a stream that filters those packages according to given params.
14  var filterStream = streamFilter(function (pkg) {
15    opts.log.gauge.pulse('search')
16    opts.log.gauge.show({section: searchSection, logline: 'scanning ' + pkg.name})
17    // Simply 'true' if the package matches search parameters.
18    var match = packageFilter(pkg, opts.include, opts.exclude, {
19      description: opts.description
20    })
21    return match
22  })
23  return ms.pipeline.obj(allEntriesStream, filterStream)
24}
25
26function streamFilter (filter) {
27  return ms.through.obj(function (data, enc, cb) {
28    if (filter(data)) {
29      this.push(standardizePkg(data))
30    }
31    cb()
32  })
33}
34
35function standardizePkg (data) {
36  return {
37    name: data.name,
38    description: data.description,
39    maintainers: (data.maintainers || []).map(function (m) {
40      return { username: m.name, email: m.email }
41    }),
42    keywords: data.keywords || [],
43    version: Object.keys(data.versions || {})[0] || [],
44    date: (
45      data.time &&
46      data.time.modified &&
47      new Date(data.time.modified)
48    ) || null
49  }
50}
51