| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #!/usr/bin/env zx
- import 'zx/globals';
- import * as k from 'kinobi';
- import { rootNodeFromAnchor } from '@kinobi-so/nodes-from-anchor';
- {% if jsClient %}
- import { renderVisitor as renderJavaScriptVisitor } from '@kinobi-so/renderers-js';
- {% endif %}
- {% if rustClient %}
- import { renderVisitor as renderRustVisitor } from '@kinobi-so/renderers-rust';
- {% endif %}
- import { getAllProgramIdls } from './utils.mjs';
- // Instanciate Kinobi.
- const [idl, ...additionalIdls] = getAllProgramIdls().map((idl) =>
- rootNodeFromAnchor(require(idl))
- );
- const kinobi = k.createFromRoot(idl, additionalIdls);
- // Update programs.
- kinobi.update(
- k.updateProgramsVisitor({
- {{ programCrateName | camelCase }}: { name: '{{ programName | camelCase }}' },
- })
- );
- {% if programFramework === 'shank' %}
- // Update accounts.
- kinobi.update(
- k.updateAccountsVisitor({
- counter: {
- seeds: [
- k.constantPdaSeedNodeFromString('utf8', 'counter'),
- k.variablePdaSeedNode(
- 'authority',
- k.publicKeyTypeNode(),
- 'The authority of the counter account'
- ),
- ],
- },
- })
- );
- // Update instructions.
- kinobi.update(
- k.updateInstructionsVisitor({
- create: {
- byteDeltas: [k.instructionByteDeltaNode(k.accountLinkNode('counter'))],
- accounts: {
- counter: { defaultValue: k.pdaValueNode('counter') },
- payer: { defaultValue: k.accountValueNode('authority') },
- },
- },
- increment: {
- accounts: {
- counter: { defaultValue: k.pdaValueNode('counter') },
- },
- arguments: {
- amount: { defaultValue: k.noneValueNode() },
- },
- },
- })
- );
- // Set account discriminators.
- const key = (name) => ({ field: 'key', value: k.enumValueNode('Key', name) });
- kinobi.update(
- k.setAccountDiscriminatorFromFieldVisitor({
- counter: key('counter'),
- })
- );
- {% endif %}
- {% if jsClient %}
- // Render JavaScript.
- const jsClient = path.join(__dirname, '..', 'clients', 'js');
- kinobi.accept(
- renderJavaScriptVisitor(path.join(jsClient, 'src', 'generated'), {
- prettierOptions: require(path.join(jsClient, '.prettierrc.json')),
- })
- );
- {% endif %}
- {% if rustClient %}
- // Render Rust.
- const rustClient = path.join(__dirname, '..', 'clients', 'rust');
- kinobi.accept(
- renderRustVisitor(path.join(rustClient, 'src', 'generated'), {
- formatCode: true,
- crateFolder: rustClient,
- })
- );
- {% endif %}
|