generate-clients.mjs.njk 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) =>
  14. rootNodeFromAnchor(require(idl))
  15. );
  16. const kinobi = k.createFromRoot(idl, additionalIdls);
  17. // Update programs.
  18. kinobi.update(
  19. k.updateProgramsVisitor({
  20. {{ programCrateName | camelCase }}: { name: '{{ programName | camelCase }}' },
  21. })
  22. );
  23. {% if programFramework === 'shank' %}
  24. // Update accounts.
  25. kinobi.update(
  26. k.updateAccountsVisitor({
  27. counter: {
  28. seeds: [
  29. k.constantPdaSeedNodeFromString('utf8', 'counter'),
  30. k.variablePdaSeedNode(
  31. 'authority',
  32. k.publicKeyTypeNode(),
  33. 'The authority of the counter account'
  34. ),
  35. ],
  36. },
  37. })
  38. );
  39. // Update instructions.
  40. kinobi.update(
  41. k.updateInstructionsVisitor({
  42. create: {
  43. byteDeltas: [k.instructionByteDeltaNode(k.accountLinkNode('counter'))],
  44. accounts: {
  45. counter: { defaultValue: k.pdaValueNode('counter') },
  46. payer: { defaultValue: k.accountValueNode('authority') },
  47. },
  48. },
  49. increment: {
  50. accounts: {
  51. counter: { defaultValue: k.pdaValueNode('counter') },
  52. },
  53. arguments: {
  54. amount: { defaultValue: k.noneValueNode() },
  55. },
  56. },
  57. })
  58. );
  59. // Set account discriminators.
  60. const key = (name) => ({ field: 'key', value: k.enumValueNode('Key', name) });
  61. kinobi.update(
  62. k.setAccountDiscriminatorFromFieldVisitor({
  63. counter: key('counter'),
  64. })
  65. );
  66. {% endif %}
  67. {% if jsClient %}
  68. // Render JavaScript.
  69. const jsClient = path.join(__dirname, '..', 'clients', 'js');
  70. kinobi.accept(
  71. renderJavaScriptVisitor(path.join(jsClient, 'src', 'generated'), {
  72. prettierOptions: require(path.join(jsClient, '.prettierrc.json')),
  73. })
  74. );
  75. {% endif %}
  76. {% if rustClient %}
  77. // Render Rust.
  78. const rustClient = path.join(__dirname, '..', 'clients', 'rust');
  79. kinobi.accept(
  80. renderRustVisitor(path.join(rustClient, 'src', 'generated'), {
  81. formatCode: true,
  82. crateFolder: rustClient,
  83. })
  84. );
  85. {% endif %}