check-marketplace-sorted.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env bun
  2. /**
  3. * Checks that marketplace.json plugins are alphabetically sorted by name.
  4. *
  5. * Usage:
  6. * bun check-marketplace-sorted.ts # check, exit 1 if unsorted
  7. * bun check-marketplace-sorted.ts --fix # sort in place
  8. */
  9. import { readFileSync, writeFileSync } from "fs";
  10. import { join } from "path";
  11. const MARKETPLACE = join(import.meta.dir, "../../.claude-plugin/marketplace.json");
  12. type Plugin = { name: string; [k: string]: unknown };
  13. type Marketplace = { plugins: Plugin[]; [k: string]: unknown };
  14. const raw = readFileSync(MARKETPLACE, "utf8");
  15. const mp: Marketplace = JSON.parse(raw);
  16. const cmp = (a: Plugin, b: Plugin) =>
  17. a.name.toLowerCase().localeCompare(b.name.toLowerCase());
  18. if (process.argv.includes("--fix")) {
  19. mp.plugins.sort(cmp);
  20. writeFileSync(MARKETPLACE, JSON.stringify(mp, null, 2) + "\n");
  21. console.log(`sorted ${mp.plugins.length} plugins`);
  22. process.exit(0);
  23. }
  24. for (let i = 1; i < mp.plugins.length; i++) {
  25. if (cmp(mp.plugins[i - 1], mp.plugins[i]) > 0) {
  26. console.error(
  27. `marketplace.json plugins are not sorted: ` +
  28. `'${mp.plugins[i - 1].name}' should come after '${mp.plugins[i].name}' (index ${i})`,
  29. );
  30. console.error(` run: bun .github/scripts/check-marketplace-sorted.ts --fix`);
  31. process.exit(1);
  32. }
  33. }
  34. console.log(`ok: ${mp.plugins.length} plugins sorted`);