Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions change/apibara-ada1fb63-906b-4e5d-adf0-79c86ee61a13.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "cli: fix prod mode for runtime values",
"packageName": "apibara",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkForUnknownArgs } from "apibara/common";
import { addIndexer } from "apibara/create";
import { defineCommand } from "citty";
import { checkForUnknownArgs } from "../common";

export default defineCommand({
meta: {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { checkForUnknownArgs, commonArgs } from "apibara/common";
import { build, createApibara, prepare, writeTypes } from "apibara/core";
import { runtimeDir } from "apibara/runtime/meta";
import { defineCommand } from "citty";
import { join, resolve } from "pathe";
import { checkForUnknownArgs, commonArgs } from "../common";

export default defineCommand({
meta: {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type ChildProcess, spawn } from "node:child_process";
import { checkForUnknownArgs, commonArgs } from "apibara/common";
import { build, createApibara, prepare, writeTypes } from "apibara/core";
import { runtimeDir } from "apibara/runtime/meta";
import type { Apibara } from "apibara/types";
import { defineCommand } from "citty";
import { colors } from "consola/utils";
import { join, resolve } from "pathe";
import { blueBright, gray } from "../../create/colors";
import { checkForUnknownArgs, commonArgs } from "../common";

// Hot module reloading key regex
// for only runtimeConfig.*, presets.* keys & preset key
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkForUnknownArgs } from "apibara/common";
import { initializeProject } from "apibara/create";
import { defineCommand } from "citty";
import { checkForUnknownArgs } from "../common";

export default defineCommand({
meta: {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/cli/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { checkForUnknownArgs, commonArgs } from "apibara/common";
import { createApibara, writeTypes } from "apibara/core";
import {} from "apibara/types";
import { defineCommand } from "citty";
import { resolve } from "pathe";
import { checkForUnknownArgs, commonArgs } from "../common";

export default defineCommand({
meta: {
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { spawn } from "node:child_process";
import { checkForUnknownArgs, commonArgs } from "apibara/common";
import { createApibara } from "apibara/core";
import { defineCommand } from "citty";
import { colors } from "consola/utils";
import fse from "fs-extra";
import { resolve } from "pathe";
import { checkForUnknownArgs, commonArgs } from "../common";

export default defineCommand({
meta: {
Expand Down Expand Up @@ -50,7 +50,15 @@ export default defineCommand({

await apibara.close();

const childArgs = [entry, "start", "--indexer", indexer];
const childArgs = [
entry,
"start",
"--indexer",
indexer,
// important: this is required to run the indexer with apibara cli to load runtime values correctly
"--no-standalone",
...(preset ? ["--preset", preset] : []),
];

const childProcess = spawn("node", childArgs, {
stdio: "inherit",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/write-project-info.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { spawn } from "node:child_process";
import { join } from "node:path";
import { checkForUnknownArgs, commonArgs } from "apibara/common";
import { build, createApibara, prepare, writeTypes } from "apibara/core";
import { runtimeDir } from "apibara/runtime/meta";
import { defineCommand } from "citty";
import consola from "consola";
import { resolve } from "pathe";
import { checkForUnknownArgs, commonArgs } from "../common";

export default defineCommand({
meta: {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions packages/cli/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./constants";
export * from "./helper";
export * from "./cli";
3 changes: 2 additions & 1 deletion packages/cli/src/rolldown/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from "rolldown";
import { indexers } from "./plugins/indexers";
import { instrumentation } from "./plugins/instrumentation";
import { staticConfig } from "./plugins/static-config";

const runtimeDependencies = [
"better-sqlite3",
Expand Down Expand Up @@ -77,8 +78,8 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
},
);

rolldownConfig.plugins?.push(staticConfig(apibara));
rolldownConfig.plugins?.push(instrumentation(apibara));
rolldownConfig.plugins?.push(indexers(apibara));

return rolldownConfig;
}
21 changes: 21 additions & 0 deletions packages/cli/src/rolldown/plugins/static-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import virtual from "@rollup/plugin-virtual";
import { USER_ENV_APIBARA_RUNTIME_CONFIG } from "apibara/common";
import type { Apibara } from "apibara/types";
import type { RolldownPluginOption } from "rolldown";

export function staticConfig(apibara: Apibara) {
const presetString = apibara.options.preset ?? "";
const presetsStringified = JSON.stringify(apibara.options.presets ?? {});
const runtimeConfigStringified = JSON.stringify(
apibara.options.runtimeConfig ?? {},
);

return virtual({
"#apibara-internal-virtual/static-config": `
export const preset = ${presetString ? `"${presetString}"` : "undefined"};
export const presets = ${presetsStringified};
export const runtimeConfig = ${runtimeConfigStringified};
export const userEnvRuntimeConfig = JSON.parse(process.env.${USER_ENV_APIBARA_RUNTIME_CONFIG} ?? "{}");
`,
}) as RolldownPluginOption;
}
48 changes: 44 additions & 4 deletions packages/cli/src/runtime/start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { runWithReconnect } from "@apibara/indexer";
import { getRuntimeDataFromEnv } from "apibara/common";
import {
checkForUnknownArgs,
getProcessedRuntimeConfig,
getRuntimeDataFromEnv,
} from "apibara/common";
import { defineCommand, runMain } from "citty";
import consola from "consola";
import { blueBright } from "picocolors";
import { register } from "#apibara-internal-virtual/instrumentation";
// used when running with node .apibara/build/start.mjs as these values are made static during build time (except userEnvRuntimeConfig)
import {
preset as originalPreset,
presets,
runtimeConfig,
userEnvRuntimeConfig,
} from "#apibara-internal-virtual/static-config";
import { createAuthenticatedClient, createIndexer } from "./internal/app";

const startCommand = defineCommand({
Expand All @@ -17,18 +28,47 @@ const startCommand = defineCommand({
description: "Indexer name",
required: true,
},
preset: {
type: "string",
description: "Preset name",
},
standalone: {
type: "boolean",
default: true,
description:
"--standalone: can run the indexer just with node and without apibara cli \n\t\t\t --no-standalone: must run the indexer with apibara cli",
},
},
async run({ args }) {
const { indexer } = args;
async run({ args, cmd }) {
const { indexer, preset: argPreset, standalone } = args;
await checkForUnknownArgs(args, cmd);

const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
let preset: string | undefined;
let processedRuntimeConfig: Record<string, unknown> | undefined;

if (standalone) {
// when user does node .apibara/build/start.mjs
preset = argPreset ?? originalPreset;
processedRuntimeConfig = getProcessedRuntimeConfig({
preset,
presets,
runtimeConfig,
userEnvRuntimeConfig,
});
} else {
// When user does apibara start
const envResult = getRuntimeDataFromEnv();
preset = envResult.preset;
processedRuntimeConfig = envResult.processedRuntimeConfig;
}

const { indexer: indexerInstance, logger } =
createIndexer({
indexerName: indexer,
processedRuntimeConfig,
preset,
}) ?? {};

if (!indexerInstance) {
consola.error(`Specified indexer "${indexer}" but it was not defined`);
process.exit(1);
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/types/virtual/static-config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const preset: string | undefined = undefined;
export const presets: Record<string, unknown> = {};
export const runtimeConfig: Record<string, unknown> = {};
export const userEnvRuntimeConfig: Record<string, unknown> = {};