apibara 2.1.0-beta.1 → 2.1.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/chunks/add.mjs +12 -7
  2. package/dist/chunks/dev.mjs +25 -6
  3. package/dist/chunks/init.mjs +3 -7
  4. package/dist/chunks/start.mjs +2 -1
  5. package/dist/core/index.mjs +85 -42
  6. package/dist/create/index.d.mts +2 -1
  7. package/dist/create/index.d.ts +2 -1
  8. package/dist/create/index.mjs +185 -121
  9. package/dist/hooks/index.mjs +3 -1
  10. package/dist/rolldown/index.d.mts +7 -0
  11. package/dist/rolldown/index.d.ts +7 -0
  12. package/dist/rolldown/index.mjs +126 -0
  13. package/dist/runtime/dev.mjs +3 -0
  14. package/dist/runtime/internal/app.d.ts +1 -1
  15. package/dist/runtime/internal/app.mjs +12 -11
  16. package/dist/runtime/start.mjs +5 -0
  17. package/dist/shared/apibara.af330c7d.mjs +15 -0
  18. package/dist/types/index.d.mts +18 -15
  19. package/dist/types/index.d.ts +18 -15
  20. package/package.json +13 -15
  21. package/src/cli/commands/add.ts +12 -6
  22. package/src/cli/commands/dev.ts +27 -5
  23. package/src/cli/commands/init.ts +3 -7
  24. package/src/cli/commands/start.ts +1 -0
  25. package/src/core/build/build.ts +13 -5
  26. package/src/core/build/dev.ts +44 -23
  27. package/src/core/build/error.ts +9 -14
  28. package/src/core/build/prod.ts +15 -10
  29. package/src/core/build/types.ts +8 -0
  30. package/src/core/config/defaults.ts +3 -0
  31. package/src/core/config/resolvers/runtime-config.resolver.ts +21 -2
  32. package/src/core/config/update.ts +1 -1
  33. package/src/create/add.ts +26 -12
  34. package/src/create/constants.ts +9 -10
  35. package/src/create/init.ts +28 -14
  36. package/src/create/templates.ts +154 -118
  37. package/src/create/utils.ts +10 -0
  38. package/src/hooks/useRuntimeConfig.ts +2 -1
  39. package/src/rolldown/config.ts +108 -0
  40. package/src/rolldown/index.ts +2 -0
  41. package/src/rolldown/plugins/config.ts +25 -0
  42. package/src/{rollup → rolldown}/plugins/indexers.ts +3 -3
  43. package/src/runtime/dev.ts +3 -0
  44. package/src/runtime/internal/app.ts +30 -13
  45. package/src/runtime/start.ts +5 -0
  46. package/src/types/config.ts +12 -7
  47. package/src/types/hooks.ts +8 -5
  48. package/src/types/index.ts +1 -1
  49. package/src/types/rolldown.ts +5 -0
  50. package/src/types/virtual/config.d.ts +4 -1
  51. package/src/types/virtual/indexers.d.ts +4 -1
  52. package/src/utils/helper.ts +15 -0
  53. package/dist/rollup/index.d.mts +0 -6
  54. package/dist/rollup/index.d.ts +0 -6
  55. package/dist/rollup/index.mjs +0 -150
  56. package/src/rollup/config.ts +0 -87
  57. package/src/rollup/index.ts +0 -2
  58. package/src/rollup/plugins/config.ts +0 -12
  59. package/src/rollup/plugins/esm-shim.ts +0 -69
  60. package/src/types/rollup.ts +0 -8
@@ -1,31 +1,36 @@
1
- import type { Apibara, RollupConfig } from "apibara/types";
1
+ import type { Apibara } from "apibara/types";
2
2
  import { colors } from "consola/utils";
3
- import { type OutputOptions, rollup } from "rollup";
3
+ import * as rolldown from "rolldown";
4
4
 
5
5
  export async function buildProduction(
6
6
  apibara: Apibara,
7
- rollupConfig: RollupConfig,
7
+ rolldownConfig: rolldown.RolldownOptions,
8
8
  ) {
9
9
  apibara.logger.start(
10
10
  `Building ${colors.cyan(apibara.indexers.length)} indexers`,
11
11
  );
12
12
 
13
+ const startTime = Date.now();
14
+
13
15
  try {
14
- const bundle = await rollup(rollupConfig);
16
+ const bundle = await rolldown.rolldown(rolldownConfig);
15
17
 
16
- if (Array.isArray(rollupConfig.output)) {
17
- for (const outputOptions of rollupConfig.output) {
18
+ if (Array.isArray(rolldownConfig.output)) {
19
+ for (const outputOptions of rolldownConfig.output) {
18
20
  await bundle.write(outputOptions);
19
21
  }
20
- } else if (rollupConfig.output) {
21
- await bundle.write(rollupConfig.output as OutputOptions);
22
+ } else if (rolldownConfig.output) {
23
+ await bundle.write(rolldownConfig.output as rolldown.OutputOptions);
22
24
  } else {
23
- throw new Error("No output options specified in Rollup config");
25
+ throw new Error("No output options specified in Rolldown config");
24
26
  }
25
27
 
26
28
  await bundle.close();
27
29
 
28
- apibara.logger.success("Build succeeded!");
30
+ const endTime = Date.now();
31
+ const duration = endTime - startTime;
32
+
33
+ apibara.logger.success(`Build succeeded in ${duration}ms`);
29
34
  apibara.logger.info(
30
35
  `You can start the indexers with ${colors.cyan("apibara start")}`,
31
36
  );
@@ -5,6 +5,14 @@ import { type JSValue, generateTypes, resolveSchema } from "untyped";
5
5
  import { prettyPath } from "../path";
6
6
 
7
7
  export async function writeTypes(apibara: Apibara) {
8
+ // Check if the config file has a TypeScript extension so we assume it's a TypeScript project
9
+ const isTypeScript = apibara.options._c12.configFile?.endsWith(".ts");
10
+
11
+ if (!isTypeScript) {
12
+ // If it's not a TypeScript project, we don't need to generate the types
13
+ return;
14
+ }
15
+
8
16
  const typesDir = resolve(apibara.options.buildDir, "types");
9
17
 
10
18
  const config = [
@@ -14,4 +14,7 @@ export const ApibaraDefaults: ApibaraConfig = {
14
14
  generateRuntimeConfigTypes: true,
15
15
  internalPaths: false,
16
16
  },
17
+
18
+ node: true,
19
+ exportConditions: ["node"],
17
20
  };
@@ -1,6 +1,25 @@
1
1
  import type { ApibaraOptions } from "apibara/types";
2
+ import { serialize } from "../../../utils/helper";
2
3
 
3
4
  export async function resolveRuntimeConfigOptions(options: ApibaraOptions) {
4
- options.runtimeConfig = { ...options.runtimeConfig };
5
- process.env.APIBARA_RUNTIME_CONFIG = JSON.stringify(options.runtimeConfig);
5
+ const { preset, presets } = options;
6
+ let runtimeConfig: Record<string, unknown> = { ...options.runtimeConfig };
7
+
8
+ if (preset) {
9
+ if (presets === undefined) {
10
+ throw new Error(
11
+ `Specified preset "${preset}" but no presets were defined`,
12
+ );
13
+ }
14
+
15
+ if (presets[preset] === undefined) {
16
+ throw new Error(`Specified preset "${preset}" but it was not defined`);
17
+ }
18
+
19
+ const presetValue = presets[preset] as {
20
+ runtimeConfig: Record<string, unknown>;
21
+ };
22
+ runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
23
+ }
24
+ process.env.APIBARA_RUNTIME_CONFIG = serialize(runtimeConfig);
6
25
  }
@@ -4,6 +4,6 @@ export async function updateApibaraConfig(
4
4
  apibara: Apibara,
5
5
  _config: ApibaraDynamicConfig,
6
6
  ) {
7
- await apibara.hooks.callHook("rollup:reload");
7
+ await apibara.hooks.callHook("rolldown:reload");
8
8
  apibara.logger.success("Apibara config hot reloaded!");
9
9
  }
package/src/create/add.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import path from "node:path";
1
2
  import consola from "consola";
2
3
  import prompts from "prompts";
3
4
  import { cyan, red, reset } from "./colors";
@@ -18,6 +19,7 @@ import {
18
19
  import type { Chain, IndexerOptions, Network, Storage } from "./types";
19
20
  import {
20
21
  cancelOperation,
22
+ checkFileExists,
21
23
  convertKebabToCamelCase,
22
24
  getApibaraConfigLanguage,
23
25
  getPackageManager,
@@ -35,6 +37,7 @@ type Options = {
35
37
  argNetwork?: string;
36
38
  argStorage?: string;
37
39
  argDnaUrl?: string;
40
+ argRootDir?: string;
38
41
  };
39
42
 
40
43
  export async function addIndexer({
@@ -43,8 +46,10 @@ export async function addIndexer({
43
46
  argNetwork,
44
47
  argStorage,
45
48
  argDnaUrl,
49
+ argRootDir,
46
50
  }: Options) {
47
- const configExists = hasApibaraConfig(process.cwd());
51
+ const cwd = path.join(process.cwd(), argRootDir ?? ".");
52
+ const configExists = hasApibaraConfig(cwd);
48
53
 
49
54
  if (!configExists) {
50
55
  consola.error("No apibara.config found in the current directory.");
@@ -72,7 +77,7 @@ export async function addIndexer({
72
77
  }
73
78
  }
74
79
 
75
- const language = getApibaraConfigLanguage(process.cwd());
80
+ const language = getApibaraConfigLanguage(cwd);
76
81
 
77
82
  validateIndexerId(argIndexerId, true);
78
83
  validateChain(argChain, true);
@@ -88,8 +93,19 @@ export async function addIndexer({
88
93
  message: reset("Indexer ID:"),
89
94
  initial: argIndexerId ?? "my-indexer",
90
95
  validate: (id) =>
91
- validateIndexerId(id) ||
92
- "Invalid indexer ID cannot be empty and must be in kebab-case format",
96
+ validateIndexerId(id)
97
+ ? checkFileExists(
98
+ path.join(
99
+ cwd,
100
+ "indexers",
101
+ `${id}.indexer.${language === "typescript" ? "ts" : "js"}`,
102
+ ),
103
+ ).then(({ exists }) =>
104
+ exists
105
+ ? `Indexer ${cyan(`${id}.indexer.${language === "typescript" ? "ts" : "js"}`)} already exists`
106
+ : true,
107
+ )
108
+ : "Invalid indexer ID, it cannot be empty and must be in kebab-case format",
93
109
  },
94
110
  {
95
111
  type: argChain ? null : "select",
@@ -184,7 +200,7 @@ export async function addIndexer({
184
200
  const pkgManager = getPackageManager();
185
201
 
186
202
  const options: IndexerOptions = {
187
- cwd: process.cwd(),
203
+ cwd: cwd,
188
204
  indexerFileId,
189
205
  indexerId: convertKebabToCamelCase(indexerFileId),
190
206
  chain: (argChain as Chain) ?? prompt_chain?.name!,
@@ -195,13 +211,13 @@ export async function addIndexer({
195
211
  packageManager: pkgManager.name,
196
212
  };
197
213
 
198
- updateApibaraConfigFile(options);
214
+ await updateApibaraConfigFile(options);
199
215
 
200
216
  consola.success(
201
217
  `Updated ${cyan("apibara.config." + (language === "typescript" ? "ts" : "js"))}`,
202
218
  );
203
219
 
204
- updatePackageJson(options);
220
+ await updatePackageJson(options);
205
221
 
206
222
  consola.success(`Updated ${cyan("package.json")}`);
207
223
 
@@ -215,11 +231,9 @@ export async function addIndexer({
215
231
 
216
232
  console.log();
217
233
 
234
+ const baseCommand = `${options.packageManager} install`;
235
+ const tsCommand = `${baseCommand} && ${options.packageManager} run prepare`;
218
236
  consola.info(
219
- `Before running the indexer, run ${cyan(`${options.packageManager} run install`)}${
220
- language === "typescript"
221
- ? " & " + cyan(`${options.packageManager} run prepare`)
222
- : ""
223
- }`,
237
+ `Before running the indexer, run ${cyan(language === "typescript" ? tsCommand : baseCommand)}`,
224
238
  );
225
239
  }
@@ -66,17 +66,17 @@ export const storages: StorageDataType[] = [
66
66
 
67
67
  export const packageVersions = {
68
68
  // Required Dependencies
69
- apibara: "^2.1.0-beta.1",
70
- "@apibara/indexer": "^2.1.0-beta.1",
71
- "@apibara/protocol": "^2.1.0-beta.1",
69
+ apibara: "next",
70
+ "@apibara/indexer": "next",
71
+ "@apibara/protocol": "next",
72
72
  // Chain Dependencies
73
- "@apibara/evm": "^2.1.0-beta.1",
74
- "@apibara/beaconchain": "^2.1.0-beta.1",
75
- "@apibara/starknet": "^2.1.0-beta.1",
73
+ "@apibara/evm": "next",
74
+ "@apibara/beaconchain": "next",
75
+ "@apibara/starknet": "next",
76
76
  // Storage Dependencies
77
- "@apibara/plugin-drizzle": "^2.1.0-beta.1",
78
- "@apibara/plugin-mongo": "^2.1.0-beta.1",
79
- "@apibara/plugin-sqlite": "^2.1.0-beta.1",
77
+ "@apibara/plugin-drizzle": "next",
78
+ "@apibara/plugin-mongo": "next",
79
+ "@apibara/plugin-sqlite": "next",
80
80
  // Postgres Dependencies
81
81
  "@electric-sql/pglite": "^0.2.17",
82
82
  "drizzle-orm": "^0.37.0",
@@ -85,7 +85,6 @@ export const packageVersions = {
85
85
  "drizzle-kit": "^0.29.0",
86
86
  // Typescript Dependencies
87
87
  typescript: "^5.6.2",
88
- "@rollup/plugin-typescript": "^11.1.6",
89
88
  "@types/node": "^20.5.2",
90
89
  };
91
90
 
@@ -5,6 +5,7 @@ import prompts from "prompts";
5
5
  import { addIndexer } from "./add";
6
6
  import { cyan, green } from "./colors";
7
7
  import {
8
+ createGitIgnoreFile,
8
9
  generateApibaraConfig,
9
10
  generatePackageJson,
10
11
  generateTsConfig,
@@ -13,6 +14,7 @@ import type { Language } from "./types";
13
14
  import {
14
15
  cancelOperation,
15
16
  emptyDir,
17
+ formatFile,
16
18
  getLanguageFromAlias,
17
19
  getPackageManager,
18
20
  isEmpty,
@@ -121,30 +123,42 @@ export async function initializeProject({
121
123
  consola.info(`Initializing project in ${argTargetDir}\n\n`);
122
124
 
123
125
  // Generate package.json
126
+ const packageJsonPath = path.join(root, "package.json");
124
127
  const packageJson = generatePackageJson(isTs);
125
128
  fs.writeFileSync(
126
- path.join(root, "package.json"),
129
+ packageJsonPath,
127
130
  JSON.stringify(packageJson, null, 2) + "\n",
128
131
  );
129
- consola.success("Created ", cyan("package.json"));
132
+ await formatFile(packageJsonPath);
133
+ consola.success("Created", cyan("package.json"));
130
134
 
131
135
  // Generate tsconfig.json if TypeScript
132
136
  if (isTs) {
137
+ const tsConfigPath = path.join(root, "tsconfig.json");
133
138
  const tsConfig = generateTsConfig();
134
- fs.writeFileSync(
135
- path.join(root, "tsconfig.json"),
136
- JSON.stringify(tsConfig, null, 2) + "\n",
137
- );
138
- consola.success("Created ", cyan("tsconfig.json"));
139
+ fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2) + "\n");
140
+ await formatFile(tsConfigPath);
141
+ consola.success("Created", cyan("tsconfig.json"));
139
142
  }
140
143
 
144
+ const apibaraConfigPath = path.join(root, `apibara.config.${configExt}`);
141
145
  // Generate apibara.config
142
146
  const apibaraConfig = generateApibaraConfig(isTs);
143
- fs.writeFileSync(
144
- path.join(root, `apibara.config.${configExt}`),
145
- apibaraConfig,
146
- );
147
- consola.success("Created ", cyan(`apibara.config.${configExt}`), "\n\n");
147
+ fs.writeFileSync(apibaraConfigPath, apibaraConfig);
148
+ await formatFile(apibaraConfigPath);
149
+ consola.success("Created", cyan(`apibara.config.${configExt}`));
150
+
151
+ // Create "indexers" directory if not exists
152
+ const indexersDir = path.join(root, "indexers");
153
+ if (!fs.existsSync(indexersDir)) {
154
+ fs.mkdirSync(indexersDir, { recursive: true });
155
+ consola.success(`Created ${cyan("indexers")} directory`);
156
+ }
157
+
158
+ await createGitIgnoreFile(root);
159
+
160
+ console.log("\n");
161
+
148
162
  consola.ready(green("Project initialized successfully"));
149
163
 
150
164
  console.log();
@@ -152,12 +166,12 @@ export async function initializeProject({
152
166
  if (!argNoCreateIndexer) {
153
167
  consola.info("Let's create an indexer\n");
154
168
 
155
- await addIndexer({});
169
+ await addIndexer({ argRootDir: argTargetDir });
156
170
  } else {
157
171
  const pkgManager = getPackageManager();
158
172
  consola.info(
159
173
  "Run ",
160
- green(`${pkgManager.name} run install`),
174
+ green(`${pkgManager.name} install`),
161
175
  " to install all dependencies",
162
176
  );
163
177
  }