iter(g34): ua info 支持组件 id 查询并输出可读 props 表
This commit is contained in:
@@ -121,15 +121,47 @@ function cmdList() {
|
||||
}
|
||||
}
|
||||
|
||||
/*** 输出单包完整元数据 ***/
|
||||
function cmdInfo(name) {
|
||||
/*** 摊平组件清单(带所属包),供 info / MCP 按组件 id 查询 ***/
|
||||
function collectComponents(packages) {
|
||||
const list = packages ?? loadIndex().packages;
|
||||
return list.flatMap((pkg) => (pkg.docs?.components ?? []).map((comp) => ({ pkg, comp })));
|
||||
}
|
||||
|
||||
/*** 渲染组件的人类可读说明:props 表 + 步骤 + 示例,便于直接抄进代码 ***/
|
||||
function formatComponent(pkg, comp) {
|
||||
const lines = [`${comp.id} — ${comp.title ?? ''} (${pkg.name}${comp.demo ? `, demo: ${comp.demo}` : ''})`, ''];
|
||||
if ((comp.props ?? []).length) {
|
||||
lines.push('props:');
|
||||
const width = Math.max(...comp.props.map((x) => (x.n ?? '').length));
|
||||
for (const x of comp.props) lines.push(` ${(x.n ?? '').padEnd(width)} ${(x.t ?? '').padEnd(30)} ${x.r ? '必填 ' : ' '}${x.d ?? ''}`);
|
||||
lines.push('');
|
||||
}
|
||||
if ((comp.steps ?? []).length) {
|
||||
lines.push('steps:');
|
||||
comp.steps.forEach((t, i) => lines.push(` ${i + 1}. ${t}`));
|
||||
lines.push('');
|
||||
}
|
||||
if (comp.example) lines.push('example:', ` ${comp.example}`);
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/*** 输出单包或单组件信息:默认人读摘要,--json 输出原始 JSON ***/
|
||||
function cmdInfo(name, args = []) {
|
||||
const { packages } = loadIndex();
|
||||
const p = packages.find((x) => x.name === name || x.name === `@ui-arch/${name}`);
|
||||
if (!p) {
|
||||
console.error(`not found: ${name}`);
|
||||
const asJson = args.includes('--json');
|
||||
const pkg = packages.find((x) => x.name === name || x.name === `@ui-arch/${name}`);
|
||||
if (pkg) {
|
||||
if (asJson) return console.log(JSON.stringify(pkg, null, 2));
|
||||
return console.log(
|
||||
`${pkg.name} v${pkg.version} [${pkg.type}] ${pkg.description}\n(--json 看完整清单;按组件查:ua info <组件id>)`,
|
||||
);
|
||||
}
|
||||
const hit = collectComponents(packages).find((x) => x.comp.id === name);
|
||||
if (!hit) {
|
||||
console.error(`not found: ${name}(包名与组件 id 都没命中)`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(JSON.stringify(p, null, 2));
|
||||
console.log(asJson ? JSON.stringify({ package: hit.pkg.name, ...hit.comp }, null, 2) : formatComponent(hit.pkg, hit.comp));
|
||||
}
|
||||
|
||||
// ---- 静态文档站生成 ----
|
||||
@@ -407,7 +439,7 @@ function cmdMcp() {
|
||||
},
|
||||
{
|
||||
name: 'get_component',
|
||||
description: 'Get full metadata (API, usage, conventions) of one component',
|
||||
description: 'Get one package by name, or one component by its id (returns props/steps/example)',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: { name: { type: 'string', description: 'package name, e.g. @ui-arch/table-core or table-core' } },
|
||||
@@ -439,8 +471,10 @@ function cmdMcp() {
|
||||
}
|
||||
if (name === 'get_component') {
|
||||
const p = packages.find((x) => x.name === args.name || x.name === `@ui-arch/${args.name}`);
|
||||
if (!p) throw new Error(`not found: ${args.name}`);
|
||||
return p;
|
||||
if (p) return p;
|
||||
const hit = collectComponents(packages).find((x) => x.comp.id === args.name); // 退化到组件级查询
|
||||
if (!hit) throw new Error(`not found: ${args.name}`);
|
||||
return { package: hit.pkg.name, ...hit.comp };
|
||||
}
|
||||
if (name === 'search_components') {
|
||||
const q = String(args.query ?? '').toLowerCase();
|
||||
@@ -910,10 +944,10 @@ switch (cmd) {
|
||||
break;
|
||||
case 'info':
|
||||
if (!args[0]) {
|
||||
console.error('usage: ua info <package-name>');
|
||||
console.error('usage: ua info <包名|组件id> [--json]');
|
||||
process.exit(1);
|
||||
}
|
||||
cmdInfo(args[0]);
|
||||
cmdInfo(args[0], args.slice(1));
|
||||
break;
|
||||
case 'docs':
|
||||
cmdDocs();
|
||||
|
||||
Reference in New Issue
Block a user