refactor(cli): split tjwater cli modules
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { SCHEMA_VERSION } from "../core/constants.js";
|
||||
import { GROUP_SUMMARIES, commandDocs, hasVisibleSubcommands, isHiddenPath } from "./docs.js";
|
||||
import type { HelpPayload } from "../core/types.js";
|
||||
|
||||
export function helpPayload(pathParts: string[]): HelpPayload | null {
|
||||
const path = pathParts.join(" ");
|
||||
if (path && isHiddenPath(path)) return null;
|
||||
if (path && commandDocs.has(path)) return commandDocs.get(path)!;
|
||||
if (!path) {
|
||||
const seen = new Set<string>();
|
||||
const commands: Array<{ command: string; summary: string }> = [];
|
||||
for (const doc of [...commandDocs.values()].sort((a, b) => a.command.localeCompare(b.command))) {
|
||||
if (isHiddenPath(doc.command)) continue;
|
||||
const command = doc.command.split(" ")[0]!;
|
||||
if (seen.has(command)) continue;
|
||||
seen.add(command);
|
||||
commands.push({ command, summary: GROUP_SUMMARIES[command] ?? `${command} 可用子命令` });
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
schema_version: SCHEMA_VERSION,
|
||||
summary: "可用一级菜单",
|
||||
menu_level: 1,
|
||||
commands,
|
||||
};
|
||||
}
|
||||
const prefix = path ? `${path} ` : "";
|
||||
const children: Array<{ command: string; summary: string; usage: string; example: string }> = [];
|
||||
const seen = new Set<string>();
|
||||
for (const doc of [...commandDocs.values()].sort((a, b) => a.command.localeCompare(b.command))) {
|
||||
if (isHiddenPath(doc.command)) continue;
|
||||
if (!doc.command.startsWith(prefix)) continue;
|
||||
const child = doc.command.slice(prefix.length).split(" ")[0]!;
|
||||
if (seen.has(child)) continue;
|
||||
seen.add(child);
|
||||
const childPath = path ? `${path} ${child}` : child;
|
||||
const isGroup = hasVisibleSubcommands(childPath);
|
||||
const childDoc = commandDocs.get(childPath) ?? doc;
|
||||
children.push({
|
||||
command: childPath,
|
||||
summary: isGroup ? (GROUP_SUMMARIES[childPath] ?? `${childPath} 可用子命令`) : childDoc.summary,
|
||||
usage: isGroup ? `tjwater-cli ${childPath} help` : childDoc.usage,
|
||||
example: isGroup ? `tjwater-cli ${childPath} help` : childDoc.examples[0]!,
|
||||
});
|
||||
}
|
||||
if (!children.length && path) return null;
|
||||
return {
|
||||
ok: true,
|
||||
schema_version: SCHEMA_VERSION,
|
||||
summary: GROUP_SUMMARIES[path] ?? `${path} 可用子命令`,
|
||||
commands: children,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user