-
Notifications
You must be signed in to change notification settings - Fork 1
/
moveAbi.ts
66 lines (56 loc) · 2.38 KB
/
moveAbi.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fs from "fs"
import path from "path"
const contractsRootDir = path.join(__dirname, "./artifacts-zk/contracts")
const abiOutputDir = path.join(__dirname, "./abis")
if (!fs.existsSync(abiOutputDir)) {
fs.mkdirSync(abiOutputDir)
}
// Detect diamond directory correctly
function isDiamondDirectory(dir: string): boolean {
return path.basename(dir) === "diamond"
}
function extractAbisFromDir(dir: string, isDiamond: boolean, localAbis: any[] = []): void {
fs.readdir(dir, { withFileTypes: true }, (err, entries) => {
if (err) {
console.error(`Error reading contracts directory (${dir}):`, err)
return
}
entries.forEach((entry) => {
const entryPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
// Pass isDiamond as true if the current or any parent directory is diamond
extractAbisFromDir(entryPath, isDiamond || isDiamondDirectory(entryPath), isDiamond ? localAbis : undefined)
}
else if (entry.isFile() && entry.name.endsWith(".json")) {
const contractJson = JSON.parse(fs.readFileSync(entryPath, "utf-8"))
const abi = contractJson.abi
if (!abi) {
console.log(`ABI not found in ${entry.name}, skipping...`, "isDiamond", isDiamond)
return
}
else {
console.log(`ABI found in ${entry.name}`, "isDiamond", isDiamond)
}
if (isDiamond) {
localAbis.push(abi)
}
// else if (!isDiamond && (localAbis === undefined || localAbis.length === 0)) {
const abiFileName = entry.name.split(".")[0] + ".abi.json"
const abiOutputPath = path.join(abiOutputDir, abiFileName)
fs.writeFileSync(abiOutputPath, JSON.stringify(abi, null, 2), "utf-8")
console.log(`ABI extracted: ${abiFileName}`)
// }
}
})
// Write the concatenated ABIs if in diamond directory
if (isDiamond && localAbis.length > 0) {
const concatenatedAbis = localAbis.flat()
const diamondAbiFileName = "diamond.abi.json"
const diamondAbiOutputPath = path.join(abiOutputDir, diamondAbiFileName)
fs.writeFileSync(diamondAbiOutputPath, JSON.stringify(concatenatedAbis, null, 2), "utf-8")
console.log(`Concatenated ABI for diamond contract saved: ${diamondAbiFileName}`)
}
})
}
// Start processing from the root directory
extractAbisFromDir(contractsRootDir, isDiamondDirectory(contractsRootDir))