create-basic-plugin.mjs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { dirname, join, resolve } from "node:path";
  3. import { pathToFileURL } from "node:url";
  4. import { parseArgs } from "node:util";
  5. import { scaffoldFiles } from "./scaffold-files.mjs";
  6. import {
  7. exists,
  8. rejectSymlink,
  9. atomicJson,
  10. marketplacePlan,
  11. withMarketplaceLock,
  12. } from "./marketplace-files.mjs";
  13. const COMPONENTS = ["skills", "commands", "mcp", "hooks", "scripts", "assets"];
  14. export function normalizePluginName(input) {
  15. if (/[\\/]/u.test(input)) throw new Error("Plugin name must not contain path separators");
  16. const name = input
  17. .trim()
  18. .toLowerCase()
  19. .replace(/[^a-z0-9]+/gu, "-")
  20. .replace(/^-|-$/gu, "");
  21. if (!name || name.length > 64)
  22. throw new Error("Plugin name must contain 1–64 ASCII letters/digits/hyphens");
  23. return name;
  24. }
  25. async function checkDestination(root, path, force) {
  26. let current = path;
  27. while (current !== dirname(root)) {
  28. await rejectSymlink(current);
  29. if (current === root) break;
  30. current = dirname(current);
  31. }
  32. if (!force && (await exists(path))) throw new Error(`File already exists: ${path}`);
  33. }
  34. export async function createPlugin({
  35. name: rawName,
  36. parentPath = resolve("plugins"),
  37. marketplacePath,
  38. components = [],
  39. force = false,
  40. }) {
  41. const name = normalizePluginName(rawName);
  42. if (components.some((component) => !COMPONENTS.includes(component)))
  43. throw new Error("Unsupported component");
  44. const root = resolve(parentPath, name);
  45. const marketPath = marketplacePath ? resolve(marketplacePath) : undefined;
  46. // 所有覆盖/路径检查先完成,避免在发现市场冲突前写出半份 scaffold。
  47. if (marketPath) await marketplacePlan(marketPath, root, name, force);
  48. await rejectSymlink(root);
  49. if (!force && (await exists(root))) throw new Error(`Plugin directory already exists: ${root}`);
  50. const files = scaffoldFiles(name, components);
  51. for (const file of files.keys()) await checkDestination(root, join(root, file), force);
  52. await mkdir(root, { recursive: true });
  53. for (const [file, contents] of files) {
  54. const destination = join(root, file);
  55. await mkdir(dirname(destination), { recursive: true });
  56. await writeFile(destination, contents, { flag: force ? "w" : "wx" });
  57. }
  58. if (marketPath)
  59. await withMarketplaceLock(marketPath, async () =>
  60. atomicJson(marketPath, await marketplacePlan(marketPath, root, name, force)),
  61. );
  62. return root;
  63. }
  64. async function main() {
  65. const { values, positionals } = parseArgs({
  66. allowPositionals: true,
  67. options: {
  68. path: { type: "string" },
  69. "marketplace-path": { type: "string" },
  70. force: { type: "boolean", default: false },
  71. ...Object.fromEntries(
  72. COMPONENTS.map((component) => [`with-${component}`, { type: "boolean", default: false }]),
  73. ),
  74. },
  75. });
  76. if (positionals.length !== 1)
  77. throw new Error(
  78. "Usage: node create-basic-plugin.mjs <name> [--path parent] [--with-skills] [--with-mcp] [--with-hooks] [--marketplace-path file] [--force]",
  79. );
  80. const root = await createPlugin({
  81. name: positionals[0],
  82. parentPath: values.path,
  83. marketplacePath: values["marketplace-path"],
  84. force: values.force,
  85. components: COMPONENTS.filter((component) => values[`with-${component}`]),
  86. });
  87. console.log(
  88. `Created ${root}. Customize the scaffold, then run zcode plugins validate before installation.`,
  89. );
  90. }
  91. if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href)
  92. main().catch((error) => {
  93. console.error(error.message);
  94. process.exitCode = 1;
  95. });