-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb-config.ts
184 lines (169 loc) · 5.31 KB
/
web-config.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { contractNames, getChainAddress } from "@tasks/utils"
import { Chain, tokens } from "@tasks/utils/tokens"
import { copySync, outputFileSync, outputJsonSync, readdirSync, readJsonSync, removeSync } from "fs-extra"
import { join, resolve } from "path"
import sh from "shelljs"
import type { Token } from "@tasks/utils/tokens"
const names = {
main: "index.js",
types: "index.d.ts",
}
const paths = {
package: resolve("package.json"),
tmp: resolve("tmp"),
abis: resolve("abis"),
out: resolve("dist-web"),
license: resolve("LICENSE"),
readme: resolve("README.md"),
tsconfig: resolve("tsconfig.json"),
}
const tsconfig = {
compilerOptions: {
target: "ES2020",
lib: ["dom", "ES2022"],
module: "CommonJS",
declaration: true,
esModuleInterop: true,
moduleResolution: "Node",
sourceMap: false,
allowJs: false,
resolveJsonModule: true,
skipLibCheck: true,
outDir: paths.out,
},
}
const chainMapping: Record<Chain, number> = {
[Chain.mainnet]: 1,
[Chain.polygon]: 137,
[Chain.mumbai]: 80001,
[Chain.goerli]: 5,
[Chain.sepolia]: 11155111,
}
const clean = () => {
removeSync(paths.tmp)
removeSync(paths.out)
}
const compile = () => {
// tokens.ts
try {
const toks: Record<number, Record<string, Token>> = tokens.reduce(
(acc, curr) => ({
...acc,
[chainMapping[curr.chain]]: {
...acc[chainMapping[curr.chain]],
[curr.symbol.toLowerCase()]: {
address: curr.address.toLowerCase(),
name: curr.symbol,
decimals: curr.decimals,
},
},
}),
{},
)
const types = tokens.reduce((acc, curr) => acc.add(curr.symbol.toLocaleLowerCase()), new Set())
outputFileSync(
join(paths.tmp, "tokens.ts"),
`export type SupportedToken = '${Array.from(types).join("'|'")}';
export const tokens = ${JSON.stringify(toks, null, 2)}
`,
)
} catch (e) {
console.error("Error writing token.ts ", e)
}
// contracts.ts
try {
const contracts = Object.entries(chainMapping).reduce(
(acc, [chain, id]) => ({
...acc,
[id]: contractNames.reduce((a, name) => ({ ...a, [name]: getChainAddress(name, Number(chain))?.toLowerCase() }), {}),
}),
{},
)
const types = contractNames.reduce((acc, curr) => acc.add(curr), new Set())
outputFileSync(
join(paths.tmp, "contracts.ts"),
`export type SupportedContract = '${Array.from(types).join("'|'")}';
export const contracts = ${JSON.stringify(contracts, null, 2)}`,
)
} catch (e) {
console.error("Error writing contracts.ts ", e)
}
// abis
let abis: string[]
try {
sh.exec("yarn compile")
sh.exec("yarn compile-abis")
copySync(paths.abis, join(paths.tmp, "abis"))
abis = readdirSync(join(paths.tmp, "abis"))
} catch (e) {
console.error("Error extracting abis ", e)
}
// index.ts
try {
outputFileSync(
join(paths.tmp, "index.ts"),
`export * from "./contracts"\nexport * from "./tokens"\n${abis
.map((abi) => `export { default as ${abi.replace(".json", "")}ABI } from "./abis/${abi}"`)
.join(`\n`)}`,
)
} catch (e) {
console.error("Error writing index.ts ", e)
}
}
const bundle = () => {
// package.json
try {
const { name, version, description, author, license, repository, bugs, homepage, keywords } = readJsonSync(paths.package)
outputJsonSync(join(paths.tmp, "package.json"), {
name: `${name}-web`,
version,
description,
author,
license,
repository: { ...repository, url: "https://github.com/mstable/metavaults" },
bugs,
homepage,
keywords,
main: names.main,
types: names.types,
// Enable if it is desired to publish to git hub packages
publishConfig: {
registry: "https://registry.npmjs.org",
email: "[email protected]",
scope: "@mstable",
},
})
} catch (e) {
console.error("Error writing package.json ", e)
}
// typescript
try {
outputJsonSync(join(paths.tmp, "tsconfig.json"), tsconfig)
sh.exec(`npx tsc --project ${join(paths.tmp, "tsconfig.json")}`)
} catch (e) {
console.error("Error compiling typescript ", e)
}
// misc files
try {
copySync(join(paths.tmp, "package.json"), join(paths.out, "package.json"))
copySync(paths.license, join(paths.out, "LICENSE"))
copySync(paths.readme, join(paths.out, "README.md"))
} catch (e) {
console.error("Error copying legal ", e)
}
}
const publish = () => {
// publish
try {
sh.exec(`npm publish --access public`, { cwd: paths.out })
} catch (e) {
console.error("Error publishing npm package ", e)
}
}
; (async () => {
clean()
compile()
bundle()
publish()
clean()
})()