scaffold-files.mjs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /** 只生成已声明的组件;模板中的路径由插件 loader 在运行时解析。 */
  2. export function scaffoldFiles(name, components) {
  3. const manifest = {
  4. name,
  5. version: "0.1.0",
  6. description: `${name} plugin`,
  7. author: { name: "Local developer" },
  8. };
  9. const files = new Map();
  10. files.set(
  11. "README.md",
  12. `# ${name}\n\nDescribe this plugin's workflow and required configuration here.\n\nValidate before installation: \`zcode plugins validate .\`.\n`,
  13. );
  14. if (components.includes("skills")) {
  15. manifest.skills = "./skills";
  16. files.set(
  17. `skills/${name}/SKILL.md`,
  18. `---\nname: ${name}\ndescription: Guide the ${name} workflow when the user explicitly requests this plugin.\n---\n\n# ${name}\n\nClarify the requested outcome, inspect the available inputs, and agree on the concrete implementation before executing the workflow.\n`,
  19. );
  20. }
  21. if (components.includes("commands")) {
  22. manifest.commands = "./commands";
  23. files.set(
  24. "commands/help.md",
  25. `---\ndescription: Explain the ${name} plugin workflow\n---\n\nRead this plugin's README and explain its available capabilities.\n`,
  26. );
  27. }
  28. if (components.includes("hooks")) {
  29. manifest.hooks = "./hooks/hooks.json";
  30. files.set(
  31. "hooks/hooks.json",
  32. JSON.stringify(
  33. {
  34. hooks: {
  35. SessionStart: [
  36. {
  37. hooks: [
  38. {
  39. type: "command",
  40. command: 'node "${CLAUDE_PLUGIN_ROOT}/scripts/session-start.mjs"',
  41. },
  42. ],
  43. },
  44. ],
  45. },
  46. },
  47. null,
  48. 2,
  49. ) + "\n",
  50. );
  51. files.set(
  52. "scripts/session-start.mjs",
  53. "// 启动钩子默认无副作用;实现明确的插件行为后再输出上下文。\n",
  54. );
  55. }
  56. if (components.includes("mcp")) {
  57. manifest.mcpServers = "./.mcp.json";
  58. files.set(
  59. ".mcp.json",
  60. JSON.stringify(
  61. {
  62. mcpServers: {
  63. [name]: { command: "node", args: ["${CLAUDE_PLUGIN_ROOT}/scripts/mcp-server.mjs"] },
  64. },
  65. },
  66. null,
  67. 2,
  68. ) + "\n",
  69. );
  70. files.set(
  71. "scripts/mcp-server.mjs",
  72. `import { createInterface } from "node:readline";\n// 最小 MCP 握手骨架;只在实现真实工具后扩展 tools/list 和 tools/call。\nfor await (const line of createInterface({ input: process.stdin })) {\n const message = JSON.parse(line);\n if (message.id === undefined) continue;\n const result = message.method === "initialize"\n ? { protocolVersion: message.params.protocolVersion, capabilities: { tools: {} }, serverInfo: { name: ${JSON.stringify(name)}, version: "0.1.0" } }\n : message.method === "tools/list" ? { tools: [] } : message.method === "ping" ? {} : undefined;\n process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: message.id, ...(result ? { result } : { error: { code: -32601, message: "Method not found" } }) }) + "\\n");\n}\n`,
  73. );
  74. }
  75. for (const directory of ["scripts", "assets"])
  76. if (components.includes(directory)) files.set(`${directory}/.gitkeep`, "");
  77. files.set(".zcode-plugin/plugin.json", JSON.stringify(manifest, null, 2) + "\n");
  78. return files;
  79. }