search.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const Minipass = require('minipass')
  2. const Pipeline = require('minipass-pipeline')
  3. const libSearch = require('libnpmsearch')
  4. const log = require('npmlog')
  5. const formatPackageStream = require('./search/format-package-stream.js')
  6. const packageFilter = require('./search/package-filter.js')
  7. function prepareIncludes (args) {
  8. return args
  9. .map(s => s.toLowerCase())
  10. .filter(s => s)
  11. }
  12. function prepareExcludes (searchexclude) {
  13. var exclude
  14. if (typeof searchexclude === 'string')
  15. exclude = searchexclude.split(/\s+/)
  16. else
  17. exclude = []
  18. return exclude
  19. .map(s => s.toLowerCase())
  20. .filter(s => s)
  21. }
  22. const BaseCommand = require('./base-command.js')
  23. class Search extends BaseCommand {
  24. /* istanbul ignore next - see test/lib/load-all-commands.js */
  25. static get description () {
  26. return 'Search for packages'
  27. }
  28. /* istanbul ignore next - see test/lib/load-all-commands.js */
  29. static get name () {
  30. return 'search'
  31. }
  32. /* istanbul ignore next - see test/lib/load-all-commands.js */
  33. static get params () {
  34. return [
  35. 'long',
  36. 'json',
  37. 'color',
  38. 'parseable',
  39. 'description',
  40. 'searchopts',
  41. 'searchexclude',
  42. 'registry',
  43. 'prefer-online',
  44. 'prefer-offline',
  45. 'offline',
  46. ]
  47. }
  48. /* istanbul ignore next - see test/lib/load-all-commands.js */
  49. static get usage () {
  50. return ['[search terms ...]']
  51. }
  52. exec (args, cb) {
  53. this.search(args).then(() => cb()).catch(cb)
  54. }
  55. async search (args) {
  56. const opts = {
  57. ...this.npm.flatOptions,
  58. ...this.npm.flatOptions.search,
  59. include: prepareIncludes(args),
  60. exclude: prepareExcludes(this.npm.flatOptions.search.exclude),
  61. }
  62. if (opts.include.length === 0)
  63. throw new Error('search must be called with arguments')
  64. // Used later to figure out whether we had any packages go out
  65. let anyOutput = false
  66. class FilterStream extends Minipass {
  67. write (pkg) {
  68. if (packageFilter(pkg, opts.include, opts.exclude))
  69. super.write(pkg)
  70. }
  71. }
  72. const filterStream = new FilterStream()
  73. // Grab a configured output stream that will spit out packages in the
  74. // desired format.
  75. const outputStream = formatPackageStream({
  76. args, // --searchinclude options are not highlighted
  77. ...opts,
  78. })
  79. log.silly('search', 'searching packages')
  80. const p = new Pipeline(
  81. libSearch.stream(opts.include, opts),
  82. filterStream,
  83. outputStream
  84. )
  85. p.on('data', chunk => {
  86. if (!anyOutput)
  87. anyOutput = true
  88. this.npm.output(chunk.toString('utf8'))
  89. })
  90. await p.promise()
  91. if (!anyOutput && !this.npm.config.get('json') && !this.npm.config.get('parseable'))
  92. this.npm.output('No matches found for ' + (args.map(JSON.stringify).join(' ')))
  93. log.silly('search', 'search completed')
  94. log.clearProgress()
  95. }
  96. }
  97. module.exports = Search