This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
prepare-package.ts
94 lines (77 loc) · 2.82 KB
/
prepare-package.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import path from "path";
import fsExtra from "fs-extra";
import hre from "hardhat";
import { Artifact } from "hardhat/types";
const artifactsDir: string = path.join(__dirname, "..", "artifacts");
const contracts: string[] = [
"Admin",
"AdminInterface",
"AggregatorV3Interface",
"BalanceSheet",
"BalanceSheetInterface",
"BatterseaTargetV1",
"BatterseaTargetV1Interface",
"ChainlinkOperator",
"ChainlinkOperatorInterface",
"Erc20",
"Erc20Interface",
"Erc20Permit",
"Erc20PermitInterface",
"Fintroller",
"FintrollerInterface",
"FyToken",
"FyTokenInterface",
"RedemptionPool",
"RedemptionPoolInterface",
"StablecoinPriceFeed",
];
const tempArtifactsDir: string = path.join(__dirname, "..", "artifacts-temp");
const typeChainDir: string = path.join(__dirname, "..", "typechain");
const tempTypeChainDir: string = path.join(__dirname, "..", "typechain-temp");
async function writeArtifactToFile(contractName: string): Promise<void> {
const artifact: Artifact = await hre.artifacts.readArtifact(contractName);
await fsExtra.writeJson(path.join(tempArtifactsDir, contractName + ".json"), artifact, { spaces: 2 });
}
async function moveTypeChainBinding(contractName: string): Promise<void> {
const bindingPath: string = path.join(typeChainDir, contractName + ".d.ts");
if (fsExtra.existsSync(bindingPath) == false) {
throw new Error("TypeChain binding " + contractName + ".d.ts not found");
}
await fsExtra.move(bindingPath, path.join(tempTypeChainDir, contractName + ".d.ts"));
}
async function prepareArtifacts(): Promise<void> {
await fsExtra.ensureDir(tempArtifactsDir);
const npmIgnorePath: string = path.join(tempArtifactsDir, ".npmignore");
await fsExtra.ensureFile(npmIgnorePath);
await fsExtra.writeFile(npmIgnorePath, ".DS_Store\n.npmignore");
for (const contract of contracts) {
await writeArtifactToFile(contract);
}
await fsExtra.remove(artifactsDir);
await fsExtra.move(tempArtifactsDir, artifactsDir);
}
async function prepareTypeChainBindings(): Promise<void> {
await fsExtra.ensureDir(tempTypeChainDir);
for (const contract of contracts) {
await moveTypeChainBinding(contract);
}
await fsExtra.remove(typeChainDir);
await fsExtra.move(tempTypeChainDir, typeChainDir);
}
async function main(): Promise<void> {
if (fsExtra.existsSync(artifactsDir) === false) {
throw new Error("Please generate the Hardhat artifacts before running this script");
}
if (fsExtra.existsSync(typeChainDir) === false) {
throw new Error("Please generate the TypeChain bindings before running this script");
}
await prepareArtifacts();
await prepareTypeChainBindings();
console.log("Contract artifacts and TypeChain bindings successfully prepared for npm");
}
main()
.then(() => process.exit(0))
.catch((error: Error) => {
console.error(error);
process.exit(1);
});