search.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict'
  2. module.exports = exports = search
  3. const npm = require('./npm.js')
  4. const allPackageSearch = require('./search/all-package-search')
  5. const figgyPudding = require('figgy-pudding')
  6. const formatPackageStream = require('./search/format-package-stream.js')
  7. const libSearch = require('libnpm/search')
  8. const log = require('npmlog')
  9. const ms = require('mississippi')
  10. const npmConfig = require('./config/figgy-config.js')
  11. const output = require('./utils/output.js')
  12. const usage = require('./utils/usage')
  13. search.usage = usage(
  14. 'search',
  15. 'npm search [--long] [search terms ...]'
  16. )
  17. search.completion = function (opts, cb) {
  18. cb(null, [])
  19. }
  20. const SearchOpts = figgyPudding({
  21. description: {},
  22. exclude: {},
  23. include: {},
  24. limit: {},
  25. log: {},
  26. staleness: {},
  27. unicode: {}
  28. })
  29. function search (args, cb) {
  30. const opts = SearchOpts(npmConfig()).concat({
  31. description: npm.config.get('description'),
  32. exclude: prepareExcludes(npm.config.get('searchexclude')),
  33. include: prepareIncludes(args, npm.config.get('searchopts')),
  34. limit: npm.config.get('searchlimit') || 20,
  35. log: log,
  36. staleness: npm.config.get('searchstaleness'),
  37. unicode: npm.config.get('unicode')
  38. })
  39. if (opts.include.length === 0) {
  40. return cb(new Error('search must be called with arguments'))
  41. }
  42. // Used later to figure out whether we had any packages go out
  43. let anyOutput = false
  44. const entriesStream = ms.through.obj()
  45. let esearchWritten = false
  46. libSearch.stream(opts.include, opts).on('data', pkg => {
  47. entriesStream.write(pkg)
  48. !esearchWritten && (esearchWritten = true)
  49. }).on('error', err => {
  50. if (esearchWritten) {
  51. // If esearch errored after already starting output, we can't fall back.
  52. return entriesStream.emit('error', err)
  53. }
  54. log.warn('search', 'fast search endpoint errored. Using old search.')
  55. allPackageSearch(opts)
  56. .on('data', pkg => entriesStream.write(pkg))
  57. .on('error', err => entriesStream.emit('error', err))
  58. .on('end', () => entriesStream.end())
  59. }).on('end', () => entriesStream.end())
  60. // Grab a configured output stream that will spit out packages in the
  61. // desired format.
  62. var outputStream = formatPackageStream({
  63. args: args, // --searchinclude options are not highlighted
  64. long: npm.config.get('long'),
  65. description: npm.config.get('description'),
  66. json: npm.config.get('json'),
  67. parseable: npm.config.get('parseable'),
  68. color: npm.color
  69. })
  70. outputStream.on('data', chunk => {
  71. if (!anyOutput) { anyOutput = true }
  72. output(chunk.toString('utf8'))
  73. })
  74. log.silly('search', 'searching packages')
  75. ms.pipe(entriesStream, outputStream, err => {
  76. if (err) return cb(err)
  77. if (!anyOutput && !npm.config.get('json') && !npm.config.get('parseable')) {
  78. output('No matches found for ' + (args.map(JSON.stringify).join(' ')))
  79. }
  80. log.silly('search', 'search completed')
  81. log.clearProgress()
  82. cb(null, {})
  83. })
  84. }
  85. function prepareIncludes (args, searchopts) {
  86. if (typeof searchopts !== 'string') searchopts = ''
  87. return searchopts.split(/\s+/).concat(args).map(function (s) {
  88. return s.toLowerCase()
  89. }).filter(function (s) { return s })
  90. }
  91. function prepareExcludes (searchexclude) {
  92. var exclude
  93. if (typeof searchexclude === 'string') {
  94. exclude = searchexclude.split(/\s+/)
  95. } else {
  96. exclude = []
  97. }
  98. return exclude.map(function (s) {
  99. return s.toLowerCase()
  100. })
  101. }