-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathread.ts
More file actions
92 lines (76 loc) · 2.48 KB
/
read.ts
File metadata and controls
92 lines (76 loc) · 2.48 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
/*
* 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} from '@oclif/core';
import {marked} from 'marked';
import {markedTerminal} from 'marked-terminal';
import {BaseCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {readDocByQuery, type DocEntry} from '@salesforce/b2c-tooling-sdk/operations/docs';
import {t} from '../../i18n/index.js';
interface ReadDocsResult {
entry: DocEntry;
content: string;
}
// Configure marked with terminal renderer
marked.use(
markedTerminal({
width: 80,
reflowText: true,
tableOptions: {
wordWrap: true,
colWidths: [35, 45],
},
}),
);
export default class DocsRead extends BaseCommand<typeof DocsRead> {
static args = {
query: Args.string({
description: 'Search query for documentation (class name, module path, or partial match)',
required: true,
}),
};
static description = t('commands.docs.read.description', 'Read Script API documentation for a class or module');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> ProductMgr',
'<%= config.bin %> <%= command.id %> dw.catalog.ProductMgr',
'<%= config.bin %> <%= command.id %> "dw system Status"',
'<%= config.bin %> <%= command.id %> ProductMgr --raw',
'<%= config.bin %> <%= command.id %> ProductMgr --json',
];
static flags = {
...BaseCommand.baseFlags,
raw: Flags.boolean({
char: 'r',
description: 'Output raw markdown without terminal formatting',
default: false,
}),
};
protected operations = {
readDocByQuery,
};
async run(): Promise<ReadDocsResult> {
const {query} = this.args;
const {raw} = this.flags;
const result = this.operations.readDocByQuery(query);
if (!result) {
this.error(t('commands.docs.read.notFound', 'No documentation found matching: {{query}}', {query}), {
suggestions: ['Try a broader search term', 'Use "b2c docs search <query>" to see available matches'],
});
}
if (this.jsonEnabled()) {
return result;
}
// Determine if we should render markdown for terminal
const useRaw = raw || !process.stdout.isTTY;
if (useRaw) {
process.stdout.write(result.content);
} else {
const rendered = marked.parse(result.content);
process.stdout.write(rendered as string);
}
return result;
}
}