11'use strict'
22
3- module . exports = exports = search
4-
3+ const Minipass = require ( 'minipass' )
54const Pipeline = require ( 'minipass-pipeline' )
6-
7- const npm = require ( './npm.js' )
8- const formatPackageStream = require ( './search/format-package-stream.js' )
9-
105const libSearch = require ( 'libnpmsearch' )
116const log = require ( 'npmlog' )
7+
8+ const formatPackageStream = require ( './search/format-package-stream.js' )
9+ const packageFilter = require ( './search/package-filter.js' )
10+ const npm = require ( './npm.js' )
1211const output = require ( './utils/output.js' )
13- const usage = require ( './utils/usage' )
12+ const usageUtil = require ( './utils/usage.js' )
13+ const completion = require ( './utils/completion/none.js' )
1414
15- search . usage = usage (
15+ const usage = usageUtil (
1616 'search' ,
1717 'npm search [--long] [search terms ...]'
1818)
1919
20- search . completion = function ( opts , cb ) {
21- cb ( null , [ ] )
22- }
20+ const cmd = ( args , cb ) => search ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
2321
24- function search ( args , cb ) {
22+ const search = async ( args ) => {
2523 const opts = {
2624 ...npm . flatOptions ,
2725 ...npm . flatOptions . search ,
@@ -30,47 +28,52 @@ function search (args, cb) {
3028 }
3129
3230 if ( opts . include . length === 0 )
33- return cb ( new Error ( 'search must be called with arguments' ) )
31+ throw new Error ( 'search must be called with arguments' )
3432
3533 // Used later to figure out whether we had any packages go out
3634 let anyOutput = false
3735
36+ class FilterStream extends Minipass {
37+ write ( pkg ) {
38+ if ( packageFilter ( pkg , opts . include , opts . exclude ) )
39+ super . write ( pkg )
40+ }
41+ }
42+
43+ const filterStream = new FilterStream ( )
44+
3845 // Grab a configured output stream that will spit out packages in the
3946 // desired format.
40- //
41- // This is a text minipass stream
42- var outputStream = formatPackageStream ( {
47+ const outputStream = formatPackageStream ( {
4348 args, // --searchinclude options are not highlighted
4449 ...opts ,
4550 } )
4651
4752 log . silly ( 'search' , 'searching packages' )
48- const p = new Pipeline ( libSearch . stream ( opts . include , opts ) , outputStream )
53+ const p = new Pipeline (
54+ libSearch . stream ( opts . include , opts ) ,
55+ filterStream ,
56+ outputStream
57+ )
4958
5059 p . on ( 'data' , chunk => {
5160 if ( ! anyOutput )
5261 anyOutput = true
5362 output ( chunk . toString ( 'utf8' ) )
5463 } )
5564
56- p . promise ( ) . then ( ( ) => {
57- if ( ! anyOutput && ! opts . json && ! opts . parseable )
58- output ( 'No matches found for ' + ( args . map ( JSON . stringify ) . join ( ' ' ) ) )
65+ await p . promise ( )
66+ if ( ! anyOutput && ! opts . json && ! opts . parseable )
67+ output ( 'No matches found for ' + ( args . map ( JSON . stringify ) . join ( ' ' ) ) )
5968
60- log . silly ( 'search' , 'search completed' )
61- log . clearProgress ( )
62- cb ( null , { } )
63- } , err => cb ( err ) )
69+ log . silly ( 'search' , 'search completed' )
70+ log . clearProgress ( )
6471}
6572
6673function prepareIncludes ( args , searchopts ) {
67- if ( typeof searchopts !== 'string' )
68- searchopts = ''
69- return searchopts . split ( / \s + / ) . concat ( args ) . map ( function ( s ) {
70- return s . toLowerCase ( )
71- } ) . filter ( function ( s ) {
72- return s
73- } )
74+ return args
75+ . map ( s => s . toLowerCase ( ) )
76+ . filter ( s => s )
7477}
7578
7679function prepareExcludes ( searchexclude ) {
@@ -80,7 +83,9 @@ function prepareExcludes (searchexclude) {
8083 else
8184 exclude = [ ]
8285
83- return exclude . map ( function ( s ) {
84- return s . toLowerCase ( )
85- } )
86+ return exclude
87+ . map ( s => s . toLowerCase ( ) )
88+ . filter ( s => s )
8689}
90+
91+ module . exports = Object . assign ( cmd , { completion, usage } )
0 commit comments