-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathregenerateIdl.ts
43 lines (35 loc) · 1.21 KB
/
regenerateIdl.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { type Idl } from "@coral-xyz/anchor";
import fs from "fs";
const jsonPath = process.argv[2];
if (jsonPath === undefined) {
console.error(`Usage:\
${process.argv[0]} ${process.argv[1]} <idl.json>`);
process.exit(1);
}
// snake to title case
const titleCase = (str: string) =>
str
.split("_")
.map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase())
.join("");
const idl: Idl = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
const name = titleCase(idl["name"]);
idl.accounts?.forEach((account) => {
// NOTE: here we translate PascalCase to camelCase, with the exception of all
// uppercase account names, such as 'LUT', which we want to preserve.
//
// The translation needs to be done because anchor generates an invalid IDL file, so we patch it.
// Anchor handles all uppercase account names specially (when generating the account discriminator),
// so we need to preserve them.
if (!account.name.match(/^[A-Z]+$/)) {
account.name = account.name.replace(/^[A-Z]+/, (match) =>
match.toLowerCase()
);
}
});
// heredoc
const ts = `\
export type ${name} = ${JSON.stringify(idl, null, 2)}\
export const IDL: ${name} = ${JSON.stringify(idl, null, 2)}
`;
console.log(ts);