Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/issue-216-add-search-command #218

Merged
merged 1 commit into from Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main.ts
Expand Up @@ -2,6 +2,7 @@ import * as program from 'commander';
import { packageCommand, ls } from './package';
import { publish, list, unpublish } from './publish';
import { show } from './show';
import { search } from './search';
import { listPublishers, createPublisher, deletePublisher, loginPublisher, logoutPublisher } from './store';
import { getLatestVersion } from './npm';
import { CancellationToken, isCancelledError } from './util';
Expand Down Expand Up @@ -119,6 +120,12 @@ module.exports = function (argv: string[]): void {
.description('Show extension metadata')
.action((extensionid, { json }) => main(show(extensionid, json)));

program
.command('search <text>')
.option('--json', 'Output result in json format', false)
.description('search extension gallery')
.action((text, { json }) => main(search(text, json)));

program
.command('*')
.action(() => program.help());
Expand Down
45 changes: 45 additions & 0 deletions src/search.ts
@@ -0,0 +1,45 @@
import { getPublicGalleryAPI } from './util';
import { PublishedExtension, ExtensionQueryFilterType } from 'vso-node-api/interfaces/GalleryInterfaces';
import { tableView, wordTrim } from './viewutils';

const pageSize = 100;

export function search(searchText: string, json: boolean = false, pageNumber: number = 1): Promise<any> {
const flags = [];
return getPublicGalleryAPI()
.extensionQuery({
pageSize,
criteria: [
{ filterType: ExtensionQueryFilterType.SearchText, value: searchText },
],
flags,
})
.then(results => {
if (json) {
console.log(JSON.stringify(results, undefined, '\t'));
} else {
renderSearchView(searchText, results);
}
});
}

function renderSearchView(searchText: string, results: PublishedExtension[] = []) {
if (!results.length) {
console.log('No matching results');
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'));
}
11 changes: 10 additions & 1 deletion src/viewutils.ts
Expand Up @@ -7,6 +7,8 @@ const format = {
time: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
};

const columns = process.stdout.columns ? process.stdout.columns : 80;

export function formatDate(date) { return date.toLocaleString(fixedLocale, format.date); }
export function formatTime(date) { return date.toLocaleString(fixedLocale, format.time); }
export function formatDateTime(date) { return date.toLocaleString(fixedLocale, { ...format.date, ...format.time }); }
Expand All @@ -30,7 +32,7 @@ export function tableView(table: ViewTable, spacing: number = 2): string[] {
return table.map(row => row.map((cell, i) => `${cell}${repeatString(' ', maxLen[i] - cell.length + spacing)}`).join(''));
}

export function wordWrap(text: string, width: number = 80): string {
export function wordWrap(text: string, width: number = columns): string {
const [indent = ''] = text.match(/^\s+/) || [];
const maxWidth = width - indent.length;
return text
Expand All @@ -47,3 +49,10 @@ export function wordWrap(text: string, width: number = 80): string {
};

export function indentRow(row: string) { return ` ${row}`; };

export function wordTrim(text: string, width: number = columns, indicator = '...') {
if (text.length > width) {
return text.substr(0, width - indicator.length) + indicator;
}
return text;
}