2
2
3
3
module . exports = exports = search
4
4
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' )
13
15
14
16
search . usage = usage (
15
17
'search' ,
@@ -20,46 +22,50 @@ search.completion = function (opts, cb) {
20
22
cb ( null , [ ] )
21
23
}
22
24
25
+ const SearchOpts = figgyPudding ( {
26
+ description : { } ,
27
+ exclude : { } ,
28
+ include : { } ,
29
+ limit : { } ,
30
+ log : { } ,
31
+ staleness : { } ,
32
+ unicode : { }
33
+ } )
34
+
23
35
function search ( args , cb ) {
24
- var searchOpts = {
36
+ const opts = SearchOpts ( npmConfig ( ) ) . concat ( {
25
37
description : npm . config . get ( 'description' ) ,
26
38
exclude : prepareExcludes ( npm . config . get ( 'searchexclude' ) ) ,
27
39
include : prepareIncludes ( args , npm . config . get ( 'searchopts' ) ) ,
28
- limit : npm . config . get ( 'searchlimit' ) ,
40
+ limit : npm . config . get ( 'searchlimit' ) || 20 ,
29
41
log : log ,
30
42
staleness : npm . config . get ( 'searchstaleness' ) ,
31
43
unicode : npm . config . get ( 'unicode' )
32
- }
33
-
34
- if ( searchOpts . include . length === 0 ) {
44
+ } )
45
+ if ( opts . include . length === 0 ) {
35
46
return cb ( new Error ( 'search must be called with arguments' ) )
36
47
}
37
48
38
49
// Used later to figure out whether we had any packages go out
39
- var anyOutput = false
50
+ let anyOutput = false
40
51
41
- var entriesStream = ms . through . obj ( )
52
+ const entriesStream = ms . through . obj ( )
42
53
43
- var esearchWritten = false
44
- esearch ( searchOpts ) . on ( 'data' , function ( pkg ) {
54
+ let esearchWritten = false
55
+ libSearch . stream ( opts . include , opts ) . on ( 'data' , pkg => {
45
56
entriesStream . write ( pkg )
46
57
! esearchWritten && ( esearchWritten = true )
47
- } ) . on ( 'error' , function ( e ) {
58
+ } ) . on ( 'error' , err => {
48
59
if ( esearchWritten ) {
49
60
// If esearch errored after already starting output, we can't fall back.
50
- return entriesStream . emit ( 'error' , e )
61
+ return entriesStream . emit ( 'error' , err )
51
62
}
52
63
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 ( ) )
63
69
64
70
// Grab a configured output stream that will spit out packages in the
65
71
// desired format.
@@ -71,14 +77,14 @@ function search (args, cb) {
71
77
parseable : npm . config . get ( 'parseable' ) ,
72
78
color : npm . color
73
79
} )
74
- outputStream . on ( 'data' , function ( chunk ) {
80
+ outputStream . on ( 'data' , chunk => {
75
81
if ( ! anyOutput ) { anyOutput = true }
76
82
output ( chunk . toString ( 'utf8' ) )
77
83
} )
78
84
79
85
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 )
82
88
if ( ! anyOutput && ! npm . config . get ( 'json' ) && ! npm . config . get ( 'parseable' ) ) {
83
89
output ( 'No matches found for ' + ( args . map ( JSON . stringify ) . join ( ' ' ) ) )
84
90
}
0 commit comments