Skip to content

Commit

Permalink
feat: Search command improvements (#728)
Browse files Browse the repository at this point in the history
* feat: added the page size option for search

* feat: search results with rating and installs info

* feat: added the publisher displayName to search

* fix: addressed the PR comments
  • Loading branch information
prashantvc authored May 18, 2022
1 parent 194d59b commit 3826665
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getLatestVersion } from './npm';
import { CancellationToken, log } from './util';
import * as semver from 'semver';
import { isatty } from 'tty';

const pkg = require('../package.json');

function fatal(message: any, ...args: any[]): void {
Expand Down Expand Up @@ -281,8 +282,10 @@ module.exports = function (argv: string[]): void {
program
.command('search <text>')
.option('--json', 'Output result in json format', false)
.option('--stats', 'Shows the extension rating and download counts', false)
.option('-p, --pagesize [value]', 'Number of results to return', '100')
.description('search extension gallery')
.action((text, { json }) => main(search(text, json)));
.action((text, { json, pagesize, stats }) => main(search(text, json, parseInt(pagesize), stats)));

program.on('command:*', ([cmd]: string) => {
if (cmd === 'create-publisher') {
Expand Down
84 changes: 60 additions & 24 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import {
ExtensionQueryFilterType,
ExtensionQueryFlags,
PublishedExtension,
ExtensionStatistic,
} from 'azure-devops-node-api/interfaces/GalleryInterfaces';
import { tableView, wordTrim } from './viewutils';

const pageSize = 100;
import { ratingStars, tableView, wordTrim } from './viewutils';
import { ExtensionStatiticsMap } from './show';
const installationTarget = 'Microsoft.VisualStudio.Code';
const excludeFlags = '37888'; //Value to exclude un-published, locked or hidden extensions

const baseResultsTableHeaders = ['<ExtensionId>', '<Publisher>', '<Name>'];

interface VSCodePublishedExtension extends PublishedExtension {
publisher: { displayName: string; publisherName: string };
}

export async function search(searchText: string, json: boolean = false): Promise<any> {
export async function search(
searchText: string,
json: boolean = false,
pageSize: number = 10,
stats: boolean = false
): Promise<any> {
const api = getPublicGalleryAPI();
const results = (await api.extensionQuery({
pageSize,
Expand All @@ -23,11 +29,25 @@ export async function search(searchText: string, json: boolean = false): Promise
{ filterType: ExtensionQueryFilterType.InstallationTarget, value: installationTarget },
{ filterType: ExtensionQueryFilterType.ExcludeWithFlags, value: excludeFlags },
],
flags: [ExtensionQueryFlags.ExcludeNonValidated, ExtensionQueryFlags.IncludeLatestVersionOnly],
flags: [
ExtensionQueryFlags.ExcludeNonValidated,
ExtensionQueryFlags.IncludeLatestVersionOnly,
stats ? ExtensionQueryFlags.IncludeStatistics : 0,
],
})) as VSCodePublishedExtension[];

if (json) {
console.log(JSON.stringify(results, undefined, '\t'));
if (stats || !json) {
console.log(
[
`Search results:`,
'',
...buildResultTableView(results, stats),
'',
'For more information on an extension use "vsce show <extensionId>"',
]
.map(line => wordTrim(line.replace(/\s+$/g, '')))
.join('\n')
);
return;
}

Expand All @@ -36,21 +56,37 @@ export async function search(searchText: string, json: boolean = false): Promise
return;
}

console.log(
[
`Search results:`,
'',
...tableView([
['<ExtensionId>', '<Description>'],
...results.map(({ publisher: { publisherName }, extensionName, shortDescription }) => [
publisherName + '.' + extensionName,
(shortDescription || '').replace(/\n|\r|\t/g, ' '),
]),
]),
'',
'For more information on an extension use "vsce show <extensionId>"',
]
.map(line => wordTrim(line.replace(/\s+$/g, '')))
.join('\n')
if (json) {
console.log(JSON.stringify(results, undefined, '\t'));
return;
}
}

function buildResultTableView(results: VSCodePublishedExtension[], stats: boolean): string[] {
const values = results.map(({ publisher, extensionName, displayName, shortDescription, statistics }) => [
publisher.publisherName + '.' + extensionName,
publisher.displayName,
wordTrim(displayName || '', 25),
stats ? buildExtensionStatisticsText(statistics!) : wordTrim(shortDescription || '', 150).replace(/\n|\r|\t/g, ' '),
]);

var resultsTableHeaders = stats
? [...baseResultsTableHeaders, '<Installs>', '<Rating>']
: [...baseResultsTableHeaders, '<Description>'];

const resultsTable = tableView([resultsTableHeaders, ...values]);

return resultsTable;
}

function buildExtensionStatisticsText(statistics: ExtensionStatistic[]): string {
const { install: installs = 0, averagerating = 0, ratingcount = 0 } = statistics?.reduce(
(map, { statisticName, value }) => ({ ...map, [statisticName!]: value }),
<ExtensionStatiticsMap>{}
);

return (
`${Number(installs).toLocaleString('en-US').padStart(12, ' ')} \t\t` +
` ${ratingStars(averagerating).padEnd(3, ' ')} (${ratingcount})`
);
}

0 comments on commit 3826665

Please sign in to comment.