generate-clients.mjs.njk 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env zx
  2. import "zx/globals";
  3. import * as k from "kinobi";
  4. import { rootNodeFromAnchor } from "@kinobi-so/nodes-from-anchor";
  5. {% if jsClient %}
  6. import { renderVisitor as renderJavaScriptVisitor } from "@kinobi-so/renderers-js";
  7. {% endif %}
  8. {% if rustClient %}
  9. import { renderVisitor as renderRustVisitor } from "@kinobi-so/renderers-rust";
  10. {% endif %}
  11. import { getAllProgramIdls } from "./utils.mjs";
  12. // Instanciate Kinobi.
  13. const [idl, ...additionalIdls] = getAllProgramIdls().map(idl => rootNodeFromAnchor(require(idl)))
  14. const kinobi = k.createFromRoot(idl, additionalIdls);
  15. // Update programs.
  16. kinobi.update(
  17. k.updateProgramsVisitor({
  18. "{{ programCrateName | camelCase }}": { name: "{{ programName | camelCase }}" },
  19. })
  20. );
  21. {% if programFramework === 'shank' %}
  22. // Update accounts.
  23. kinobi.update(
  24. k.updateAccountsVisitor({
  25. counter: {
  26. seeds: [
  27. k.constantPdaSeedNodeFromString("utf8", "counter"),
  28. k.variablePdaSeedNode(
  29. "authority",
  30. k.publicKeyTypeNode(),
  31. "The authority of the counter account"
  32. ),
  33. ],
  34. },
  35. })
  36. );
  37. // Update instructions.
  38. kinobi.update(
  39. k.updateInstructionsVisitor({
  40. create: {
  41. byteDeltas: [k.instructionByteDeltaNode(k.accountLinkNode("counter"))],
  42. accounts: {
  43. counter: { defaultValue: k.pdaValueNode("counter") },
  44. payer: { defaultValue: k.accountValueNode("authority") },
  45. },
  46. },
  47. increment: {
  48. accounts: {
  49. counter: { defaultValue: k.pdaValueNode("counter") },
  50. },
  51. arguments: {
  52. amount: { defaultValue: k.noneValueNode() },
  53. },
  54. },
  55. })
  56. );
  57. // Set account discriminators.
  58. const key = (name) => ({ field: "key", value: k.enumValueNode("Key", name) });
  59. kinobi.update(
  60. k.setAccountDiscriminatorFromFieldVisitor({
  61. counter: key("counter"),
  62. })
  63. );
  64. {% endif %}
  65. {% if jsClient %}
  66. // Render JavaScript.
  67. const jsClient = path.join(__dirname, "..", "clients", "js");
  68. kinobi.accept(
  69. renderJavaScriptVisitor(path.join(jsClient, "src", "generated"), {
  70. prettier: require(path.join(jsClient, ".prettierrc.json"))
  71. })
  72. );
  73. {% endif %}
  74. {% if rustClient %}
  75. // Render Rust.
  76. const rustClient = path.join(__dirname, "..", "clients", "rust");
  77. kinobi.accept(
  78. renderRustVisitor(path.join(rustClient, "src", "generated"), {
  79. formatCode: true,
  80. crateFolder: rustClient,
  81. })
  82. );
  83. {% endif %}