forked from metrom-xyz/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
53 lines (44 loc) · 1.45 KB
/
build.js
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
53
import { execSync } from "node:child_process";
import {
appendFileSync,
mkdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
function getJsonAsTypescript(json) {
if (json instanceof Array) {
return `[${json.map(getJsonAsTypescript).join(", ")}]`;
} else if (typeof json === "object" && json !== null) {
const properties = Object.keys(json)
.map((key) => `${key}: ${getJsonAsTypescript(json[key])}`)
.join(", ");
return `{ ${properties} }`;
} else if (typeof json === "string") {
return `"${json}"`;
} else {
return json;
}
}
const CURRENT_DIR = dirname(fileURLToPath(import.meta.url));
console.log("Building contracts...");
execSync("forge build", { stdio: "inherit" });
console.log("Removing previous dist folder...");
rmSync(join(CURRENT_DIR, "./dist"), { recursive: true });
console.log("Building library...");
execSync("pnpm tsc", { stdio: "inherit" });
console.log("Bundling ABIs...");
const { abi } = JSON.parse(
readFileSync(join(CURRENT_DIR, "./out/IMetrom.sol/IMetrom.json")),
);
mkdirSync(join(CURRENT_DIR, "./dist/abis"));
writeFileSync(
join(CURRENT_DIR, "./dist/abis/Metrom.json"),
JSON.stringify(abi, undefined, 4),
);
appendFileSync(
join(CURRENT_DIR, "./dist/index.js"),
`export const metromAbi = ${getJsonAsTypescript(abi)};\n`,
);