@@ -3,18 +3,24 @@ import {
3
3
ExtensionQueryFilterType ,
4
4
ExtensionQueryFlags ,
5
5
PublishedExtension ,
6
+ ExtensionStatistic ,
6
7
} from 'azure-devops-node-api/interfaces/GalleryInterfaces' ;
7
- import { tableView , wordTrim } from './viewutils' ;
8
-
9
- const pageSize = 100 ;
8
+ import { ratingStars , tableView , wordTrim } from './viewutils' ;
9
+ import { ExtensionStatiticsMap } from './show' ;
10
10
const installationTarget = 'Microsoft.VisualStudio.Code' ;
11
11
const excludeFlags = '37888' ; //Value to exclude un-published, locked or hidden extensions
12
12
13
+ const baseResultsTableHeaders = [ '<ExtensionId>' , '<Publisher>' , '<Name>' ] ;
14
+
13
15
interface VSCodePublishedExtension extends PublishedExtension {
14
16
publisher : { displayName : string ; publisherName : string } ;
15
17
}
16
-
17
- export async function search ( searchText : string , json : boolean = false ) : Promise < any > {
18
+ export async function search (
19
+ searchText : string ,
20
+ json : boolean = false ,
21
+ pageSize : number = 10 ,
22
+ stats : boolean = false
23
+ ) : Promise < any > {
18
24
const api = getPublicGalleryAPI ( ) ;
19
25
const results = ( await api . extensionQuery ( {
20
26
pageSize,
@@ -23,11 +29,25 @@ export async function search(searchText: string, json: boolean = false): Promise
23
29
{ filterType : ExtensionQueryFilterType . InstallationTarget , value : installationTarget } ,
24
30
{ filterType : ExtensionQueryFilterType . ExcludeWithFlags , value : excludeFlags } ,
25
31
] ,
26
- flags : [ ExtensionQueryFlags . ExcludeNonValidated , ExtensionQueryFlags . IncludeLatestVersionOnly ] ,
32
+ flags : [
33
+ ExtensionQueryFlags . ExcludeNonValidated ,
34
+ ExtensionQueryFlags . IncludeLatestVersionOnly ,
35
+ stats ? ExtensionQueryFlags . IncludeStatistics : 0 ,
36
+ ] ,
27
37
} ) ) as VSCodePublishedExtension [ ] ;
28
38
29
- if ( json ) {
30
- console . log ( JSON . stringify ( results , undefined , '\t' ) ) ;
39
+ if ( stats || ! json ) {
40
+ console . log (
41
+ [
42
+ `Search results:` ,
43
+ '' ,
44
+ ...buildResultTableView ( results , stats ) ,
45
+ '' ,
46
+ 'For more information on an extension use "vsce show <extensionId>"' ,
47
+ ]
48
+ . map ( line => wordTrim ( line . replace ( / \s + $ / g, '' ) ) )
49
+ . join ( '\n' )
50
+ ) ;
31
51
return ;
32
52
}
33
53
@@ -36,21 +56,37 @@ export async function search(searchText: string, json: boolean = false): Promise
36
56
return ;
37
57
}
38
58
39
- console . log (
40
- [
41
- `Search results:` ,
42
- '' ,
43
- ...tableView ( [
44
- [ '<ExtensionId>' , '<Description>' ] ,
45
- ...results . map ( ( { publisher : { publisherName } , extensionName, shortDescription } ) => [
46
- publisherName + '.' + extensionName ,
47
- ( shortDescription || '' ) . replace ( / \n | \r | \t / g, ' ' ) ,
48
- ] ) ,
49
- ] ) ,
50
- '' ,
51
- 'For more information on an extension use "vsce show <extensionId>"' ,
52
- ]
53
- . map ( line => wordTrim ( line . replace ( / \s + $ / g, '' ) ) )
54
- . join ( '\n' )
59
+ if ( json ) {
60
+ console . log ( JSON . stringify ( results , undefined , '\t' ) ) ;
61
+ return ;
62
+ }
63
+ }
64
+
65
+ function buildResultTableView ( results : VSCodePublishedExtension [ ] , stats : boolean ) : string [ ] {
66
+ const values = results . map ( ( { publisher, extensionName, displayName, shortDescription, statistics } ) => [
67
+ publisher . publisherName + '.' + extensionName ,
68
+ publisher . displayName ,
69
+ wordTrim ( displayName || '' , 25 ) ,
70
+ stats ? buildExtensionStatisticsText ( statistics ! ) : wordTrim ( shortDescription || '' , 150 ) . replace ( / \n | \r | \t / g, ' ' ) ,
71
+ ] ) ;
72
+
73
+ var resultsTableHeaders = stats
74
+ ? [ ...baseResultsTableHeaders , '<Installs>' , '<Rating>' ]
75
+ : [ ...baseResultsTableHeaders , '<Description>' ] ;
76
+
77
+ const resultsTable = tableView ( [ resultsTableHeaders , ...values ] ) ;
78
+
79
+ return resultsTable ;
80
+ }
81
+
82
+ function buildExtensionStatisticsText ( statistics : ExtensionStatistic [ ] ) : string {
83
+ const { install : installs = 0 , averagerating = 0 , ratingcount = 0 } = statistics ?. reduce (
84
+ ( map , { statisticName, value } ) => ( { ...map , [ statisticName ! ] : value } ) ,
85
+ < ExtensionStatiticsMap > { }
86
+ ) ;
87
+
88
+ return (
89
+ `${ Number ( installs ) . toLocaleString ( 'en-US' ) . padStart ( 12 , ' ' ) } \t\t` +
90
+ ` ${ ratingStars ( averagerating ) . padEnd ( 3 , ' ' ) } (${ ratingcount } )`
55
91
) ;
56
92
}
0 commit comments