2020-10-02 17:52:19 -04:00
|
|
|
const Pipeline = require('minipass-pipeline')
|
|
|
|
const libSearch = require('libnpmsearch')
|
2024-04-30 23:53:22 -07:00
|
|
|
const { log, output } = require('proc-log')
|
2022-05-11 17:17:18 +00:00
|
|
|
const formatSearchStream = require('../utils/format-search-stream.js')
|
2024-04-30 23:53:22 -07:00
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2022-05-11 17:17:18 +00:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Search extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Search for packages'
|
|
|
|
static name = 'search'
|
|
|
|
static params = [
|
|
|
|
'json',
|
|
|
|
'color',
|
|
|
|
'parseable',
|
|
|
|
'description',
|
2024-04-07 14:36:14 -07:00
|
|
|
'searchlimit',
|
2021-11-18 20:58:02 +00:00
|
|
|
'searchopts',
|
|
|
|
'searchexclude',
|
|
|
|
'registry',
|
|
|
|
'prefer-online',
|
|
|
|
'prefer-offline',
|
|
|
|
'offline',
|
|
|
|
]
|
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
static usage = ['<search term> [<search term> ...]']
|
2021-03-04 17:40:28 -05:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec (args) {
|
2021-03-04 17:40:28 -05:00
|
|
|
const opts = {
|
|
|
|
...this.npm.flatOptions,
|
|
|
|
...this.npm.flatOptions.search,
|
2024-04-30 23:53:22 -07:00
|
|
|
include: args.map(s => s.toLowerCase()).filter(Boolean),
|
2022-05-11 17:17:18 +00:00
|
|
|
exclude: this.npm.flatOptions.search.exclude.split(/\s+/),
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (opts.include.length === 0) {
|
2021-03-04 17:40:28 -05:00
|
|
|
throw new Error('search must be called with arguments')
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
|
|
|
|
// Used later to figure out whether we had any packages go out
|
|
|
|
let anyOutput = false
|
|
|
|
|
2023-11-16 09:12:25 -08:00
|
|
|
// Grab a configured output stream that will spit out packages in the desired format.
|
2024-04-30 23:53:22 -07:00
|
|
|
const outputStream = formatSearchStream({
|
2021-03-04 17:40:28 -05:00
|
|
|
args, // --searchinclude options are not highlighted
|
|
|
|
...opts,
|
2024-04-30 23:53:22 -07:00
|
|
|
npm: this.npm,
|
2024-02-29 07:28:15 -08:00
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
|
|
|
|
log.silly('search', 'searching packages')
|
|
|
|
const p = new Pipeline(
|
|
|
|
libSearch.stream(opts.include, opts),
|
|
|
|
outputStream
|
|
|
|
)
|
|
|
|
|
|
|
|
p.on('data', chunk => {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!anyOutput) {
|
2021-03-04 17:40:28 -05:00
|
|
|
anyOutput = true
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
output.standard(chunk.toString('utf8'))
|
2021-03-04 17:40:28 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
await p.promise()
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!anyOutput && !this.npm.config.get('json') && !this.npm.config.get('parseable')) {
|
2024-04-30 23:53:22 -07:00
|
|
|
output.standard('No matches found for ' + (args.map(JSON.stringify).join(' ')))
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
|
|
|
|
log.silly('search', 'search completed')
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Search
|