58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
import { API_DOC_AGENT_SCRIPTS, DEFAULT_API_DOC_AGENT_SCRIPT } from "./config/scripts.mjs";
|
|
|
|
function parseArgs(rawArgs) {
|
|
const args = [...rawArgs];
|
|
const scriptFlagIndex = args.findIndex((item) => item === "--script" || item === "-s");
|
|
let scriptName = DEFAULT_API_DOC_AGENT_SCRIPT;
|
|
|
|
if (scriptFlagIndex >= 0) {
|
|
const nextValue = args[scriptFlagIndex + 1];
|
|
if (!nextValue) {
|
|
throw new Error("`--script` 需要指定脚本名。");
|
|
}
|
|
scriptName = nextValue.trim();
|
|
args.splice(scriptFlagIndex, 2);
|
|
}
|
|
|
|
return {
|
|
scriptName,
|
|
scriptArgs: args,
|
|
};
|
|
}
|
|
|
|
function printAvailableScripts() {
|
|
const entries = Object.values(API_DOC_AGENT_SCRIPTS)
|
|
.sort((a, b) => a.name.localeCompare(b.name, "zh-CN"))
|
|
.map((item) => `- ${item.name}: ${item.description}`);
|
|
console.log("可用 API Agent 脚本:");
|
|
for (const line of entries) {
|
|
console.log(line);
|
|
}
|
|
}
|
|
|
|
async function run() {
|
|
const { scriptName, scriptArgs } = parseArgs(process.argv.slice(2));
|
|
if (scriptName === "list") {
|
|
printAvailableScripts();
|
|
return;
|
|
}
|
|
|
|
const scriptConfig = API_DOC_AGENT_SCRIPTS[scriptName];
|
|
if (!scriptConfig) {
|
|
printAvailableScripts();
|
|
throw new Error(`未找到脚本: ${scriptName}`);
|
|
}
|
|
|
|
const scriptModule = await import(scriptConfig.entry);
|
|
if (typeof scriptModule.runScript !== "function") {
|
|
throw new Error(`脚本 ${scriptName} 缺少 runScript 导出函数。`);
|
|
}
|
|
|
|
await scriptModule.runScript(scriptArgs);
|
|
}
|
|
|
|
run().catch((error) => {
|
|
console.error("执行 API Doc Agent 失败:", error.message);
|
|
process.exitCode = 1;
|
|
});
|