codekingpro/portable-devtools
114k
1'use strict'
2
3const npmFetch = require('npm-registry-fetch')
4
5module.exports = search
6function search (query, opts) {
7 return search.stream(query, opts).collect()
8}
9search.stream = searchStream
10function searchStream (query, opts = {}) {
11 opts = {
12 detailed: false,
13 limit: 20,
14 from: 0,
15 quality: 0.65,
16 popularity: 0.98,
17 maintenance: 0.5,
18 ...opts.opts, // this is to support the cli's --searchopts parameter
19 ...opts,
20 }
21
22 switch (opts.sortBy) {
23 case 'optimal': {
24 opts.quality = 0.65
25 opts.popularity = 0.98
26 opts.maintenance = 0.5
27 break
28 }
29 case 'quality': {
30 opts.quality = 1
31 opts.popularity = 0
32 opts.maintenance = 0
33 break
34 }
35 case 'popularity': {
36 opts.quality = 0
37 opts.popularity = 1
38 opts.maintenance = 0
39 break
40 }
41 case 'maintenance': {
42 opts.quality = 0
43 opts.popularity = 0
44 opts.maintenance = 1
45 break
46 }
47 }
48 return npmFetch.json.stream('/-/v1/search', 'objects.*',
49 {
50 ...opts,
51 query: {
52 text: Array.isArray(query) ? query.join(' ') : query,
53 size: opts.limit,
54 from: opts.from,
55 quality: opts.quality,
56 popularity: opts.popularity,
57 maintenance: opts.maintenance,
58 },
59 mapJSON: (obj) => {
60 if (obj.package.date) {
61 obj.package.date = new Date(obj.package.date)
62 }
63 if (opts.detailed) {
64 return obj
65 } else {
66 return obj.package
67 }
68 },
69 }
70 )
71}
72 