From 235bc1e617c8e7dccd13197657f61281aa54006a Mon Sep 17 00:00:00 2001 From: Francesco Ceccon Date: Mon, 29 Jul 2024 15:11:20 +0200 Subject: [PATCH] cli: isolate modules --- examples/cli/apibara.config.ts | 2 +- examples/cli/indexers/simple.ts | 30 ++++++ examples/cli/indexers/with-config.ts | 34 +++++++ examples/cli/package.json | 11 +-- examples/cli/src/indexer.ts | 93 ------------------- examples/cli/src/indexers/demo.indexer.ts | 9 -- examples/cli/src/indexers/test.indexer.ts | 6 -- examples/cli/src/instrumentation.ts | 43 --------- examples/cli/src/main.ts | 4 - packages/cli/build.config.ts | 36 +++++++ packages/cli/package.json | 43 +++++---- packages/cli/src/{ => cli}/commands/build.ts | 11 ++- packages/cli/src/{ => cli}/commands/dev.ts | 2 + .../cli/src/{ => cli}/commands/prepare.ts | 9 +- packages/cli/src/{ => cli}/common.ts | 0 packages/cli/src/{main.ts => cli/index.ts} | 6 +- packages/cli/src/config/defaults.ts | 12 --- packages/cli/src/config/index.ts | 4 +- packages/cli/src/{ => core}/apibara.ts | 4 +- packages/cli/src/core/config/defaults.ts | 12 +++ packages/cli/src/{ => core}/config/loader.ts | 22 ++--- .../config/resolvers/paths.resolver.ts | 2 +- .../config/resolvers/preset.resolver.ts | 2 +- .../resolvers/runtime-config.resolver.ts | 2 +- packages/cli/src/{ => core}/config/update.ts | 3 +- packages/cli/src/core/index.ts | 1 + packages/cli/src/index.ts | 4 - packages/cli/src/rollup/config.ts | 17 ++-- packages/cli/tsconfig.json | 24 ++++- pnpm-lock.yaml | 9 +- 30 files changed, 215 insertions(+), 242 deletions(-) create mode 100644 examples/cli/indexers/simple.ts create mode 100644 examples/cli/indexers/with-config.ts delete mode 100644 examples/cli/src/indexer.ts delete mode 100644 examples/cli/src/indexers/demo.indexer.ts delete mode 100644 examples/cli/src/indexers/test.indexer.ts delete mode 100644 examples/cli/src/instrumentation.ts delete mode 100644 examples/cli/src/main.ts create mode 100644 packages/cli/build.config.ts rename packages/cli/src/{ => cli}/commands/build.ts (64%) rename packages/cli/src/{ => cli}/commands/dev.ts (81%) rename packages/cli/src/{ => cli}/commands/prepare.ts (70%) rename packages/cli/src/{ => cli}/common.ts (100%) rename packages/cli/src/{main.ts => cli/index.ts} (78%) delete mode 100644 packages/cli/src/config/defaults.ts rename packages/cli/src/{ => core}/apibara.ts (91%) create mode 100644 packages/cli/src/core/config/defaults.ts rename packages/cli/src/{ => core}/config/loader.ts (81%) rename packages/cli/src/{ => core}/config/resolvers/paths.resolver.ts (73%) rename packages/cli/src/{ => core}/config/resolvers/preset.resolver.ts (82%) rename packages/cli/src/{ => core}/config/resolvers/runtime-config.resolver.ts (80%) rename packages/cli/src/{ => core}/config/update.ts (67%) create mode 100644 packages/cli/src/core/index.ts delete mode 100644 packages/cli/src/index.ts diff --git a/examples/cli/apibara.config.ts b/examples/cli/apibara.config.ts index 348c1df..d2f4d88 100644 --- a/examples/cli/apibara.config.ts +++ b/examples/cli/apibara.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from "@apibara/cli"; +import { defineConfig } from "apibara/config"; import { defaultSink } from "@apibara/indexer"; export default defineConfig({ diff --git a/examples/cli/indexers/simple.ts b/examples/cli/indexers/simple.ts new file mode 100644 index 0000000..8010c1d --- /dev/null +++ b/examples/cli/indexers/simple.ts @@ -0,0 +1,30 @@ +import { EvmStream } from "@apibara/evm"; +import { defineIndexer } from "@apibara/indexer"; +import { encodeEventTopics, parseAbi } from "viem"; + +const abi = parseAbi([ + "event Transfer(address indexed from, address indexed to, uint256 value)", +]); + +export default defineIndexer(EvmStream)({ + streamUrl: "https://sepolia.ethereum.a5a.ch", + finality: "accepted", + startingCursor: { + orderKey: 5_000_000n, + }, + filter: { + logs: [ + { + strict: true, + topics: encodeEventTopics({ + abi, + eventName: "Transfer", + args: { from: null, to: null }, + }) as `0x${string}`[], + }, + ], + }, + async transform({ block: { header } }) { + return [header]; + }, +}) diff --git a/examples/cli/indexers/with-config.ts b/examples/cli/indexers/with-config.ts new file mode 100644 index 0000000..7dc8777 --- /dev/null +++ b/examples/cli/indexers/with-config.ts @@ -0,0 +1,34 @@ +import { EvmStream } from "@apibara/evm"; +import { defineIndexer } from "@apibara/indexer"; +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); + return defineIndexer(EvmStream)({ + streamUrl: "https://sepolia.ethereum.a5a.ch", + finality: "accepted", + startingCursor: { + orderKey: 5_000_000n, + }, + filter: { + logs: [ + { + strict: true, + topics: encodeEventTopics({ + abi, + eventName: "Transfer", + args: { from: null, to: null }, + }) as `0x${string}`[], + }, + ], + }, + async transform({ block: { header } }) { + return [header]; + }, + }) +} diff --git a/examples/cli/package.json b/examples/cli/package.json index 96d4fcd..cbcdabb 100644 --- a/examples/cli/package.json +++ b/examples/cli/package.json @@ -3,25 +3,22 @@ "version": "1.0.0", "private": true, "scripts": { - "cli:build": "jiti ./src/main.ts build --dir ./src", - "cli:prepare": "jiti ./src/main.ts prepare --dir ./src", - "cli:start": "jiti ./dist/main.mjs", + "build": "apibara build", "typecheck": "tsc --noEmit", "lint": "biome check .", "lint:fix": "pnpm lint --write", "format": "biome format . --write" }, "dependencies": { - "@apibara/evm": "workspace:*", "@apibara/indexer": "workspace:*", - "@apibara/protocol": "workspace:*", - "@apibara/cli": "workspace:*", + "@apibara/evm": "workspace:*", "@opentelemetry/api": "^1.9.0", "@opentelemetry/exporter-trace-otlp-proto": "^0.52.0", "@opentelemetry/resources": "^1.25.0", "@opentelemetry/sdk-node": "^0.52.0", "@opentelemetry/sdk-trace-base": "^1.25.0", "@opentelemetry/semantic-conventions": "^1.25.0", + "apibara": "workspace:*", "better-sqlite3": "^11.1.2", "citty": "^0.1.6", "consola": "^3.2.3", @@ -33,4 +30,4 @@ "@types/node": "^20.12.12", "jiti": "^1.21.0" } -} +} \ No newline at end of file diff --git a/examples/cli/src/indexer.ts b/examples/cli/src/indexer.ts deleted file mode 100644 index c28dddd..0000000 --- a/examples/cli/src/indexer.ts +++ /dev/null @@ -1,93 +0,0 @@ -import assert from "node:assert"; -import { EvmStream } from "@apibara/evm"; -import { defineIndexer, useIndexerContext } from "@apibara/indexer"; -import { trace } from "@opentelemetry/api"; -import consola from "consola"; -import { encodeEventTopics, parseAbi } from "viem"; - -const abi = parseAbi([ - "event Transfer(address indexed from, address indexed to, uint256 value)", -]); - -const tracer = trace.getTracer("evm-indexer-demo"); - -export function createIndexerConfig(streamUrl: string) { - return defineIndexer(EvmStream)({ - streamUrl, - finality: "accepted", - startingCursor: { - orderKey: 560875n, - }, - filter: { - logs: [ - { - strict: true, - topics: encodeEventTopics({ - abi, - eventName: "Transfer", - args: { from: null, to: null }, - }) as `0x${string}`[], - }, - ], - }, - async transform({ block: { header, logs, transactions } }) { - const ctx = useIndexerContext(); - ctx.counter += 1; - - if (!transactions || !header || !header.number) return []; - - return tracer.startActiveSpan("parseLogs", (span) => { - const rows = logs.map((log) => { - assert(log.topics.length === 3, "Transfer event has 3 topics"); - - const { args } = tracer.startActiveSpan("decodeEventLog", (span) => { - // const decoded = decodeEventLog({ - // abi, - // topics: log.topics as [`0x${string}`, ...`0x${string}`[]], - // data: log.data, - // eventName: "Transfer", - // }); - const decoded = { args: { from: "0x0", to: "0x0", value: "0" } }; - - span.end(); - return decoded; - }); - - return { - blockNumber: Number(header.number), - blockHash: header.hash, - logIndex: Number(log.logIndex), - fromAddress: args.from, - toAddress: args.to, - value: Number(args.value), - }; - }); - - span.end(); - return rows; - }); - }, - hooks: { - async "run:before"() {}, - "handler:after"({ output }) { - for (const transfer of output) { - consola.debug( - "Transfer", - transfer.blockNumber, - transfer.logIndex, - transfer.fromAddress, - transfer.toAddress, - transfer.value.toString(), - ); - } - }, - "sink:write"({ data }) { - consola.info("Wrote", data.length, "transfers"); - }, - "sink:flush"() { - consola.debug("Flushing"); - }, - }, - plugins: [], - }); -} diff --git a/examples/cli/src/indexers/demo.indexer.ts b/examples/cli/src/indexers/demo.indexer.ts deleted file mode 100644 index 263a586..0000000 --- a/examples/cli/src/indexers/demo.indexer.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { createIndexer } from "@apibara/indexer"; -import { createIndexerConfig } from "../indexer"; - -export default function demoIndexer(runtimeConfig: unknown) { - console.log("demoIndexer runtimeConfig", runtimeConfig); - return createIndexer( - createIndexerConfig("http://mainnet-v2.starknet.a5a.ch:7007"), - ); -} diff --git a/examples/cli/src/indexers/test.indexer.ts b/examples/cli/src/indexers/test.indexer.ts deleted file mode 100644 index 277ae5b..0000000 --- a/examples/cli/src/indexers/test.indexer.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { createIndexer } from "@apibara/indexer"; -import { createIndexerConfig } from "../indexer"; - -export default createIndexer( - createIndexerConfig("http://mainnet-v2.starknet.a5a.ch:7007"), -); diff --git a/examples/cli/src/instrumentation.ts b/examples/cli/src/instrumentation.ts deleted file mode 100644 index feeb160..0000000 --- a/examples/cli/src/instrumentation.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; -import { Resource } from "@opentelemetry/resources"; -import * as opentelemetry from "@opentelemetry/sdk-node"; -import { - BatchSpanProcessor, - type SpanProcessor, -} from "@opentelemetry/sdk-trace-base"; -import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; -import consola from "consola"; - -let spanProcessor: SpanProcessor | undefined; - -const axiomApiKey = process.env.AXIOM_API_KEY; -const axiomDataset = process.env.AXIOM_DATASET ?? "evm-indexer-demo"; -if (axiomApiKey) { - const exporter = new OTLPTraceExporter({ - url: "https://api.axiom.co/v1/traces", - headers: { - Authorization: `Bearer ${axiomApiKey}`, - "X-Axiom-Dataset": axiomDataset, - }, - }); - spanProcessor = new BatchSpanProcessor(exporter); - consola.info("Sending traces to Axiom", axiomDataset); -} - -const resource = new Resource({ - [SEMRESATTRS_SERVICE_NAME]: "evm-indexer", -}); - -const sdk = new opentelemetry.NodeSDK( - spanProcessor - ? { - spanProcessor, - resource, - } - : { - resource, - }, -); - -consola.info("Starting OpenTelemetry SDK"); -sdk.start(); diff --git a/examples/cli/src/main.ts b/examples/cli/src/main.ts deleted file mode 100644 index 5614928..0000000 --- a/examples/cli/src/main.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { mainCli } from "@apibara/cli"; -import { runMain } from "citty"; - -runMain(mainCli); diff --git a/packages/cli/build.config.ts b/packages/cli/build.config.ts new file mode 100644 index 0000000..321083e --- /dev/null +++ b/packages/cli/build.config.ts @@ -0,0 +1,36 @@ +import { defineBuildConfig } from "unbuild"; +import { fileURLToPath } from "node:url"; +import { resolve } from "pathe"; + +const modules = [ + "cli", + "config", + "core", + "rollup", + "types", +]; + +const srcDir = fileURLToPath(new URL("src", import.meta.url)); + +export default defineBuildConfig({ + entries: [ + { input: "./src/cli/index.ts" }, + { input: "./src/config/index.ts" }, + { input: "./src/core/index.ts" }, + ], + clean: true, + outDir: "./dist", + declaration: true, + alias: { + ...Object.fromEntries( + modules.map((module) => [ + `apibara/${module}`, + resolve(srcDir, `${module}/index.ts`), + ]) + ), + }, + externals: [ + ...modules.map((module) => `apibara/${module}`), + ], +}); + diff --git a/packages/cli/package.json b/packages/cli/package.json index 8f7cf8e..7508db6 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,24 +1,35 @@ { - "name": "@apibara/cli", + "name": "apibara", "version": "2.0.0", "type": "module", - "source": "./src/index.ts", - "main": "./src/index.ts", + "source": "./src/core/index.ts", + "main": "./dist/core/index.mjs", "exports": { - ".": "./src/index.ts" - }, - "publishConfig": { - "files": ["dist", "src", "README.md"], - "main": "./dist/index.mjs", - "types": "./dist/index.d.ts", - "exports": { - ".": { - "types": "./dist/index.d.ts", - "import": "./dist/index.mjs", - "default": "./dist/index.mjs" - } + ".": { + "types": "./dist/core/index.d.ts", + "import": "./dist/core/index.mjs" + }, + "./cli": { + "types": "./dist/cli/index.d.ts", + "import": "./dist/cli/index.mjs" + }, + "./config": { + "types": "./dist/config/index.d.ts", + "import": "./dist/config/index.mjs" + }, + "./core": { + "types": "./dist/core/index.d.ts", + "import": "./dist/core/index.mjs" } }, + "bin": { + "apibara": "./dist/cli/index.mjs" + }, + "files": [ + "dist", + "src", + "README.md" + ], "scripts": { "build": "unbuild", "lint": "biome check .", @@ -52,4 +63,4 @@ "tslib": "^2.6.3", "untyped": "^1.4.2" } -} +} \ No newline at end of file diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/cli/commands/build.ts similarity index 64% rename from packages/cli/src/commands/build.ts rename to packages/cli/src/cli/commands/build.ts index ce189af..6908bc3 100644 --- a/packages/cli/src/commands/build.ts +++ b/packages/cli/src/cli/commands/build.ts @@ -1,9 +1,10 @@ import { defineCommand } from "citty"; import { resolve } from "pathe"; -import { createApibara } from "../apibara"; +import { createApibara } from "apibara/core"; import { commonArgs } from "../common"; -import { build } from "../core/build/build"; -import { prepare } from "../core/build/prepare"; +import consola from "consola"; +// import { build } from "../../core/build/build"; +// import { prepare } from "../../core/build/prepare"; export default defineCommand({ meta: { @@ -14,12 +15,16 @@ export default defineCommand({ ...commonArgs, }, async run({ args }) { + consola.info("build", 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(); + */ }, }); diff --git a/packages/cli/src/commands/dev.ts b/packages/cli/src/cli/commands/dev.ts similarity index 81% rename from packages/cli/src/commands/dev.ts rename to packages/cli/src/cli/commands/dev.ts index d3d337b..8a00f62 100644 --- a/packages/cli/src/commands/dev.ts +++ b/packages/cli/src/cli/commands/dev.ts @@ -1,5 +1,6 @@ import { defineCommand } from "citty"; import { commonArgs } from "../common"; +import consola from "consola"; export default defineCommand({ meta: { @@ -11,5 +12,6 @@ export default defineCommand({ }, async run({ args }) { // TODO + consola.info("dev", args); }, }); diff --git a/packages/cli/src/commands/prepare.ts b/packages/cli/src/cli/commands/prepare.ts similarity index 70% rename from packages/cli/src/commands/prepare.ts rename to packages/cli/src/cli/commands/prepare.ts index 51ea497..ccfba1c 100644 --- a/packages/cli/src/commands/prepare.ts +++ b/packages/cli/src/cli/commands/prepare.ts @@ -1,8 +1,9 @@ +import consola from "consola"; import { defineCommand } from "citty"; import { resolve } from "pathe"; -import { createApibara } from "../apibara"; +// import { createApibara } from "../../apibara"; import { commonArgs } from "../common"; -import { writeTypes } from "../core/build/types"; +// import { writeTypes } from "../../core/build/types"; export default defineCommand({ meta: { @@ -13,9 +14,11 @@ export default defineCommand({ ...commonArgs, }, async run({ args }) { - console.log("prepare", args); const rootDir = resolve((args.dir || ".") as string); + consola.info("prepare", args); + /* const apibara = await createApibara({ rootDir }); await writeTypes(apibara); + */ }, }); diff --git a/packages/cli/src/common.ts b/packages/cli/src/cli/common.ts similarity index 100% rename from packages/cli/src/common.ts rename to packages/cli/src/cli/common.ts diff --git a/packages/cli/src/main.ts b/packages/cli/src/cli/index.ts similarity index 78% rename from packages/cli/src/main.ts rename to packages/cli/src/cli/index.ts index 2735e9d..c370cc1 100755 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/cli/index.ts @@ -1,4 +1,4 @@ -import { defineCommand } from "citty"; +import { defineCommand, runMain } from "citty"; export const mainCli = defineCommand({ meta: { @@ -12,5 +12,5 @@ export const mainCli = defineCommand({ prepare: () => import("./commands/prepare").then((r) => r.default), }, }); -// TODO uncomment when we build the CLI -// runMain(mainCli); + +runMain(mainCli); diff --git a/packages/cli/src/config/defaults.ts b/packages/cli/src/config/defaults.ts deleted file mode 100644 index 4819d62..0000000 --- a/packages/cli/src/config/defaults.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { defaultSink } from "@apibara/indexer"; -import { defineConfig } from "../config"; - -export const ApibaraDefaults = defineConfig({ - rootDir: ".", - outputDir: "./dist", - runtimeConfig: {}, - sink: { - default: () => defaultSink(), - }, - hooks: {}, -}); diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts index 1677c14..dc4dd21 100644 --- a/packages/cli/src/config/index.ts +++ b/packages/cli/src/config/index.ts @@ -1,5 +1,4 @@ -import type { DeepPartial } from "../types"; -import type { ApibaraConfig } from "../types/config"; +import type { DeepPartial, ApibaraConfig } from "apibara/types"; export function defineConfig< // biome-ignore lint/complexity/noBannedTypes: @@ -9,3 +8,4 @@ export function defineConfig< >(config: ApibaraConfig): ApibaraConfig { return config; } + diff --git a/packages/cli/src/apibara.ts b/packages/cli/src/core/apibara.ts similarity index 91% rename from packages/cli/src/apibara.ts rename to packages/cli/src/core/apibara.ts index fab22ce..029b447 100644 --- a/packages/cli/src/apibara.ts +++ b/packages/cli/src/core/apibara.ts @@ -2,12 +2,12 @@ import consola from "consola"; import { createHooks } from "hookable"; import { loadOptions } from "./config/loader"; import { updateApibaraConfig } from "./config/update"; -import type { Apibara } from "./types/apibara"; import type { + Apibara, ApibaraConfig, ApibaraDynamicConfig, LoadConfigOptions, -} from "./types/config"; +} from "apibara/types"; export async function createApibara( config: ApibaraConfig = {}, diff --git a/packages/cli/src/core/config/defaults.ts b/packages/cli/src/core/config/defaults.ts new file mode 100644 index 0000000..445deb4 --- /dev/null +++ b/packages/cli/src/core/config/defaults.ts @@ -0,0 +1,12 @@ +// import { defaultSink } from "@apibara/indexer"; +import { type ApibaraConfig } from "apibara/types"; + +export const ApibaraDefaults: ApibaraConfig = { + rootDir: ".", + outputDir: "./dist", + runtimeConfig: {}, + // sink: { + // // default: () => defaultSink(), + // }, + hooks: {}, +}; diff --git a/packages/cli/src/config/loader.ts b/packages/cli/src/core/config/loader.ts similarity index 81% rename from packages/cli/src/config/loader.ts rename to packages/cli/src/core/config/loader.ts index 13b1ffe..e20ec97 100644 --- a/packages/cli/src/config/loader.ts +++ b/packages/cli/src/core/config/loader.ts @@ -4,7 +4,7 @@ import type { ApibaraConfig, ApibaraOptions, LoadConfigOptions, -} from "../types/config"; +} from "apibara/types"; import { ApibaraDefaults } from "./defaults"; import { resolvePathOptions } from "./resolvers/paths.resolver"; import { presetResolver } from "./resolvers/preset.resolver"; @@ -37,16 +37,16 @@ async function _loadUserConfig( const loadedConfig = await (opts.watch ? watchConfig : loadConfig)({ - name: "apibara", - // path from where apibara.config.ts is loaded - // defautl is "." path - // cwd: configOverrides.rootDir, - overrides: { - ...configOverrides, - }, - defaults: { ...ApibaraDefaults }, - ...opts.c12, - }); + name: "apibara", + // path from where apibara.config.ts is loaded + // defautl is "." path + // cwd: configOverrides.rootDir, + overrides: { + ...configOverrides, + }, + defaults: { ...ApibaraDefaults }, + ...opts.c12, + }); const options = klona(loadedConfig.config) as ApibaraOptions; diff --git a/packages/cli/src/config/resolvers/paths.resolver.ts b/packages/cli/src/core/config/resolvers/paths.resolver.ts similarity index 73% rename from packages/cli/src/config/resolvers/paths.resolver.ts rename to packages/cli/src/core/config/resolvers/paths.resolver.ts index 79f24e6..ec59c70 100644 --- a/packages/cli/src/config/resolvers/paths.resolver.ts +++ b/packages/cli/src/core/config/resolvers/paths.resolver.ts @@ -1,5 +1,5 @@ import { resolve } from "pathe"; -import type { ApibaraOptions } from "../../types/config"; +import type { ApibaraOptions } from "apibara/types"; export async function resolvePathOptions(options: ApibaraOptions) { options.rootDir = resolve(options.rootDir || "."); diff --git a/packages/cli/src/config/resolvers/preset.resolver.ts b/packages/cli/src/core/config/resolvers/preset.resolver.ts similarity index 82% rename from packages/cli/src/config/resolvers/preset.resolver.ts rename to packages/cli/src/core/config/resolvers/preset.resolver.ts index 96a191a..372ce8f 100644 --- a/packages/cli/src/config/resolvers/preset.resolver.ts +++ b/packages/cli/src/core/config/resolvers/preset.resolver.ts @@ -1,5 +1,5 @@ import defu from "defu"; -import type { ApibaraOptions } from "../../types/config"; +import type { ApibaraOptions } from "apibara/types"; export async function presetResolver(options: ApibaraOptions) { if (options.preset && options.presets?.[options.preset]) { diff --git a/packages/cli/src/config/resolvers/runtime-config.resolver.ts b/packages/cli/src/core/config/resolvers/runtime-config.resolver.ts similarity index 80% rename from packages/cli/src/config/resolvers/runtime-config.resolver.ts rename to packages/cli/src/core/config/resolvers/runtime-config.resolver.ts index 92bdf86..45d7a57 100644 --- a/packages/cli/src/config/resolvers/runtime-config.resolver.ts +++ b/packages/cli/src/core/config/resolvers/runtime-config.resolver.ts @@ -1,4 +1,4 @@ -import type { ApibaraOptions } from "../../types/config"; +import type { ApibaraOptions } from "apibara/types"; export async function resolveRuntimeConfigOptions(options: ApibaraOptions) { options.runtimeConfig = { ...options.runtimeConfig, default: "value" }; diff --git a/packages/cli/src/config/update.ts b/packages/cli/src/core/config/update.ts similarity index 67% rename from packages/cli/src/config/update.ts rename to packages/cli/src/core/config/update.ts index 6017093..b87b102 100644 --- a/packages/cli/src/config/update.ts +++ b/packages/cli/src/core/config/update.ts @@ -1,5 +1,4 @@ -import type { Apibara } from "../types/apibara"; -import type { ApibaraDynamicConfig } from "../types/config"; +import type { Apibara, ApibaraDynamicConfig } from "apibara/types"; export async function updateApibaraConfig( apibara: Apibara, diff --git a/packages/cli/src/core/index.ts b/packages/cli/src/core/index.ts new file mode 100644 index 0000000..2cf10fc --- /dev/null +++ b/packages/cli/src/core/index.ts @@ -0,0 +1 @@ +export { createApibara } from "./apibara"; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts deleted file mode 100644 index c838408..0000000 --- a/packages/cli/src/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from "./main"; -export * from "./types"; -export * from "./hooks"; -export * from "./config"; diff --git a/packages/cli/src/rollup/config.ts b/packages/cli/src/rollup/config.ts index cd49ec1..62ea561 100644 --- a/packages/cli/src/rollup/config.ts +++ b/packages/cli/src/rollup/config.ts @@ -6,8 +6,7 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import typescript from "@rollup/plugin-typescript"; import { readdirSync } from "fs-extra"; import { basename, join } from "pathe"; -import type { Apibara } from "../types/apibara"; -import type { RollupConfig } from "../types/rollup"; +import type { Apibara, RollupConfig } from "apibara/types"; export const getRollupConfig = ( apibara: Apibara, @@ -65,10 +64,10 @@ ${indexerImports} const indexers = { ${indexerFiles - .map( - (file, index) => `'${basename(file, ".indexer.ts")}': indexer${index},`, - ) - .join("\n ")} + .map( + (file, index) => `'${basename(file, ".indexer.ts")}': indexer${index},`, + ) + .join("\n ")} }; const command = defineCommand({ @@ -114,13 +113,13 @@ const command = defineCommand({ } const indexerFactory = indexers[name]; - const indexer = typeof indexerFactory === 'function' - ? await indexerFactory(runtimeConfig) + const indexer = typeof indexerFactory === 'function' + ? await indexerFactory(runtimeConfig) : indexerFactory; const client = createClient(indexer.streamConfig, indexer.options.streamUrl); const sink = sinkFunction(); - + try { console.log("Running Indexer: ", name); await run(client, indexer, sink); diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 9d05974..d19d6f9 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -5,7 +5,25 @@ "declarationDir": "dist", "noEmit": false, "rootDir": "src", - "types": ["node"] + "types": [ + "node" + ], + "paths": { + "apibara/cli": [ + "./src/cli" + ], + "apibara/config": [ + "./src/config" + ], + "apibara/core": [ + "./src/core" + ], + "apibara/types": [ + "./src/types" + ] + } }, - "include": ["src/"] -} + "include": [ + "src/" + ] +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 65482a4..14143cc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,18 +51,12 @@ importers: examples/cli: dependencies: - '@apibara/cli': - specifier: workspace:* - version: link:../../packages/cli '@apibara/evm': specifier: workspace:* version: link:../../packages/evm '@apibara/indexer': specifier: workspace:* version: link:../../packages/indexer - '@apibara/protocol': - specifier: workspace:* - version: link:../../packages/protocol '@opentelemetry/api': specifier: ^1.9.0 version: 1.9.0 @@ -81,6 +75,9 @@ importers: '@opentelemetry/semantic-conventions': specifier: ^1.25.0 version: 1.25.0 + apibara: + specifier: workspace:* + version: link:../../packages/cli better-sqlite3: specifier: ^11.1.2 version: 11.1.2