-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-codama-client.ts
More file actions
26 lines (22 loc) · 886 Bytes
/
create-codama-client.ts
File metadata and controls
26 lines (22 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { createFromRoot } from "codama";
import { rootNodeFromAnchor } from "@codama/nodes-from-anchor";
import { renderJavaScriptVisitor } from "@codama/renderers";
import path from "path";
import { promises as fs } from "fs";
const loadJSON = async (...pathSegments: Array<string>) => {
const filePath = path.join(...pathSegments);
try {
return JSON.parse(await fs.readFile(filePath, "utf-8"));
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
throw new Error(`Failed to load JSON file: ${filePath} does not exist`);
}
throw error;
}
};
// Instantiate Codama
const idl = await loadJSON("target", "idl", "escrow.json");
const codama = createFromRoot(rootNodeFromAnchor(idl));
// Render JavaScript.
const generatedPath = path.join("dist", "js-client");
codama.accept(renderJavaScriptVisitor(generatedPath));