Skip to content

Commit

Permalink
feat: compile generated chain srcs with esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
grikomsn committed Jun 17, 2023
1 parent a79cde6 commit 39f029e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/graz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@walletconnect/utils": "^2.8.1",
"@web3modal/standalone": "^2.4.3",
"commander": "^11.0.0",
"esbuild": "^0.18.4",
"zustand": "^4.3.8"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions packages/graz/src/cli/build-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as esbuild from "esbuild";
import { globby } from "globby";

export const buildJs = async (format: esbuild.Format) => {
const paths = await globby(["chains/**/*.ts"]);
await esbuild.build({
allowOverwrite: true,
bundle: false,
entryPoints: [...paths],
format,
outdir: "chains",
});
};
13 changes: 9 additions & 4 deletions packages/graz/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import os from "node:os";

import * as p from "@clack/prompts";
import { Command } from "commander";
import type { Format } from "esbuild";
import pMap from "p-map";

import { buildJs } from "./build-js";
import { cloneRegistry } from "./clone-registry";
import { getChainPaths } from "./get-chain-paths";
import { makeRootSources } from "./make-root-sources";
Expand Down Expand Up @@ -33,37 +35,40 @@ const cli = async () => {
"-T, --testnet <chainPaths...>",
'generate given testnet chain paths separated by spaces (e.g. "atlantic bitcannadev cheqdtestnet")',
)
.option("--format <format>", "specify javascript module format: cjs, esm (defaults to cjs)")
.action(async (options) => {
const customRegistry = options.registry as string | undefined;
const mainnetFilter = options.mainnet as string[] | undefined;
const testnetFilter = options.testnet as string[] | undefined;

const jsFormat: Format = options.format === "esm" || options.format === "cjs" ? options.format : "cjs";

p.intro("graz generate");
const s = p.spinner();

// p.log.step("Cloning chain registry...");
s.start(`Cloning chain registry`);
await cloneRegistry(customRegistry);
s.stop("Cloned chain registry ✅");

// p.log.step("Retrieving chain paths...");
s.start("Retrieving chain paths");
const { mainnetPaths, testnetPaths } = await getChainPaths({ mainnetFilter, testnetFilter });
s.stop("Retrieved chain paths ✅");

// p.log.step("Generating chain sources...");
s.start("Generating chain sources");
await fs.rm("chains/", { recursive: true, force: true });
await pMap([...mainnetPaths, ...testnetPaths], makeSources, {
concurrency: Math.max(1, (os.cpus() || { length: 1 }).length - 1),
});
s.stop("Generated chain sources ✅");

// p.log.step("Generating chain index...");
s.start("Generating chain index");
await makeRootSources({ mainnetPaths, testnetPaths });
s.stop("Generated chain index ✅");

s.start("Compiling typescript sources to javascript");
await buildJs(jsFormat);
s.stop("Compiled sources ✅");

p.outro('Generate complete! You can import `mainnetChains` and `testnetChains` from "graz/chains". 🎉');
});

Expand Down
4 changes: 0 additions & 4 deletions packages/graz/stubs/chains-index.ts.stub
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { createRequire } from "node:module";

import type { ChainData, ChainName, ReturnTuple } from "./generated";

const require = createRequire(import.meta.url);

export * from "./generated";

export const getChainData = <T extends ChainName>(pathOrPaths: T | T[]) => {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 39f029e

@grikomsn
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.