1
1
'use strict'
2
2
3
- module . exports = exports = search
4
-
3
+ const Minipass = require ( 'minipass' )
5
4
const Pipeline = require ( 'minipass-pipeline' )
6
-
7
- const npm = require ( './npm.js' )
8
- const formatPackageStream = require ( './search/format-package-stream.js' )
9
-
10
5
const libSearch = require ( 'libnpmsearch' )
11
6
const 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' )
12
11
const 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' )
14
14
15
- search . usage = usage (
15
+ const usage = usageUtil (
16
16
'search' ,
17
17
'npm search [--long] [search terms ...]'
18
18
)
19
19
20
- search . completion = function ( opts , cb ) {
21
- cb ( null , [ ] )
22
- }
20
+ const cmd = ( args , cb ) => search ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
23
21
24
- function search ( args , cb ) {
22
+ const search = async ( args ) => {
25
23
const opts = {
26
24
...npm . flatOptions ,
27
25
...npm . flatOptions . search ,
@@ -30,47 +28,52 @@ function search (args, cb) {
30
28
}
31
29
32
30
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' )
34
32
35
33
// Used later to figure out whether we had any packages go out
36
34
let anyOutput = false
37
35
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
+
38
45
// Grab a configured output stream that will spit out packages in the
39
46
// desired format.
40
- //
41
- // This is a text minipass stream
42
- var outputStream = formatPackageStream ( {
47
+ const outputStream = formatPackageStream ( {
43
48
args, // --searchinclude options are not highlighted
44
49
...opts ,
45
50
} )
46
51
47
52
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
+ )
49
58
50
59
p . on ( 'data' , chunk => {
51
60
if ( ! anyOutput )
52
61
anyOutput = true
53
62
output ( chunk . toString ( 'utf8' ) )
54
63
} )
55
64
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 ( ' ' ) ) )
59
68
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 ( )
64
71
}
65
72
66
73
function 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 )
74
77
}
75
78
76
79
function prepareExcludes ( searchexclude ) {
@@ -80,7 +83,9 @@ function prepareExcludes (searchexclude) {
80
83
else
81
84
exclude = [ ]
82
85
83
- return exclude . map ( function ( s ) {
84
- return s . toLowerCase ( )
85
- } )
86
+ return exclude
87
+ . map ( s => s . toLowerCase ( ) )
88
+ . filter ( s => s )
86
89
}
90
+
91
+ module . exports = Object . assign ( cmd , { completion, usage } )
0 commit comments