UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.search = void 0;
4const util_1 = require("./util");
5const GalleryInterfaces_1 = require("azure-devops-node-api/interfaces/GalleryInterfaces");
6const viewutils_1 = require("./viewutils");
7const installationTarget = 'Microsoft.VisualStudio.Code';
8const excludeFlags = '37888'; //Value to exclude un-published, locked or hidden extensions
9const baseResultsTableHeaders = ['<ExtensionId>', '<Publisher>', '<Name>'];
10async function search(searchText, json = false, pageSize = 10, stats = false) {
11 const api = (0, util_1.getPublicGalleryAPI)();
12 const results = (await api.extensionQuery({
13 pageSize,
14 criteria: [
15 { filterType: GalleryInterfaces_1.ExtensionQueryFilterType.SearchText, value: searchText },
16 { filterType: GalleryInterfaces_1.ExtensionQueryFilterType.InstallationTarget, value: installationTarget },
17 { filterType: GalleryInterfaces_1.ExtensionQueryFilterType.ExcludeWithFlags, value: excludeFlags },
18 ],
19 flags: [
20 GalleryInterfaces_1.ExtensionQueryFlags.ExcludeNonValidated,
21 GalleryInterfaces_1.ExtensionQueryFlags.IncludeLatestVersionOnly,
22 stats ? GalleryInterfaces_1.ExtensionQueryFlags.IncludeStatistics : 0,
23 ],
24 }));
25 if (stats || !json) {
26 console.log([
27 `Search results:`,
28 '',
29 ...buildResultTableView(results, stats),
30 '',
31 'For more information on an extension use "vsce show <extensionId>"',
32 ]
33 .map(line => (0, viewutils_1.wordTrim)(line.replace(/\s+$/g, '')))
34 .join('\n'));
35 return;
36 }
37 if (!results.length) {
38 console.log('No matching results');
39 return;
40 }
41 if (json) {
42 console.log(JSON.stringify(results, undefined, '\t'));
43 return;
44 }
45}
46exports.search = search;
47function buildResultTableView(results, stats) {
48 const values = results.map(({ publisher, extensionName, displayName, shortDescription, statistics }) => [
49 publisher.publisherName + '.' + extensionName,
50 publisher.displayName,
51 (0, viewutils_1.wordTrim)(displayName || '', 25),
52 stats ? buildExtensionStatisticsText(statistics) : (0, viewutils_1.wordTrim)(shortDescription || '', 150).replace(/\n|\r|\t/g, ' '),
53 ]);
54 var resultsTableHeaders = stats
55 ? [...baseResultsTableHeaders, '<Installs>', '<Rating>']
56 : [...baseResultsTableHeaders, '<Description>'];
57 const resultsTable = (0, viewutils_1.tableView)([resultsTableHeaders, ...values]);
58 return resultsTable;
59}
60function buildExtensionStatisticsText(statistics) {
61 const { install: installs = 0, averagerating = 0, ratingcount = 0 } = statistics?.reduce((map, { statisticName, value }) => ({ ...map, [statisticName]: value }), {});
62 return (`${Number(installs).toLocaleString('en-US').padStart(12, ' ')} \t\t` +
63 ` ${(0, viewutils_1.ratingStars)(averagerating).padEnd(3, ' ')} (${ratingcount})`);
64}
65//# sourceMappingURL=search.js.map
\No newline at end of file