Skip to content

Commit 49d1f1e

Browse files
committed
Initial commit
Copy/pasted from Codama monorepo as-is.
0 parents  commit 49d1f1e

File tree

95 files changed

+7271
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+7271
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist/
2+
test/e2e/
3+
test-ledger/
4+
target/
5+
CHANGELOG.md

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Codama
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Codama ➤ Renderers ➤ Demo
2+
3+
[![npm][npm-image]][npm-url]
4+
[![npm-downloads][npm-downloads-image]][npm-url]
5+
6+
[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/renderers-demo.svg?style=flat
7+
[npm-image]: https://img.shields.io/npm/v/@codama/renderers-demo.svg?style=flat&label=%40codama%2Frenderers-demo
8+
[npm-url]: https://www.npmjs.com/package/@codama/renderers-demo
9+
10+
This package provides a demo implementation of a Codama renderer to help developers create their own.
11+
12+
## Installation
13+
14+
```sh
15+
pnpm install @codama/renderers-demo
16+
```
17+
18+
## Usage
19+
20+
Add the following script to your Codama configuration file.
21+
22+
```json
23+
{
24+
// ...
25+
"scripts": {
26+
"demo": {
27+
"from": "@codama/renderers-demo",
28+
"args": ["docs"]
29+
}
30+
}
31+
}
32+
```

package.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "@codama/renderers-demo",
3+
"version": "1.0.1",
4+
"description": "Demo renderer to help creating new ones",
5+
"exports": {
6+
"types": "./dist/types/index.d.ts",
7+
"react-native": "./dist/index.react-native.mjs",
8+
"browser": {
9+
"import": "./dist/index.browser.mjs",
10+
"require": "./dist/index.browser.cjs"
11+
},
12+
"node": {
13+
"import": "./dist/index.node.mjs",
14+
"require": "./dist/index.node.cjs"
15+
}
16+
},
17+
"browser": {
18+
"./dist/index.node.cjs": "./dist/index.browser.cjs",
19+
"./dist/index.node.mjs": "./dist/index.browser.mjs"
20+
},
21+
"main": "./dist/index.node.cjs",
22+
"module": "./dist/index.node.mjs",
23+
"react-native": "./dist/index.react-native.mjs",
24+
"types": "./dist/types/index.d.ts",
25+
"type": "commonjs",
26+
"files": [
27+
"./dist/types",
28+
"./dist/index.*"
29+
],
30+
"sideEffects": false,
31+
"keywords": [
32+
"solana",
33+
"framework",
34+
"standard",
35+
"renderers",
36+
"demo"
37+
],
38+
"scripts": {
39+
"build": "rimraf dist && tsup && tsc -p ./tsconfig.declarations.json",
40+
"dev": "vitest --project node",
41+
"lint": "eslint . && prettier --check .",
42+
"lint:fix": "eslint --fix . && prettier --write .",
43+
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:unit && pnpm test:e2e && pnpm test:exports",
44+
"test:e2e": "./test/e2e/test.sh",
45+
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
46+
"test:treeshakability": "for file in dist/index.*.mjs; do agadoo $file; done",
47+
"test:types": "tsc --noEmit",
48+
"test:unit": "vitest run"
49+
},
50+
"dependencies": {
51+
"@codama/errors": "workspace:*",
52+
"@codama/nodes": "workspace:*",
53+
"@codama/renderers-core": "workspace:*",
54+
"@codama/visitors-core": "workspace:*",
55+
"@solana/codecs": "^3.0.3"
56+
},
57+
"devDependencies": {
58+
"@codama/cli": "workspace:*"
59+
},
60+
"license": "MIT",
61+
"repository": {
62+
"type": "git",
63+
"url": "https://github.com/codama-idl/codama"
64+
},
65+
"bugs": {
66+
"url": "http://github.com/codama-idl/codama/issues"
67+
},
68+
"browserslist": [
69+
"supports bigint and not dead",
70+
"maintained node versions"
71+
]
72+
}

src/fragments/accountPage.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { AccountNode, PdaNode, titleCase } from '@codama/nodes';
2+
import { visit } from '@codama/visitors-core';
3+
4+
import {
5+
Fragment,
6+
fragment,
7+
getCodeBlockFragment,
8+
getFrontmatterFragment,
9+
getPageFragment,
10+
getTitleAndDescriptionFragment,
11+
} from '../utils';
12+
import { TypeVisitor } from '../visitors/getTypeVisitor';
13+
import { getPdaFunctionUsageFragment } from './pdaFunctionUsage';
14+
15+
export function getAccountPageFragment(
16+
node: AccountNode,
17+
typeVisitor: TypeVisitor,
18+
size?: number,
19+
pda?: PdaNode,
20+
): Fragment {
21+
const title = titleCase(node.name);
22+
const type = visit(node, typeVisitor);
23+
24+
return getPageFragment(
25+
[
26+
getFrontmatterFragment(title, `Overview of the ${title} account`),
27+
getTitleAndDescriptionFragment(title, node.docs),
28+
fragment`## Account data`,
29+
getCodeBlockFragment(type, 'ts'),
30+
...(size ? [fragment`This account has a fixed size of ${size} bytes.`] : []),
31+
...(pda ? [fragment`## PDA`, getCodeBlockFragment(getPdaFunctionUsageFragment(pda), 'ts')] : []),
32+
],
33+
// Generated accounts are within the same directory.
34+
{ generatedAccounts: '.' },
35+
);
36+
}

src/fragments/definedTypePage.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { DefinedTypeNode, titleCase } from '@codama/nodes';
2+
import { visit } from '@codama/visitors-core';
3+
4+
import {
5+
Fragment,
6+
fragment,
7+
getCodeBlockFragment,
8+
getFrontmatterFragment,
9+
getPageFragment,
10+
getTitleAndDescriptionFragment,
11+
} from '../utils';
12+
import { TypeVisitor } from '../visitors/getTypeVisitor';
13+
14+
export function getDefinedTypePageFragment(node: DefinedTypeNode, typeVisitor: TypeVisitor): Fragment {
15+
const title = titleCase(node.name);
16+
const type = visit(node, typeVisitor);
17+
18+
return getPageFragment(
19+
[
20+
getFrontmatterFragment(title, `Overview of the ${title} type`),
21+
getTitleAndDescriptionFragment(title, node.docs),
22+
fragment`## Type definition`,
23+
getCodeBlockFragment(type, 'ts'),
24+
],
25+
// Generated types are within the same directory.
26+
{ generatedTypes: '.' },
27+
);
28+
}

src/fragments/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export * from './accountPage';
2+
export * from './definedTypePage';
3+
export * from './instructionAccounts';
4+
export * from './instructionPage';
5+
export * from './pdaFunctionUsage';
6+
export * from './pdaPage';
7+
export * from './pdaSeeds';
8+
export * from './programErrors';
9+
export * from './programPage';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { camelCase, InstructionAccountNode } from '@codama/nodes';
2+
3+
import { Fragment, getTableFragment } from '../utils';
4+
5+
export function getInstructionAccountsFragment(accounts: InstructionAccountNode[]): Fragment | undefined {
6+
if (accounts.length === 0) return;
7+
8+
const hasDescriptions = (accounts ?? []).some(account => account.docs && account.docs.length > 0);
9+
const accountHeaders = ['Name', 'Signer', 'Writable', 'Required', ...(hasDescriptions ? ['Description'] : [])];
10+
const accountRows = accounts.map(account => [
11+
`\`${camelCase(account.name)}\``,
12+
formatBoolean(account.isSigner ?? false),
13+
formatBoolean(account.isWritable ?? false),
14+
formatBoolean(!(account.isOptional ?? false)),
15+
...(hasDescriptions ? [(account.docs ?? []).join(' ')] : []),
16+
]);
17+
18+
return getTableFragment(accountHeaders, accountRows);
19+
}
20+
21+
function formatBoolean(value: boolean | 'either') {
22+
return value === 'either' ? value : value ? '✅' : '❌';
23+
}

src/fragments/instructionPage.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { InstructionNode, titleCase } from '@codama/nodes';
2+
import { visit } from '@codama/visitors-core';
3+
4+
import {
5+
Fragment,
6+
fragment,
7+
getCodeBlockFragment,
8+
getFrontmatterFragment,
9+
getPageFragment,
10+
getTitleAndDescriptionFragment,
11+
} from '../utils';
12+
import { TypeVisitor } from '../visitors/getTypeVisitor';
13+
import { getInstructionAccountsFragment } from './instructionAccounts';
14+
15+
export function getInstructionPageFragment(node: InstructionNode, typeVisitor: TypeVisitor): Fragment {
16+
const title = titleCase(node.name);
17+
const type = visit(node, typeVisitor);
18+
const accountsFragment = getInstructionAccountsFragment(node.accounts);
19+
const hasArguments = node.arguments.length > 0;
20+
21+
return getPageFragment(
22+
[
23+
getFrontmatterFragment(title, `Overview of the ${title} instruction`),
24+
getTitleAndDescriptionFragment(title, node.docs),
25+
fragment`## Instruction accounts`,
26+
accountsFragment ? accountsFragment : fragment`_This instruction has no accounts._`,
27+
fragment`## Instruction arguments`,
28+
hasArguments ? getCodeBlockFragment(type, 'ts') : fragment`_This instruction has no arguments._`,
29+
],
30+
// Generated instructions are within the same directory.
31+
{ generatedInstructions: '.' },
32+
);
33+
}

0 commit comments

Comments
 (0)