-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
58 lines (45 loc) · 1.51 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
54
55
56
57
58
const { build } = require("esbuild");
const fs = require("fs");
(async () => {
const contractEntries = fs
.readdirSync("./src", { withFileTypes: true })
.filter((el) => el.isDirectory())
.map((el) => `${el.name}/index.ts`);
const testEntries = fs
.readdirSync("./__tests__", { withFileTypes: true })
.filter((el) => !el.isDirectory())
.map(({ name }) => name);
console.log("Building contracts...");
// build contracts
await build({
entryPoints: contractEntries.map((entry) => `./src/${entry}`),
outdir: "./dist",
format: "esm",
bundle: true
});
console.log("Building tests...");
// build tests
await build({
entryPoints: testEntries.map((entry) => `./__tests__/${entry}`),
outdir: "./dist/tests",
format: "cjs"
});
console.log("Getting license...");
// read license
let license = fs.readFileSync("./LICENSE").toString();
const lines = license.split("\n");
// make license a comment
for (let i = 0; i < lines.length; i++) {
lines[i] = " * " + lines[i];
}
license = "/*\n * \n" + lines.join("\n") + "\n * \n */\n\n";
console.log("Appending license and fixing exports...");
for (const entry of contractEntries) {
const filename = `./dist/${entry.replace(/\.ts$/, ".js")}`;
let src = fs.readFileSync(filename).toString();
src = src.replace("async function handle", "export async function handle");
src = src.replace("export {\n handle\n};\n", "");
src = license + src;
fs.writeFileSync(filename, src);
}
})();