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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "indexer: show debug logs with useLogger when debug is true",
"packageName": "@apibara/indexer",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/apibara-b87e2be4-ca28-4711-b029-a215cfd726c8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "cli: fix apibara cli not working with .mjs extensions",
"packageName": "apibara",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/cli/src/core/build/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ const getIgnorePatterns = (apibara: Apibara) => [
"**/dist/**",
"**/.turbo/**",
// changes to apibara.config is handled by c12 itself so we dont need rolldown to handle this
"**/apibara.config?(.ts|.js)",
"**/apibara.config?(.ts|.js|.mjs)",
];
2 changes: 1 addition & 1 deletion packages/cli/src/core/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Apibara } from "apibara/types";
import fse from "fs-extra";
import { basename, join } from "pathe";

const INDEXER_EXTENSIONS = [".indexer.ts", ".indexer.js"];
const INDEXER_EXTENSIONS = [".indexer.ts", ".indexer.js", ".indexer.mjs"];

export async function scanIndexers(apibara: Apibara) {
apibara.logger.debug("Scanning indexers");
Expand Down
19 changes: 6 additions & 13 deletions packages/cli/src/create/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function addIndexer({
}
}

const language = getApibaraConfigLanguage(cwd);
const { language, extension } = getApibaraConfigLanguage(cwd);

validateIndexerId(argIndexerId, true);
validateChain(argChain, true);
Expand All @@ -95,14 +95,10 @@ export async function addIndexer({
validate: (id) =>
validateIndexerId(id)
? checkFileExists(
path.join(
cwd,
"indexers",
`${id}.indexer.${language === "typescript" ? "ts" : "js"}`,
),
path.join(cwd, "indexers", `${id}.indexer.${extension}`),
).then(({ exists }) =>
exists
? `Indexer ${cyan(`${id}.indexer.${language === "typescript" ? "ts" : "js"}`)} already exists`
? `Indexer ${cyan(`${id}.indexer.${extension}`)} already exists`
: true,
)
: "Invalid indexer ID, it cannot be empty and must be in kebab-case format",
Expand Down Expand Up @@ -209,23 +205,20 @@ export async function addIndexer({
dnaUrl: argDnaUrl ?? prompt_dnaUrl,
language,
packageManager: pkgManager.name,
extension,
};

await updateApibaraConfigFile(options);

consola.success(
`Updated ${cyan("apibara.config." + (language === "typescript" ? "ts" : "js"))}`,
);
consola.success(`Updated ${cyan(`apibara.config.${extension}`)}`);

await updatePackageJson(options);

consola.success(`Updated ${cyan("package.json")}`);

await createIndexerFile(options);

consola.success(
`Created ${cyan(`${indexerFileId}.indexer.${language === "typescript" ? "ts" : "js"}`)}`,
);
consola.success(`Created ${cyan(`${indexerFileId}.indexer.${extension}`)}`);

await createStorageRelatedFiles(options);

Expand Down
22 changes: 12 additions & 10 deletions packages/cli/src/create/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function createIndexerFile(options: IndexerOptions) {
const indexerFilePath = path.join(
options.cwd,
"indexers",
`${options.indexerFileId}.indexer.${options.language === "typescript" ? "ts" : "js"}`,
`${options.indexerFileId}.indexer.${options.extension}`,
);

const { exists, overwrite } = await checkFileExists(indexerFilePath, {
Expand Down Expand Up @@ -214,11 +214,9 @@ export async function updateApibaraConfigFile({
language,
network,
dnaUrl,
extension,
}: IndexerOptions) {
const pathToConfig = path.join(
cwd,
`apibara.config.${language === "typescript" ? "ts" : "js"}`,
);
const pathToConfig = path.join(cwd, `apibara.config.${extension}`);

const runtimeConfigString = `{
startingBlock: 0,
Expand Down Expand Up @@ -267,12 +265,16 @@ export async function updateApibaraConfigFile({
}

export async function createDrizzleStorageFiles(options: IndexerOptions) {
const { cwd, language, storage, indexerId } = options;
const {
cwd,
language,
storage,
indexerId,
extension: fileExtension,
} = options;

if (storage !== "postgres") return;

const fileExtension = language === "typescript" ? "ts" : "js";

/**
*
*
Expand Down Expand Up @@ -349,7 +351,7 @@ export {};

await formatFile(schemaPath);

consola.success(`Created ${cyan("lib/schema.ts")}`);
consola.success(`Created ${cyan(`lib/${schemaFileName}`)}`);
}

console.log("\n");
Expand All @@ -366,7 +368,7 @@ export {};

${yellow(`
┌──────────────────────────────────────────┐
│ lib/schema.ts
│ lib/schema
└──────────────────────────────────────────┘

import { bigint, pgTable, text, uuid } from "drizzle-orm/pg-core";
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/create/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type Network =

export type Storage = "postgres" | "none";

export type FileExtension = "js" | "ts" | "mjs";

export type IndexerOptions = {
cwd: string;
indexerFileId: string;
Expand All @@ -26,6 +28,7 @@ export type IndexerOptions = {
dnaUrl?: string;
packageManager: string;
language: Language;
extension: FileExtension;
};

export type PkgInfo = {
Expand Down
27 changes: 20 additions & 7 deletions packages/cli/src/create/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as prettier from "prettier";
import prompts from "prompts";
import { blue, cyan, red, yellow } from "./colors";
import { dnaUrls, networks } from "./constants";
import type { Chain, Language, Network, PkgInfo } from "./types";
import type { Chain, FileExtension, Language, Network, PkgInfo } from "./types";

export function isEmpty(path: string) {
const files = fs.readdirSync(path);
Expand All @@ -31,7 +31,8 @@ export function validateLanguage(language?: string, throwError = false) {
language === "typescript" ||
language === "ts" ||
language === "javascript" ||
language === "js"
language === "js" ||
language === "mjs"
) {
return true;
}
Expand All @@ -49,7 +50,7 @@ export function getLanguageFromAlias(alias: string): Language {
if (alias === "ts" || alias === "typescript") {
return "typescript";
}
if (alias === "js" || alias === "javascript") {
if (alias === "js" || alias === "javascript" || alias === "mjs") {
return "javascript";
}

Expand Down Expand Up @@ -184,19 +185,31 @@ export function validateDnaUrl(dnaUrl?: string, throwError = false) {
export function hasApibaraConfig(cwd: string): boolean {
const configPathJS = path.join(cwd, "apibara.config.js");
const configPathTS = path.join(cwd, "apibara.config.ts");
const configPathMJS = path.join(cwd, "apibara.config.mjs");

return fs.existsSync(configPathJS) || fs.existsSync(configPathTS);
return (
fs.existsSync(configPathJS) ||
fs.existsSync(configPathTS) ||
fs.existsSync(configPathMJS)
);
}

export function getApibaraConfigLanguage(cwd: string): Language {
export function getApibaraConfigLanguage(cwd: string): {
language: Language;
extension: FileExtension;
} {
const configPathJS = path.join(cwd, "apibara.config.js");
const configPathTS = path.join(cwd, "apibara.config.ts");
const configPathMJS = path.join(cwd, "apibara.config.mjs");

if (fs.existsSync(configPathMJS)) {
return { language: "javascript", extension: "mjs" };
}
if (fs.existsSync(configPathJS)) {
return "javascript";
return { language: "javascript", extension: "js" };
}
if (fs.existsSync(configPathTS)) {
return "typescript";
return { language: "typescript", extension: "ts" };
}

throw new Error(red("✖") + " No apibara.config found");
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/rolldown/plugins/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { RolldownPluginOption } from "rolldown";
export function instrumentation(apibara: Apibara) {
const instrumentationPath = join(
apibara.options._c12.cwd!,
`instrumentation.${apibara.options._c12.configFile?.endsWith(".ts") ? "ts" : "js"}`,
`instrumentation.${apibara.options._c12.configFile?.endsWith(".ts") ? "ts" : apibara.options._c12.configFile?.endsWith(".mjs") ? "mjs" : "js"}`,
);

if (!existsSync(instrumentationPath)) {
Expand Down
5 changes: 5 additions & 0 deletions packages/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ export async function run<TFilter, TBlock>(
) {
await indexerAsyncContext.callAsync({}, async () => {
const context = useIndexerContext();

if (indexer.options.debug) {
context.debug = true;
}

const middleware = await registerMiddleware(indexer);

const indexerMetrics = createIndexerMetrics();
Expand Down
11 changes: 10 additions & 1 deletion packages/indexer/src/plugins/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type ConsolaInstance, type ConsolaReporter, consola } from "consola";
import {
type ConsolaInstance,
type ConsolaReporter,
LogLevels,
consola,
} from "consola";
import { useIndexerContext } from "../context";
import { defineIndexerPlugin } from "./config";

Expand All @@ -16,6 +21,10 @@ export function logger<TFilter, TBlock, TTxnParams>({
} else {
ctx.logger = consola.create({});
}

if (ctx.debug) {
ctx.logger.level = LogLevels.debug;
}
});
});
}
Expand Down