all-package-search.js 1.5 KB

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