-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearch.ts
More file actions
144 lines (118 loc) · 3.61 KB
/
search.ts
File metadata and controls
144 lines (118 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Args, Flags, ux} from '@oclif/core';
import {BaseCommand, createTable, type ColumnDef} from '@salesforce/b2c-tooling-sdk/cli';
import {searchDocs, listDocs, type SearchResult, type DocEntry} from '@salesforce/b2c-tooling-sdk/operations/docs';
import {t} from '../../i18n/index.js';
interface SearchDocsResponse {
query?: string;
results: SearchResult[];
}
interface ListDocsResponse {
entries: DocEntry[];
}
const COLUMNS: Record<string, ColumnDef<SearchResult>> = {
id: {
header: 'ID',
get: (r) => r.entry.id,
},
title: {
header: 'Title',
get: (r) => r.entry.title,
},
score: {
header: 'Match',
get(r) {
// Convert score to percentage (lower is better in Fuse.js)
const percent = Math.round((1 - r.score) * 100);
return `${percent}%`;
},
},
};
const DEFAULT_COLUMNS = ['id', 'title', 'score'];
export default class DocsSearch extends BaseCommand<typeof DocsSearch> {
static args = {
query: Args.string({
description: 'Search query (fuzzy match against class/module names)',
required: false,
}),
};
static description = t('commands.docs.search.description', 'Search Script API documentation');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> ProductMgr',
'<%= config.bin %> <%= command.id %> "catalog product"',
'<%= config.bin %> <%= command.id %> status --limit 5',
'<%= config.bin %> <%= command.id %> --list',
];
static flags = {
...BaseCommand.baseFlags,
limit: Flags.integer({
char: 'l',
description: 'Maximum number of results to display',
default: 20,
}),
list: Flags.boolean({
description: 'List all available documentation entries',
default: false,
}),
};
protected operations = {
listDocs,
searchDocs,
};
async run(): Promise<ListDocsResponse | SearchDocsResponse> {
const {query} = this.args;
const {limit, list} = this.flags;
// List mode
if (list) {
const entries = this.operations.listDocs();
if (this.jsonEnabled()) {
return {entries};
}
// Convert to search results for table rendering
const results: SearchResult[] = entries.map((entry) => ({
entry,
score: 0,
}));
const listColumns: Record<string, ColumnDef<SearchResult>> = {
id: COLUMNS.id,
title: COLUMNS.title,
};
createTable(listColumns).render(results, ['id', 'title']);
this.log(
t('commands.docs.search.totalCount', '{{count}} documentation entries available', {count: entries.length}),
);
return {entries};
}
// Search mode requires query
if (!query) {
this.error(
t('commands.docs.search.queryRequired', 'Query is required for search. Use --list to see all entries.'),
);
}
const results = this.operations.searchDocs(query, limit);
const response: SearchDocsResponse = {
query,
results,
};
if (this.jsonEnabled()) {
return response;
}
if (results.length === 0) {
ux.stdout(t('commands.docs.search.noResults', 'No documentation found matching: {{query}}', {query}));
return response;
}
createTable(COLUMNS).render(results, DEFAULT_COLUMNS);
this.log(
t('commands.docs.search.resultCount', 'Found {{count}} matches for "{{query}}"', {
count: results.length,
query,
}),
);
return response;
}
}