22
33module . exports = exports = search
44
5- var npm = require ( './npm.js' )
6- var allPackageSearch = require ( './search/all-package-search' )
7- var esearch = require ( './search/esearch.js' )
8- var formatPackageStream = require ( './search/format-package-stream.js' )
9- var usage = require ( './utils/usage' )
10- var output = require ( './utils/output.js' )
11- var log = require ( 'npmlog' )
12- var ms = require ( 'mississippi' )
5+ const npm = require ( './npm.js' )
6+ const allPackageSearch = require ( './search/all-package-search' )
7+ const figgyPudding = require ( 'figgy-pudding' )
8+ const formatPackageStream = require ( './search/format-package-stream.js' )
9+ const libSearch = require ( 'libnpmsearch' )
10+ const log = require ( 'npmlog' )
11+ const ms = require ( 'mississippi' )
12+ const npmConfig = require ( './config/figgy-config.js' )
13+ const output = require ( './utils/output.js' )
14+ const usage = require ( './utils/usage' )
1315
1416search . usage = usage (
1517 'search' ,
@@ -20,46 +22,50 @@ search.completion = function (opts, cb) {
2022 cb ( null , [ ] )
2123}
2224
25+ const SearchOpts = figgyPudding ( {
26+ description : { } ,
27+ exclude : { } ,
28+ include : { } ,
29+ limit : { } ,
30+ log : { } ,
31+ staleness : { } ,
32+ unicode : { }
33+ } )
34+
2335function search ( args , cb ) {
24- var searchOpts = {
36+ const opts = SearchOpts ( npmConfig ( ) ) . concat ( {
2537 description : npm . config . get ( 'description' ) ,
2638 exclude : prepareExcludes ( npm . config . get ( 'searchexclude' ) ) ,
2739 include : prepareIncludes ( args , npm . config . get ( 'searchopts' ) ) ,
28- limit : npm . config . get ( 'searchlimit' ) ,
40+ limit : npm . config . get ( 'searchlimit' ) || 20 ,
2941 log : log ,
3042 staleness : npm . config . get ( 'searchstaleness' ) ,
3143 unicode : npm . config . get ( 'unicode' )
32- }
33-
34- if ( searchOpts . include . length === 0 ) {
44+ } )
45+ if ( opts . include . length === 0 ) {
3546 return cb ( new Error ( 'search must be called with arguments' ) )
3647 }
3748
3849 // Used later to figure out whether we had any packages go out
39- var anyOutput = false
50+ let anyOutput = false
4051
41- var entriesStream = ms . through . obj ( )
52+ const entriesStream = ms . through . obj ( )
4253
43- var esearchWritten = false
44- esearch ( searchOpts ) . on ( 'data' , function ( pkg ) {
54+ let esearchWritten = false
55+ libSearch . stream ( opts . include , opts ) . on ( 'data' , pkg => {
4556 entriesStream . write ( pkg )
4657 ! esearchWritten && ( esearchWritten = true )
47- } ) . on ( 'error' , function ( e ) {
58+ } ) . on ( 'error' , err => {
4859 if ( esearchWritten ) {
4960 // If esearch errored after already starting output, we can't fall back.
50- return entriesStream . emit ( 'error' , e )
61+ return entriesStream . emit ( 'error' , err )
5162 }
5263 log . warn ( 'search' , 'fast search endpoint errored. Using old search.' )
53- allPackageSearch ( searchOpts ) . on ( 'data' , function ( pkg ) {
54- entriesStream . write ( pkg )
55- } ) . on ( 'error' , function ( e ) {
56- entriesStream . emit ( 'error' , e )
57- } ) . on ( 'end' , function ( ) {
58- entriesStream . end ( )
59- } )
60- } ) . on ( 'end' , function ( ) {
61- entriesStream . end ( )
62- } )
64+ allPackageSearch ( opts )
65+ . on ( 'data' , pkg => entriesStream . write ( pkg ) )
66+ . on ( 'error' , err => entriesStream . emit ( 'error' , err ) )
67+ . on ( 'end' , ( ) => entriesStream . end ( ) )
68+ } ) . on ( 'end' , ( ) => entriesStream . end ( ) )
6369
6470 // Grab a configured output stream that will spit out packages in the
6571 // desired format.
@@ -71,14 +77,14 @@ function search (args, cb) {
7177 parseable : npm . config . get ( 'parseable' ) ,
7278 color : npm . color
7379 } )
74- outputStream . on ( 'data' , function ( chunk ) {
80+ outputStream . on ( 'data' , chunk => {
7581 if ( ! anyOutput ) { anyOutput = true }
7682 output ( chunk . toString ( 'utf8' ) )
7783 } )
7884
7985 log . silly ( 'search' , 'searching packages' )
80- ms . pipe ( entriesStream , outputStream , function ( er ) {
81- if ( er ) return cb ( er )
86+ ms . pipe ( entriesStream , outputStream , err => {
87+ if ( err ) return cb ( err )
8288 if ( ! anyOutput && ! npm . config . get ( 'json' ) && ! npm . config . get ( 'parseable' ) ) {
8389 output ( 'No matches found for ' + ( args . map ( JSON . stringify ) . join ( ' ' ) ) )
8490 }
0 commit comments