Skip to content

Commit

Permalink
cli: add prepare command
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Jul 30, 2024
1 parent f374a44 commit b0652bc
Show file tree
Hide file tree
Showing 29 changed files with 286 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
dist
.turbo
bun.lockb
.vscode
.vscode
.apibara
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"recommended": true,
"correctness": {
"noUnusedImports": "error"
},
"style": {
"noNonNullAssertion": "off"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion examples/cli/apibara.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from "apibara/config";
import { defaultSink } from "@apibara/indexer";
import { defineConfig } from "apibara/config";

export default defineConfig({
runtimeConfig: {
Expand Down
2 changes: 1 addition & 1 deletion examples/cli/indexers/simple.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export default defineIndexer(EvmStream)({
async transform({ block: { header } }) {
return [header];
},
})
});
7 changes: 3 additions & 4 deletions examples/cli/indexers/with-config.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { EvmStream } from "@apibara/evm";
import { defineIndexer } from "@apibara/indexer";
import type { ApibaraRuntimeConfig } from "apibara/types";
import { encodeEventTopics, parseAbi } from "viem";

const abi = parseAbi([
"event Transfer(address indexed from, address indexed to, uint256 value)",
]);

// TODO: `runtimeConfig` type should come from the generated types file
export default function indexer(runtimeConfig: unknown) {
console.log("runtimeConfig", runtimeConfig);
export default function indexer(runtimeConfig: ApibaraRuntimeConfig) {
return defineIndexer(EvmStream)({
streamUrl: "https://sepolia.ethereum.a5a.ch",
finality: "accepted",
Expand All @@ -30,5 +29,5 @@ export default function indexer(runtimeConfig: unknown) {
async transform({ block: { header } }) {
return [header];
},
})
});
}
6 changes: 4 additions & 2 deletions examples/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.0.0",
"private": true,
"scripts": {
"build": "apibara build",
"apibara:build": "apibara build",
"apibara:start": "jiti ./dist/main.mjs",
"apibara:prepare": "apibara prepare",
"typecheck": "tsc --noEmit",
"lint": "biome check .",
"lint:fix": "pnpm lint --write",
Expand Down Expand Up @@ -31,4 +33,4 @@
"@types/node": "^20.12.12",
"jiti": "^1.21.0"
}
}
}
12 changes: 4 additions & 8 deletions examples/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"extends": "../../tsconfig.json",
"extends": ["../../tsconfig.json", "./.apibara/types/tsconfig.json"],
"compilerOptions": {
"outDir": "dist",
"declarationDir": "dist",
"noEmit": false,
"rootDir": ".",
"types": [
"node"
]
"types": ["node"]
},
"include": [
"indexers/"
]
}
"include": ["indexers/", "apibara.config.ts"]
}
20 changes: 7 additions & 13 deletions packages/cli/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { defineBuildConfig } from "unbuild";
import { fileURLToPath } from "node:url";
import { resolve } from "pathe";
import { defineBuildConfig } from "unbuild";

const modules = [
"cli",
"config",
"core",
"rollup",
"types",
];
const modules = ["cli", "config", "core", "rollup", "types", "hooks "];

// @ts-ignore The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
const srcDir = fileURLToPath(new URL("src", import.meta.url));

export default defineBuildConfig({
Expand All @@ -18,6 +13,8 @@ export default defineBuildConfig({
{ input: "./src/config/index.ts" },
{ input: "./src/core/index.ts" },
{ input: "./src/rollup/index.ts" },
{ input: "./src/types/index.ts" },
{ input: "./src/hooks/index.ts" },
],
clean: true,
outDir: "./dist",
Expand All @@ -27,11 +24,8 @@ export default defineBuildConfig({
modules.map((module) => [
`apibara/${module}`,
resolve(srcDir, `${module}/index.ts`),
])
]),
),
},
externals: [
...modules.map((module) => `apibara/${module}`),
],
externals: [...modules.map((module) => `apibara/${module}`)],
});

17 changes: 11 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
"./rollup": {
"types": "./dist/rollup/index.d.ts",
"import": "./dist/rollup/index.mjs"
},
"./types": {
"types": "./dist/types/index.d.ts",
"import": "./dist/types/index.mjs"
},
"./hooks": {
"types": "./dist/hooks/index.d.ts",
"import": "./dist/hooks/index.mjs"
}
},
"bin": {
"apibara": "./dist/cli/index.mjs"
},
"files": [
"dist",
"src",
"README.md"
],
"files": ["dist", "src", "README.md"],
"scripts": {
"build": "unbuild",
"lint": "biome check .",
Expand Down Expand Up @@ -63,8 +67,9 @@
"hookable": "^5.5.3",
"klona": "^2.0.6",
"pathe": "^1.1.2",
"pkg-types": "^1.1.3",
"rollup": "^4.18.1",
"tslib": "^2.6.3",
"untyped": "^1.4.2"
}
}
}
7 changes: 3 additions & 4 deletions packages/cli/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { build, createApibara, prepare } from "apibara/core";
import { defineCommand } from "citty";
import consola from "consola";
import { resolve } from "pathe";
import { createApibara, build, prepare } from "apibara/core";
import { commonArgs } from "../common";
import consola from "consola";

export default defineCommand({
meta: {
Expand All @@ -13,12 +13,11 @@ export default defineCommand({
...commonArgs,
},
async run({ args }) {
consola.info("build", args);
consola.info("Building with args", args);
const rootDir = resolve((args.dir || args._dir || ".") as string);
const apibara = await createApibara({
rootDir,
});
consola.info("apibara", apibara);
await prepare(apibara);
await build(apibara);
await apibara.close();
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCommand } from "citty";
import { commonArgs } from "../common";
import consola from "consola";
import { commonArgs } from "../common";

export default defineCommand({
meta: {
Expand All @@ -12,6 +12,6 @@ export default defineCommand({
},
async run({ args }) {
// TODO
consola.info("dev", args);
consola.info("Not implemented!", args);
},
});
8 changes: 4 additions & 4 deletions packages/cli/src/cli/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import consola from "consola";
import { createApibara, writeTypes } from "apibara/core";
import {} from "apibara/types";
import { defineCommand } from "citty";
import consola from "consola";
import { resolve } from "pathe";
// import { createApibara } from "../../apibara";
import { commonArgs } from "../common";
Expand All @@ -14,11 +16,9 @@ export default defineCommand({
...commonArgs,
},
async run({ args }) {
consola.info("Prepairing \n\n");
const rootDir = resolve((args.dir || ".") as string);
consola.info("prepare", args);
/*
const apibara = await createApibara({ rootDir });
await writeTypes(apibara);
*/
},
});
3 changes: 1 addition & 2 deletions packages/cli/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DeepPartial, ApibaraConfig } from "apibara/types";
import type { ApibaraConfig, DeepPartial } from "apibara/types";

export function defineConfig<
// biome-ignore lint/complexity/noBannedTypes: <explanation>
Expand All @@ -8,4 +8,3 @@ export function defineConfig<
>(config: ApibaraConfig<T, R>): ApibaraConfig<T, R> {
return config;
}

8 changes: 4 additions & 4 deletions packages/cli/src/core/apibara.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import consola from "consola";
import { createHooks } from "hookable";
import { loadOptions } from "./config/loader";
import { updateApibaraConfig } from "./config/update";
import type {
Apibara,
ApibaraConfig,
ApibaraDynamicConfig,
LoadConfigOptions,
} from "apibara/types";
import consola from "consola";
import { createHooks } from "hookable";
import { loadOptions } from "./config/loader";
import { updateApibaraConfig } from "./config/update";

export async function createApibara(
config: ApibaraConfig = {},
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/core/build/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fsp from "node:fs/promises";
import fse from "fs-extra";
import type { Apibara } from "apibara/types";
import consola from "consola";
import fse from "fs-extra";

export async function prepare(apibara: Apibara) {
consola.info("Cleaning output directory");
await prepareDir(apibara.options.outputDir);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/core/build/prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Apibara, RollupConfig } from "apibara/types";
import consola from "consola";
import { type OutputOptions, rollup } from "rollup";
import type { Apibara } from "../../types/apibara";
import type { RollupConfig } from "../../types/rollup";

export async function buildProduction(
apibara: Apibara,
Expand All @@ -24,7 +24,7 @@ export async function buildProduction(
// Close the bundle
await bundle.close();

console.log("Build completed successfully!");
consola.success("Build completed successfully!");
} catch (error) {
console.error("Build failed:", error);
throw error;
Expand Down
Loading

0 comments on commit b0652bc

Please sign in to comment.