From b0652bcfd0f2ff908771baefaf69d4514411ace2 Mon Sep 17 00:00:00 2001 From: jaipaljadeja Date: Tue, 30 Jul 2024 20:59:55 +0530 Subject: [PATCH] cli: add prepare command --- .gitignore | 3 +- biome.json | 3 + examples/cli/apibara.config.ts | 2 +- examples/cli/indexers/simple.indexer.ts | 2 +- examples/cli/indexers/with-config.indexer.ts | 7 +- examples/cli/package.json | 6 +- examples/cli/tsconfig.json | 12 +- packages/cli/build.config.ts | 20 +-- packages/cli/package.json | 17 +- packages/cli/src/cli/commands/build.ts | 7 +- packages/cli/src/cli/commands/dev.ts | 4 +- packages/cli/src/cli/commands/prepare.ts | 8 +- packages/cli/src/config/index.ts | 3 +- packages/cli/src/core/apibara.ts | 8 +- packages/cli/src/core/build/prepare.ts | 4 +- packages/cli/src/core/build/prod.ts | 6 +- packages/cli/src/core/build/types.ts | 159 ++++++++++++++++-- packages/cli/src/core/config/defaults.ts | 17 +- packages/cli/src/core/config/loader.ts | 24 +-- .../core/config/resolvers/paths.resolver.ts | 2 +- .../core/config/resolvers/preset.resolver.ts | 2 +- packages/cli/src/core/index.ts | 1 + packages/cli/src/hooks/useRuntimeConfig.ts | 4 +- packages/cli/src/rollup/config.ts | 32 ++-- packages/cli/src/types/config.ts | 13 ++ packages/cli/src/types/index.ts | 1 + packages/cli/src/types/runtime.ts | 1 + packages/cli/tsconfig.json | 31 +--- pnpm-lock.yaml | 25 +-- 29 files changed, 286 insertions(+), 138 deletions(-) create mode 100644 packages/cli/src/types/runtime.ts diff --git a/.gitignore b/.gitignore index b178996..b83eb57 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules dist .turbo bun.lockb -.vscode \ No newline at end of file +.vscode +.apibara \ No newline at end of file diff --git a/biome.json b/biome.json index 1e7a95f..9629783 100644 --- a/biome.json +++ b/biome.json @@ -9,6 +9,9 @@ "recommended": true, "correctness": { "noUnusedImports": "error" + }, + "style": { + "noNonNullAssertion": "off" } } }, diff --git a/examples/cli/apibara.config.ts b/examples/cli/apibara.config.ts index d2f4d88..284df07 100644 --- a/examples/cli/apibara.config.ts +++ b/examples/cli/apibara.config.ts @@ -1,5 +1,5 @@ -import { defineConfig } from "apibara/config"; import { defaultSink } from "@apibara/indexer"; +import { defineConfig } from "apibara/config"; export default defineConfig({ runtimeConfig: { diff --git a/examples/cli/indexers/simple.indexer.ts b/examples/cli/indexers/simple.indexer.ts index 8010c1d..9101046 100644 --- a/examples/cli/indexers/simple.indexer.ts +++ b/examples/cli/indexers/simple.indexer.ts @@ -27,4 +27,4 @@ export default defineIndexer(EvmStream)({ async transform({ block: { header } }) { return [header]; }, -}) +}); diff --git a/examples/cli/indexers/with-config.indexer.ts b/examples/cli/indexers/with-config.indexer.ts index 7dc8777..f8305b6 100644 --- a/examples/cli/indexers/with-config.indexer.ts +++ b/examples/cli/indexers/with-config.indexer.ts @@ -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", @@ -30,5 +29,5 @@ export default function indexer(runtimeConfig: unknown) { async transform({ block: { header } }) { return [header]; }, - }) + }); } diff --git a/examples/cli/package.json b/examples/cli/package.json index 43c7bdd..5567051 100644 --- a/examples/cli/package.json +++ b/examples/cli/package.json @@ -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", @@ -31,4 +33,4 @@ "@types/node": "^20.12.12", "jiti": "^1.21.0" } -} \ No newline at end of file +} diff --git a/examples/cli/tsconfig.json b/examples/cli/tsconfig.json index 0f2829f..3ec03b8 100644 --- a/examples/cli/tsconfig.json +++ b/examples/cli/tsconfig.json @@ -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/" - ] -} \ No newline at end of file + "include": ["indexers/", "apibara.config.ts"] +} diff --git a/packages/cli/build.config.ts b/packages/cli/build.config.ts index f87c7f6..f90a4fb 100644 --- a/packages/cli/build.config.ts +++ b/packages/cli/build.config.ts @@ -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({ @@ -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", @@ -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}`)], }); - diff --git a/packages/cli/package.json b/packages/cli/package.json index 06e63e3..eb9ede5 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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 .", @@ -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" } -} \ No newline at end of file +} diff --git a/packages/cli/src/cli/commands/build.ts b/packages/cli/src/cli/commands/build.ts index 4ce5368..8aee0f0 100644 --- a/packages/cli/src/cli/commands/build.ts +++ b/packages/cli/src/cli/commands/build.ts @@ -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: { @@ -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(); diff --git a/packages/cli/src/cli/commands/dev.ts b/packages/cli/src/cli/commands/dev.ts index 8a00f62..7561240 100644 --- a/packages/cli/src/cli/commands/dev.ts +++ b/packages/cli/src/cli/commands/dev.ts @@ -1,6 +1,6 @@ import { defineCommand } from "citty"; -import { commonArgs } from "../common"; import consola from "consola"; +import { commonArgs } from "../common"; export default defineCommand({ meta: { @@ -12,6 +12,6 @@ export default defineCommand({ }, async run({ args }) { // TODO - consola.info("dev", args); + consola.info("Not implemented!", args); }, }); diff --git a/packages/cli/src/cli/commands/prepare.ts b/packages/cli/src/cli/commands/prepare.ts index ccfba1c..5b81b00 100644 --- a/packages/cli/src/cli/commands/prepare.ts +++ b/packages/cli/src/cli/commands/prepare.ts @@ -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"; @@ -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); - */ }, }); diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts index dc4dd21..8ff321d 100644 --- a/packages/cli/src/config/index.ts +++ b/packages/cli/src/config/index.ts @@ -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: @@ -8,4 +8,3 @@ export function defineConfig< >(config: ApibaraConfig): ApibaraConfig { return config; } - diff --git a/packages/cli/src/core/apibara.ts b/packages/cli/src/core/apibara.ts index 029b447..52bca23 100644 --- a/packages/cli/src/core/apibara.ts +++ b/packages/cli/src/core/apibara.ts @@ -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 = {}, diff --git a/packages/cli/src/core/build/prepare.ts b/packages/cli/src/core/build/prepare.ts index 7306e6c..782e394 100644 --- a/packages/cli/src/core/build/prepare.ts +++ b/packages/cli/src/core/build/prepare.ts @@ -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); } diff --git a/packages/cli/src/core/build/prod.ts b/packages/cli/src/core/build/prod.ts index 7e4703a..2b05115 100644 --- a/packages/cli/src/core/build/prod.ts +++ b/packages/cli/src/core/build/prod.ts @@ -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, @@ -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; diff --git a/packages/cli/src/core/build/types.ts b/packages/cli/src/core/build/types.ts index 0f25bf0..667273b 100644 --- a/packages/cli/src/core/build/types.ts +++ b/packages/cli/src/core/build/types.ts @@ -1,23 +1,152 @@ +import fsp from "node:fs/promises"; +import type { Apibara } from "apibara/types"; +import consola from "consola"; +import defu from "defu"; +import { dirname, isAbsolute, join, relative, resolve } from "pathe"; +import type { TSConfig } from "pkg-types"; import { type JSValue, generateTypes, resolveSchema } from "untyped"; -import type { Apibara } from "../../types/apibara"; export async function writeTypes(apibara: Apibara) { - // TODO write to a file - const type = generateTypes( - await resolveSchema( - Object.fromEntries( - Object.entries(apibara.options.runtimeConfig), - ) as Record, - ), + const typesDir = resolve(apibara.options.buildDir, "types"); + + const config = [ + "// Generated by apibara", + ` +declare module "apibara/types" {`, + apibara.options.typescript.generateRuntimeConfigTypes + ? generateTypes( + await resolveSchema( + Object.fromEntries( + Object.entries(apibara.options.runtimeConfig), + ) as Record, + ), + { + interfaceName: "ApibaraRuntimeConfig", + addExport: false, + addDefaults: false, + allowExtraKeys: false, + indentation: 2, + }, + ) + : "", + "}", + // Makes this a module for augmentation purposes + "export type {}", + ]; + + const declarations = [ + // local apibara augmentations + '/// ', + ]; + + const buildFiles: { path: string; contents: string }[] = []; + + buildFiles.push({ + path: join(typesDir, "apibara-config.d.ts"), + contents: config.join("\n"), + }); + + buildFiles.push({ + path: join(typesDir, "apibara.d.ts"), + contents: declarations.join("\n"), + }); + + if (apibara.options.typescript.generateTsConfig) { + const tsConfigPath = resolve( + apibara.options.buildDir, + apibara.options.typescript.tsconfigPath, + ); + const tsconfigDir = dirname(tsConfigPath); + const tsConfig: TSConfig = defu(apibara.options.typescript.tsConfig, { + compilerOptions: { + forceConsistentCasingInFileNames: true, + strict: apibara.options.typescript.strict, + noEmit: true, + target: "ESNext", + module: "ESNext", + moduleResolution: "Bundler", + allowJs: true, + resolveJsonModule: true, + jsx: "preserve", + allowSyntheticDefaultImports: true, + jsxFactory: "h", + jsxFragmentFactory: "Fragment", + }, + include: [ + relativeWithDot(tsconfigDir, join(typesDir, "apibara.d.ts")).replace( + /^(?=[^.])/, + "./", + ), + ], + }); + + for (const alias in tsConfig.compilerOptions!.paths) { + const paths = tsConfig.compilerOptions!.paths[alias]; + tsConfig.compilerOptions!.paths[alias] = await Promise.all( + paths.map(async (path: string) => { + if (!isAbsolute(path)) { + return path; + } + const stats = await fsp + .stat(path) + .catch(() => null /* file does not exist */); + return relativeWithDot( + tsconfigDir, + stats?.isFile() + ? path.replace(/(?<=\w)\.\w+$/g, "") /* remove extension */ + : path, + ); + }), + ); + } + + tsConfig.include = [ + ...new Set( + tsConfig.include!.map((p) => + isAbsolute(p) ? relativeWithDot(tsconfigDir, p) : p, + ), + ), + ]; + if (tsConfig.exclude) { + tsConfig.exclude = [ + ...new Set( + tsConfig.exclude!.map((p) => + isAbsolute(p) ? relativeWithDot(tsconfigDir, p) : p, + ), + ), + ]; + } + + buildFiles.push({ + path: tsConfigPath, + contents: JSON.stringify(tsConfig, null, 2), + }); + } + + await Promise.all( + buildFiles.map(async (file) => { + const _file = resolve(apibara.options.buildDir, file.path); + await fsp.mkdir(dirname(_file), { recursive: true }); + await fsp.writeFile(_file, file.contents); + }), + ); + + consola.info( + "To leverage type hints within your project, create a tsconfig.json file that extends auto generated types.\n", + ); + consola.log.raw( { - interfaceName: "ApibaraRuntimeConfig", - addExport: false, - addDefaults: false, - allowExtraKeys: false, - indentation: 2, + extends: "./.apibara/types/tsconfig.json", }, + "\n\n", ); - console.log("apibara ---> ", apibara); - console.log("type ---> ", type); + consola.success("Preparation successful!\n\n"); +} + +const RELATIVE_RE = /^\.{1,2}\//; + +export function relativeWithDot(from: string, to: string) { + const rel = relative(from, to); + return RELATIVE_RE.test(rel) ? rel : `./${rel}`; } diff --git a/packages/cli/src/core/config/defaults.ts b/packages/cli/src/core/config/defaults.ts index 445deb4..c3a4526 100644 --- a/packages/cli/src/core/config/defaults.ts +++ b/packages/cli/src/core/config/defaults.ts @@ -1,12 +1,21 @@ // import { defaultSink } from "@apibara/indexer"; -import { type ApibaraConfig } from "apibara/types"; +import type { ApibaraConfig } from "apibara/types"; export const ApibaraDefaults: ApibaraConfig = { rootDir: ".", outputDir: "./dist", + runtimeConfig: {}, - // sink: { - // // default: () => defaultSink(), - // }, hooks: {}, + + buildDir: ".apibara", + + typescript: { + strict: false, + generateTsConfig: true, + generateRuntimeConfigTypes: true, + tsconfigPath: "types/tsconfig.json", + internalPaths: false, + tsConfig: {}, + }, }; diff --git a/packages/cli/src/core/config/loader.ts b/packages/cli/src/core/config/loader.ts index e20ec97..ac2b0de 100644 --- a/packages/cli/src/core/config/loader.ts +++ b/packages/cli/src/core/config/loader.ts @@ -1,10 +1,10 @@ -import { loadConfig, watchConfig } from "c12"; -import { klona } from "klona/full"; import type { ApibaraConfig, ApibaraOptions, LoadConfigOptions, } from "apibara/types"; +import { loadConfig, watchConfig } from "c12"; +import { klona } from "klona/full"; 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/core/config/resolvers/paths.resolver.ts b/packages/cli/src/core/config/resolvers/paths.resolver.ts index ec59c70..329200b 100644 --- a/packages/cli/src/core/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 "apibara/types"; +import { resolve } from "pathe"; export async function resolvePathOptions(options: ApibaraOptions) { options.rootDir = resolve(options.rootDir || "."); diff --git a/packages/cli/src/core/config/resolvers/preset.resolver.ts b/packages/cli/src/core/config/resolvers/preset.resolver.ts index 372ce8f..7af24d4 100644 --- a/packages/cli/src/core/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 "apibara/types"; +import defu from "defu"; export async function presetResolver(options: ApibaraOptions) { if (options.preset && options.presets?.[options.preset]) { diff --git a/packages/cli/src/core/index.ts b/packages/cli/src/core/index.ts index 4a707e5..926eb04 100644 --- a/packages/cli/src/core/index.ts +++ b/packages/cli/src/core/index.ts @@ -1,3 +1,4 @@ export { createApibara } from "./apibara"; export { build } from "./build/build"; export { prepare } from "./build/prepare"; +export { writeTypes } from "./build/types"; diff --git a/packages/cli/src/hooks/useRuntimeConfig.ts b/packages/cli/src/hooks/useRuntimeConfig.ts index e0ed7f1..b0cd157 100644 --- a/packages/cli/src/hooks/useRuntimeConfig.ts +++ b/packages/cli/src/hooks/useRuntimeConfig.ts @@ -1,3 +1,5 @@ -export function useRuntimeConfig() { +import type { ApibaraRuntimeConfig } from "apibara/types"; + +export function useRuntimeConfig(): ApibaraRuntimeConfig { return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG || "{}"); } diff --git a/packages/cli/src/rollup/config.ts b/packages/cli/src/rollup/config.ts index e1bcc12..1eeed19 100644 --- a/packages/cli/src/rollup/config.ts +++ b/packages/cli/src/rollup/config.ts @@ -4,9 +4,9 @@ import commonjs from "@rollup/plugin-commonjs"; import json from "@rollup/plugin-json"; import { nodeResolve } from "@rollup/plugin-node-resolve"; import typescript from "@rollup/plugin-typescript"; +import type { Apibara, RollupConfig } from "apibara/types"; import fsExtra from "fs-extra"; import { basename, join } from "pathe"; -import type { Apibara, RollupConfig } from "apibara/types"; export const getRollupConfig = ( apibara: Apibara, @@ -36,9 +36,9 @@ export const getRollupConfig = ( // Check if the indexers directory exists and is not empty let indexerFiles: string[] = []; try { - indexerFiles = fsExtra.readdirSync(indexerDir).filter((file) => - file.endsWith(".indexer.ts"), - ); + indexerFiles = fsExtra + .readdirSync(indexerDir) + .filter((file) => file.endsWith(".indexer.ts")); if (indexerFiles.length === 0) { console.warn(`No indexer files found in ${indexerDir}`); } @@ -56,7 +56,8 @@ export const getRollupConfig = ( // Generate main.ts content const mainContent = ` import { createClient } from "@apibara/protocol"; -import { run } from "@apibara/indexer"; +import { createIndexer, run } from "@apibara/indexer"; +import consola from "consola"; import { defineCommand, runMain } from "citty"; import config from './${configPath}'; @@ -64,10 +65,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({ @@ -112,19 +113,19 @@ const command = defineCommand({ return; } - const indexerFactory = indexers[name]; - const indexer = typeof indexerFactory === 'function' - ? await indexerFactory(runtimeConfig) - : indexerFactory; + const indexerConfig = indexers[name] + const indexer = typeof indexerConfig === 'function' + ? await createIndexer(indexerConfig(runtimeConfig)) + : createIndexer(indexerConfig); const client = createClient(indexer.streamConfig, indexer.options.streamUrl); const sink = sinkFunction(); try { - console.log("Running Indexer: ", name); + consola.log("Running Indexer: ", name); await run(client, indexer, sink); } catch (error) { - console.error(\`Error in indexer \${name}:\`, error); + consola.error(\`Error in indexer \${name}:\`, error); } })); }, @@ -169,6 +170,7 @@ runMain(command); }, }, ], + treeshake: true, external: [ ...builtinModules, "@apibara/indexer", diff --git a/packages/cli/src/types/config.ts b/packages/cli/src/types/config.ts index ae195ea..7910c18 100644 --- a/packages/cli/src/types/config.ts +++ b/packages/cli/src/types/config.ts @@ -7,6 +7,7 @@ import type { } from "c12"; import type { WatchOptions } from "chokidar"; import type { NestedHooks } from "hookable"; +import type { TSConfig } from "pkg-types"; import type { DeepPartial } from "./_utils"; import type { ApibaraHooks } from "./hooks"; import type { RollupConfig } from "./rollup"; @@ -66,6 +67,7 @@ export interface ApibaraOptions< debug: boolean; runtimeConfig: R; rootDir: string; + buildDir: string; outputDir: string; // Dev dev: boolean; @@ -77,4 +79,15 @@ export interface ApibaraOptions< rollupConfig?: RollupConfig; entry: string; minify: boolean; + + // Advanced + typescript: { + strict?: boolean; + internalPaths?: boolean; + generateRuntimeConfigTypes?: boolean; + generateTsConfig?: boolean; + /** the path of the generated `tsconfig.json`, relative to buildDir */ + tsconfigPath: string; + tsConfig?: Partial; + }; } diff --git a/packages/cli/src/types/index.ts b/packages/cli/src/types/index.ts index a3ff743..def4fc4 100644 --- a/packages/cli/src/types/index.ts +++ b/packages/cli/src/types/index.ts @@ -3,3 +3,4 @@ export * from "./_utils"; export * from "./config"; export * from "./hooks"; export * from "./rollup"; +export * from "./runtime"; diff --git a/packages/cli/src/types/runtime.ts b/packages/cli/src/types/runtime.ts new file mode 100644 index 0000000..6a54e4a --- /dev/null +++ b/packages/cli/src/types/runtime.ts @@ -0,0 +1 @@ +export type ApibaraRuntimeConfig = Record; diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 42d5044..8d7acb3 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -5,28 +5,15 @@ "declarationDir": "dist", "noEmit": false, "rootDir": "src", - "types": [ - "node" - ], + "types": ["node"], "paths": { - "apibara/cli": [ - "./src/cli" - ], - "apibara/config": [ - "./src/config" - ], - "apibara/core": [ - "./src/core" - ], - "apibara/rollup": [ - "./src/rollup" - ], - "apibara/types": [ - "./src/types" - ] + "apibara/cli": ["./src/cli"], + "apibara/config": ["./src/config"], + "apibara/core": ["./src/core"], + "apibara/rollup": ["./src/rollup"], + "apibara/types": ["./src/types"], + "apibara/hooks": ["./src/hooks"] } }, - "include": [ - "src/" - ] -} \ No newline at end of file + "include": ["src/"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d3f127f..6f2b11d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -354,6 +354,9 @@ importers: pathe: specifier: ^1.1.2 version: 1.1.2 + pkg-types: + specifier: ^1.1.3 + version: 1.1.3 rollup: specifier: ^4.18.1 version: 4.18.1 @@ -2709,7 +2712,7 @@ packages: ohash: 1.1.3 pathe: 1.1.2 perfect-debounce: 1.0.0 - pkg-types: 1.1.1 + pkg-types: 1.1.3 rc9: 2.1.2 dev: false @@ -4146,7 +4149,7 @@ packages: engines: {node: '>=14'} dependencies: mlly: 1.7.0 - pkg-types: 1.1.1 + pkg-types: 1.1.3 dev: true /locate-path@5.0.0: @@ -4374,7 +4377,7 @@ packages: mlly: 1.7.0 mri: 1.2.0 pathe: 1.1.2 - pkg-types: 1.1.1 + pkg-types: 1.1.3 postcss: 8.4.38 postcss-nested: 6.0.1(postcss@8.4.38) semver: 7.6.0 @@ -4386,17 +4389,17 @@ packages: dependencies: acorn: 8.11.3 pathe: 1.1.2 - pkg-types: 1.1.1 + pkg-types: 1.1.3 ufo: 1.5.3 + dev: true /mlly@1.7.1: resolution: {integrity: sha512-rrVRZRELyQzrIUAVMHxP97kv+G786pHmOKzuFII8zDYahFBS7qnHh2AlYSl1GAHhaMPCz6/oHjVMcfFYgFYHgA==} dependencies: acorn: 8.11.3 pathe: 1.1.2 - pkg-types: 1.1.1 + pkg-types: 1.1.3 ufo: 1.5.3 - dev: false /module-details-from-path@1.0.3: resolution: {integrity: sha512-ySViT69/76t8VhE1xXHK6Ch4NcDd26gx0MzKXLO+F7NOtnqH68d9zF94nT8ZWSxXh8ELOERsnJO/sWt1xZYw5A==} @@ -4497,7 +4500,7 @@ packages: consola: 3.2.3 execa: 8.0.1 pathe: 1.1.2 - pkg-types: 1.1.1 + pkg-types: 1.1.3 ufo: 1.5.3 dev: false @@ -4678,11 +4681,11 @@ packages: find-up: 4.1.0 dev: true - /pkg-types@1.1.1: - resolution: {integrity: sha512-ko14TjmDuQJ14zsotODv7dBlwxKhUKQEhuhmbqo1uCi9BB0Z2alo/wAXg6q1dTR5TyuqYyWhjtfe/Tsh+X28jQ==} + /pkg-types@1.1.3: + resolution: {integrity: sha512-+JrgthZG6m3ckicaOB74TwQ+tBWsFl3qVQg7mN8ulwSOElJ7gBhKzj2VkCPnZ4NlF6kEquYU+RIYNVAvzd54UA==} dependencies: confbox: 0.1.7 - mlly: 1.7.0 + mlly: 1.7.1 pathe: 1.1.2 /possible-typed-array-names@1.0.0: @@ -5903,7 +5906,7 @@ packages: mkdist: 1.5.1(typescript@5.4.5) mlly: 1.7.0 pathe: 1.1.2 - pkg-types: 1.1.1 + pkg-types: 1.1.3 pretty-bytes: 6.1.1 rollup: 3.29.4 rollup-plugin-dts: 6.1.1(rollup@3.29.4)(typescript@5.4.5)