plugin-name.mjs 750 B

12345678910111213141516
  1. import { readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. /**
  4. * 读取插件根目录 .zcode-plugin/plugin.json 的 name。
  5. *
  6. * 原因:插件名决定 MCP 服务身份与 ui://<name>/ 资源地址。内置插件包目录名带 -plugin 后缀
  7. * (如 pdf-plugin),不能再从目录名推导,否则面板资源地址与 manifest 声明的 resourceUri 不一致。
  8. */
  9. export async function readPluginName(pluginRoot) {
  10. const manifestPath = join(pluginRoot, ".zcode-plugin", "plugin.json");
  11. const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
  12. if (typeof manifest.name !== "string" || manifest.name.length === 0)
  13. throw new Error(`${manifestPath}: 缺少插件 name`);
  14. return manifest.name;
  15. }