-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsolita.ts
52 lines (45 loc) · 1.86 KB
/
solita.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
44
45
46
47
48
49
50
51
52
const PROGRAM_NAME = "blockmesh_program";
const PROGRAM_ID = "FRkQxATWhWqkj3SPZmbBCtkVM4fChd6VYLbEGhgCuHHJ";
const path = require("path");
const programDir = path.join(__dirname, "programs", "blockmesh-program");
const generatedIdlDir = path.join(__dirname, "target", "idl");
const generatedSDKDir = path.join(__dirname, "tests", "generated");
const {spawn} = require("child_process");
const {Solita} = require("@metaplex-foundation/solita");
const {writeFile} = require("fs/promises");
// @ts-ignore
const anchor = spawn("anchor", ["build", "--idl", generatedIdlDir], {
cwd: programDir,
})
.on("error", (err) => {
console.error(err);
// @ts-ignore this err does have a code
if (err.code === "ENOENT") {
console.error(
"Ensure that `anchor` is installed and in your path, see:\n https://project-serum.github.io/anchor/getting-started/installation.html#install-anchor\n",
);
}
process.exit(1);
})
.on("exit", () => {
console.log(
"IDL written to: %s",
path.join(generatedIdlDir, `${PROGRAM_NAME}.json`),
);
generateTypeScriptSDK();
});
anchor.stdout.on("data", (buf) => console.log(buf.toString("utf8")));
anchor.stderr.on("data", (buf) => console.error(buf.toString("utf8")));
async function generateTypeScriptSDK() {
console.error("Generating TypeScript SDK to %s", generatedSDKDir);
const generatedIdlPath = path.join(generatedIdlDir, `${PROGRAM_NAME}.json`);
const idl = require(generatedIdlPath);
if (idl.metadata?.address == null) {
idl.metadata = {...idl.metadata, address: PROGRAM_ID};
await writeFile(generatedIdlPath, JSON.stringify(idl, null, 2));
}
const gen = new Solita(idl, {formatCode: true});
await gen.renderAndWriteTo(generatedSDKDir);
console.error("Success!");
process.exit(0);
}