apibara 2.0.0-beta.9 → 2.1.0-beta.3
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.
- package/dist/chunks/add.mjs +44 -0
- package/dist/chunks/build.mjs +3 -3
- package/dist/chunks/dev.mjs +22 -18
- package/dist/chunks/init.mjs +37 -0
- package/dist/chunks/prepare.mjs +0 -2
- package/dist/chunks/start.mjs +56 -0
- package/dist/cli/index.mjs +5 -1
- package/dist/config/index.d.mts +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/core/index.mjs +61 -97
- package/dist/create/index.d.mts +17 -0
- package/dist/create/index.d.ts +17 -0
- package/dist/create/index.mjs +981 -0
- package/dist/rollup/index.d.mts +2 -1
- package/dist/rollup/index.d.ts +2 -1
- package/dist/rollup/index.mjs +130 -167
- package/dist/runtime/dev.d.ts +3 -0
- package/dist/runtime/dev.mjs +55 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.mjs +2 -0
- package/dist/runtime/internal/app.d.ts +2 -0
- package/dist/runtime/internal/app.mjs +56 -0
- package/dist/runtime/internal/logger.d.ts +14 -0
- package/dist/runtime/internal/logger.mjs +45 -0
- package/dist/runtime/start.d.ts +3 -0
- package/dist/runtime/start.mjs +41 -0
- package/dist/types/index.d.mts +22 -19
- package/dist/types/index.d.ts +22 -19
- package/package.json +35 -13
- package/runtime-meta.d.ts +2 -0
- package/runtime-meta.mjs +7 -0
- package/src/cli/commands/add.ts +44 -0
- package/src/cli/commands/build.ts +5 -3
- package/src/cli/commands/dev.ts +28 -18
- package/src/cli/commands/init.ts +36 -0
- package/src/cli/commands/prepare.ts +0 -2
- package/src/cli/commands/start.ts +61 -0
- package/src/cli/index.ts +3 -0
- package/src/config/index.ts +5 -4
- package/src/core/apibara.ts +4 -2
- package/src/core/build/build.ts +2 -0
- package/src/core/build/dev.ts +1 -0
- package/src/core/build/error.ts +0 -1
- package/src/core/build/prepare.ts +5 -2
- package/src/core/build/prod.ts +10 -6
- package/src/core/build/types.ts +4 -95
- package/src/core/config/defaults.ts +1 -4
- package/src/core/config/loader.ts +1 -0
- package/src/core/config/resolvers/runtime-config.resolver.ts +1 -1
- package/src/core/config/update.ts +2 -3
- package/src/core/path.ts +11 -0
- package/src/core/scan.ts +40 -0
- package/src/create/add.ts +238 -0
- package/src/create/colors.ts +15 -0
- package/src/create/constants.ts +98 -0
- package/src/create/index.ts +2 -0
- package/src/create/init.ts +175 -0
- package/src/create/templates.ts +468 -0
- package/src/create/types.ts +34 -0
- package/src/create/utils.ts +422 -0
- package/src/rollup/config.ts +67 -189
- package/src/rollup/index.ts +1 -0
- package/src/rollup/plugins/config.ts +12 -0
- package/src/rollup/plugins/esm-shim.ts +69 -0
- package/src/rollup/plugins/indexers.ts +17 -0
- package/src/runtime/dev.ts +64 -0
- package/src/runtime/index.ts +2 -0
- package/src/runtime/internal/app.ts +78 -0
- package/src/runtime/internal/logger.ts +70 -0
- package/src/runtime/start.ts +48 -0
- package/src/types/apibara.ts +8 -0
- package/src/types/config.ts +28 -27
- package/src/types/hooks.ts +1 -0
- package/src/types/virtual/config.d.ts +3 -0
- package/src/types/virtual/indexers.d.ts +10 -0
- package/dist/internal/citty/index.d.mts +0 -1
- package/dist/internal/citty/index.d.ts +0 -1
- package/dist/internal/citty/index.mjs +0 -1
- package/dist/internal/consola/index.d.mts +0 -2
- package/dist/internal/consola/index.d.ts +0 -2
- package/dist/internal/consola/index.mjs +0 -1
- package/src/internal/citty/index.ts +0 -1
- package/src/internal/consola/index.ts +0 -1
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path, { basename } from "node:path";
|
|
3
|
+
import * as prettier from "prettier";
|
|
4
|
+
import prompts from "prompts";
|
|
5
|
+
import { blue, cyan, red, yellow } from "./colors";
|
|
6
|
+
import { dnaUrls, networks } from "./constants";
|
|
7
|
+
import type { Chain, Language, Network, PkgInfo } from "./types";
|
|
8
|
+
|
|
9
|
+
export function isEmpty(path: string) {
|
|
10
|
+
const files = fs.readdirSync(path);
|
|
11
|
+
return files.length === 0 || (files.length === 1 && files[0] === ".git");
|
|
12
|
+
}
|
|
13
|
+
export function emptyDir(dir: string) {
|
|
14
|
+
if (!fs.existsSync(dir)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const file of fs.readdirSync(dir)) {
|
|
18
|
+
if (file === ".git") {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
fs.rmSync(path.resolve(dir, file), { recursive: true, force: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function validateLanguage(language?: string, throwError = false) {
|
|
26
|
+
if (!language) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
language === "typescript" ||
|
|
32
|
+
language === "ts" ||
|
|
33
|
+
language === "javascript" ||
|
|
34
|
+
language === "js"
|
|
35
|
+
) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (throwError) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Invalid language ${cyan("(--language | -l)")}: ${red(language)}. Options: ${blue("typescript, ts")} or ${yellow("javascript, js")} | default: ${cyan("typescript")}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getLanguageFromAlias(alias: string): Language {
|
|
49
|
+
if (alias === "ts" || alias === "typescript") {
|
|
50
|
+
return "typescript";
|
|
51
|
+
}
|
|
52
|
+
if (alias === "js" || alias === "javascript") {
|
|
53
|
+
return "javascript";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Invalid language ${cyan("(--language | -l)")}: ${red(alias)}. Options: ${blue("typescript, ts")} or ${yellow("javascript, js")}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function validateIndexerId(indexerId?: string, throwError = false) {
|
|
62
|
+
if (!indexerId) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (!/^[a-z0-9-]+$/.test(indexerId)) {
|
|
66
|
+
if (throwError) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Invalid indexer ID ${cyan("(--indexer-id)")}: ${red(indexerId)}. Indexer ID must contain only lowercase letters, numbers, and hyphens.`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function validateChain(chain?: string, throwError = false) {
|
|
77
|
+
if (!chain) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (chain) {
|
|
81
|
+
if (chain === "starknet" || chain === "ethereum" || chain === "beaconchain")
|
|
82
|
+
return true;
|
|
83
|
+
if (throwError) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Invalid chain ${cyan("(--chain)")}: ${red(chain)}. Chain must be one of ${blue("starknet, ethereum, beaconchain")}.`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function validateNetwork(
|
|
94
|
+
chain?: string,
|
|
95
|
+
network?: string,
|
|
96
|
+
throwError = false,
|
|
97
|
+
) {
|
|
98
|
+
if (!network) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (network === "other") {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (chain) {
|
|
107
|
+
if (chain === "starknet") {
|
|
108
|
+
if (network === "mainnet" || network === "sepolia") {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (throwError) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Invalid network ${cyan("(--network)")}: ${red(network)}. For chain ${blue("starknet")}, network must be one of ${blue("mainnet, sepolia, other")}.`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (chain === "ethereum") {
|
|
119
|
+
if (network === "mainnet" || network === "goerli") {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (throwError) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`Invalid network ${cyan("(--network)")}: ${red(network)}. For chain ${blue("ethereum")}, network must be one of ${blue("mainnet, goerli, other")}.`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (chain === "beaconchain") {
|
|
130
|
+
if (network === "mainnet") {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
if (throwError) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Invalid network ${cyan("(--network)")}: ${red(network)}. For chain ${blue("beaconchain")}, network must be ${blue("mainnet, other")}.`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (networks.find((n) => n.name === network)) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (throwError) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`Invalid network ${cyan("(--network)")}: ${red(network)}. Network must be one of ${blue("mainnet, sepolia, goerli, other")}.`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function validateStorage(storage?: string, throwError = false) {
|
|
155
|
+
if (!storage) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (storage === "postgres" || storage === "none") {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if (throwError) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Invalid storage ${cyan("(--storage)")}: ${red(storage)}. Storage must be one of ${blue("postgres, none")}.`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function validateDnaUrl(dnaUrl?: string, throwError = false) {
|
|
170
|
+
if (!dnaUrl) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (!dnaUrl.startsWith("https://") && !dnaUrl.startsWith("http://")) {
|
|
174
|
+
if (throwError) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
`Invalid DNA URL ${cyan("(--dna-url)")}: ${red(dnaUrl)}. DNA URL must start with ${blue("https:// or http://")}.`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function hasApibaraConfig(cwd: string): boolean {
|
|
185
|
+
const configPathJS = path.join(cwd, "apibara.config.js");
|
|
186
|
+
const configPathTS = path.join(cwd, "apibara.config.ts");
|
|
187
|
+
|
|
188
|
+
return fs.existsSync(configPathJS) || fs.existsSync(configPathTS);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function getApibaraConfigLanguage(cwd: string): Language {
|
|
192
|
+
const configPathJS = path.join(cwd, "apibara.config.js");
|
|
193
|
+
const configPathTS = path.join(cwd, "apibara.config.ts");
|
|
194
|
+
|
|
195
|
+
if (fs.existsSync(configPathJS)) {
|
|
196
|
+
return "javascript";
|
|
197
|
+
}
|
|
198
|
+
if (fs.existsSync(configPathTS)) {
|
|
199
|
+
return "typescript";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
throw new Error(red("✖") + " No apibara.config found");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function getDnaUrl(chain: Chain, network: Network) {
|
|
206
|
+
if (chain === "ethereum") {
|
|
207
|
+
if (network === "mainnet") {
|
|
208
|
+
return dnaUrls.ethereum;
|
|
209
|
+
}
|
|
210
|
+
if (network === "sepolia") {
|
|
211
|
+
return dnaUrls.ethereumSepolia;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (chain === "beaconchain") {
|
|
216
|
+
if (network === "mainnet") {
|
|
217
|
+
return dnaUrls.beaconchain;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (chain === "starknet") {
|
|
222
|
+
if (network === "mainnet") {
|
|
223
|
+
return dnaUrls.starknet;
|
|
224
|
+
}
|
|
225
|
+
if (network === "sepolia") {
|
|
226
|
+
return dnaUrls.starknetSepolia;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
throw new Error(red("✖") + " Invalid chain or network");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Converts a kebab-case string to camelCase.
|
|
235
|
+
*
|
|
236
|
+
* Examples:
|
|
237
|
+
* - "hello-world" → "helloWorld"
|
|
238
|
+
* - "my-long-variable-name" → "myLongVariableName"
|
|
239
|
+
* - "MY-CAPS" → "myCaps"
|
|
240
|
+
* - "-leading-dash" → "leadingDash"
|
|
241
|
+
* - "trailing-dash-" → "trailingDash"
|
|
242
|
+
* - "double--dash" → "doubleDash"
|
|
243
|
+
* - "hello---world" → "helloWorld"
|
|
244
|
+
* - "mixed_dash-and_underscore" → "mixedDashAndUnderscore"
|
|
245
|
+
*
|
|
246
|
+
* @param str The kebab-case string to convert
|
|
247
|
+
* @returns The camelCase version of the string
|
|
248
|
+
*/
|
|
249
|
+
export function convertKebabToCamelCase(_str: string): string {
|
|
250
|
+
let str = _str;
|
|
251
|
+
|
|
252
|
+
// Handle empty or invalid input
|
|
253
|
+
if (!str || typeof str !== "string") {
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Check if already camelCase
|
|
258
|
+
if (/^[a-z][a-zA-Z0-9]*$/.test(str)) {
|
|
259
|
+
return str;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Trim leading/trailing dashes and spaces
|
|
263
|
+
str = str.trim().replace(/^-+|-+$/g, "");
|
|
264
|
+
|
|
265
|
+
// Handle empty string after trim
|
|
266
|
+
if (!str) {
|
|
267
|
+
return "";
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
str
|
|
272
|
+
// Replace multiple consecutive dashes/underscores with a single dash
|
|
273
|
+
.replace(/[-_]+/g, "-")
|
|
274
|
+
// Split on dash
|
|
275
|
+
.split("-")
|
|
276
|
+
// Filter out empty strings (from consecutive dashes)
|
|
277
|
+
.filter(Boolean)
|
|
278
|
+
// Convert each word
|
|
279
|
+
.map((word, index) => {
|
|
280
|
+
// Convert word to lowercase
|
|
281
|
+
const _word = word.toLowerCase();
|
|
282
|
+
|
|
283
|
+
// Capitalize first letter if not the first word
|
|
284
|
+
if (index > 0) {
|
|
285
|
+
return _word.charAt(0).toUpperCase() + _word.slice(1);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return _word;
|
|
289
|
+
})
|
|
290
|
+
.join("")
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export async function checkFileExists(
|
|
295
|
+
path: string,
|
|
296
|
+
options?: {
|
|
297
|
+
askPrompt?: boolean;
|
|
298
|
+
fileName?: string;
|
|
299
|
+
allowIgnore?: boolean;
|
|
300
|
+
},
|
|
301
|
+
): Promise<{
|
|
302
|
+
exists: boolean;
|
|
303
|
+
overwrite: boolean;
|
|
304
|
+
}> {
|
|
305
|
+
const { askPrompt = false, fileName, allowIgnore = false } = options ?? {};
|
|
306
|
+
|
|
307
|
+
if (!fs.existsSync(path)) {
|
|
308
|
+
return {
|
|
309
|
+
exists: false,
|
|
310
|
+
overwrite: false,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (askPrompt) {
|
|
315
|
+
const { overwrite } = await prompts({
|
|
316
|
+
type: "select",
|
|
317
|
+
name: "overwrite",
|
|
318
|
+
message: `${fileName ?? basename(path)} already exists. Please choose how to proceed:`,
|
|
319
|
+
initial: 0,
|
|
320
|
+
choices: [
|
|
321
|
+
...(allowIgnore
|
|
322
|
+
? [
|
|
323
|
+
{
|
|
324
|
+
title: "Keep original file",
|
|
325
|
+
value: "ignore",
|
|
326
|
+
},
|
|
327
|
+
]
|
|
328
|
+
: []),
|
|
329
|
+
{
|
|
330
|
+
title: "Cancel operation",
|
|
331
|
+
value: "no",
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
title: "Overwrite file",
|
|
335
|
+
value: "yes",
|
|
336
|
+
},
|
|
337
|
+
],
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
if (overwrite === "no") {
|
|
341
|
+
cancelOperation();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (overwrite === "ignore") {
|
|
345
|
+
return {
|
|
346
|
+
exists: true,
|
|
347
|
+
overwrite: false,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
exists: true,
|
|
353
|
+
overwrite: true,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
exists: true,
|
|
359
|
+
overwrite: false,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function cancelOperation(message?: string) {
|
|
364
|
+
throw new Error(red("✖") + (message ?? " Operation cancelled"));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function getPackageManager(): PkgInfo {
|
|
368
|
+
const userAgent = process.env.npm_config_user_agent;
|
|
369
|
+
const pkgInfo = pkgFromUserAgent(userAgent);
|
|
370
|
+
if (pkgInfo) {
|
|
371
|
+
return pkgInfo;
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
name: "npm",
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
|
|
380
|
+
https://github.com/vitejs/vite/blob/07091a1e804e5934208ef0b6324a04317dd0d815/packages/create-vite/src/index.ts#L585
|
|
381
|
+
|
|
382
|
+
MIT License
|
|
383
|
+
|
|
384
|
+
Copyright (c) 2019-present, VoidZero Inc. and Vite contributors
|
|
385
|
+
|
|
386
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
387
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
388
|
+
in the Software without restriction, including without limitation the rights
|
|
389
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
390
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
391
|
+
furnished to do so, subject to the following conditions:
|
|
392
|
+
|
|
393
|
+
The above copyright notice and this permission notice shall be included in all
|
|
394
|
+
copies or substantial portions of the Software.
|
|
395
|
+
|
|
396
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
397
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
398
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
399
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
400
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
401
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
402
|
+
SOFTWARE.
|
|
403
|
+
|
|
404
|
+
*/
|
|
405
|
+
function pkgFromUserAgent(userAgent: string | undefined): PkgInfo | undefined {
|
|
406
|
+
if (!userAgent) return undefined;
|
|
407
|
+
const pkgSpec = userAgent.split(" ")[0];
|
|
408
|
+
const pkgSpecArr = pkgSpec.split("/");
|
|
409
|
+
return {
|
|
410
|
+
name: pkgSpecArr[0],
|
|
411
|
+
version: pkgSpecArr[1],
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export async function formatFile(path: string) {
|
|
416
|
+
const file = fs.readFileSync(path, "utf8");
|
|
417
|
+
const formatted = await prettier.format(file, {
|
|
418
|
+
filepath: path,
|
|
419
|
+
tabWidth: 2,
|
|
420
|
+
});
|
|
421
|
+
fs.writeFileSync(path, formatted);
|
|
422
|
+
}
|
package/src/rollup/config.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
1
|
import { builtinModules } from "node:module";
|
|
3
2
|
import commonjs from "@rollup/plugin-commonjs";
|
|
4
3
|
import json from "@rollup/plugin-json";
|
|
5
4
|
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
|
6
|
-
import typescript from "@rollup/plugin-typescript";
|
|
7
5
|
import type { Apibara, RollupConfig } from "apibara/types";
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
6
|
+
import { join } from "pathe";
|
|
7
|
+
import type { OutputPluginOption } from "rollup";
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
import defu from "defu";
|
|
10
|
+
import { appConfig } from "./plugins/config";
|
|
11
|
+
import { esmShim } from "./plugins/esm-shim";
|
|
12
|
+
import { indexers } from "./plugins/indexers";
|
|
13
|
+
|
|
14
|
+
const runtimeDependencies = ["better-sqlite3", "@electric-sql/pglite"];
|
|
15
|
+
|
|
16
|
+
export function getRollupConfig(apibara: Apibara): RollupConfig {
|
|
15
17
|
const extensions: string[] = [
|
|
16
18
|
".ts",
|
|
17
19
|
".mjs",
|
|
@@ -22,188 +24,64 @@ export const getRollupConfig = (
|
|
|
22
24
|
".jsx",
|
|
23
25
|
];
|
|
24
26
|
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
indexerFiles = fsExtra
|
|
40
|
-
.readdirSync(indexerDir)
|
|
41
|
-
.filter((file) => file.endsWith(".indexer.ts"));
|
|
42
|
-
if (indexerFiles.length === 0) {
|
|
43
|
-
console.warn(`No indexer files found in ${indexerDir}`);
|
|
44
|
-
}
|
|
45
|
-
} catch (error) {
|
|
46
|
-
console.error(`Error reading indexers directory: ${error}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const indexerImports = indexerFiles
|
|
50
|
-
.map(
|
|
51
|
-
(file, index) =>
|
|
52
|
-
`import indexer${index} from '${join(indexerDir, file)}';`,
|
|
53
|
-
)
|
|
54
|
-
.join("\n");
|
|
55
|
-
|
|
56
|
-
// Generate main.ts content
|
|
57
|
-
const mainContent = `
|
|
58
|
-
import { createClient } from "@apibara/protocol";
|
|
59
|
-
import { createIndexer, run } from "@apibara/indexer";
|
|
60
|
-
import { consola as _consola } from "apibara/internal/consola";
|
|
61
|
-
import { defineCommand, runMain } from "apibara/internal/citty";
|
|
62
|
-
import config from './${configPath}';
|
|
63
|
-
|
|
64
|
-
const consola = _consola.withTag("Apibara | ");
|
|
65
|
-
|
|
66
|
-
${indexerImports}
|
|
67
|
-
|
|
68
|
-
const indexers = {
|
|
69
|
-
${indexerFiles
|
|
70
|
-
.map(
|
|
71
|
-
(file, index) => `'${basename(file, ".indexer.ts")}': indexer${index},`,
|
|
72
|
-
)
|
|
73
|
-
.join("\n ")}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const command = defineCommand({
|
|
77
|
-
meta: {
|
|
78
|
-
name: "run-indexers",
|
|
79
|
-
description: "Run Apibara indexers",
|
|
80
|
-
},
|
|
81
|
-
args: {
|
|
82
|
-
indexers: {
|
|
83
|
-
type: "string",
|
|
84
|
-
description: "Comma-separated list of indexers to run",
|
|
85
|
-
},
|
|
86
|
-
preset: {
|
|
87
|
-
type: "string",
|
|
88
|
-
description: "Preset to use",
|
|
89
|
-
},
|
|
90
|
-
sink: {
|
|
91
|
-
type: "string",
|
|
92
|
-
description: "Sink to use",
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
async run({ args }) {
|
|
96
|
-
const selectedIndexers = args.indexers ? args.indexers.split(',') : Object.keys(indexers);
|
|
97
|
-
const preset = args.preset || config.preset || 'default';
|
|
98
|
-
const sinkName = args.sink || 'default';
|
|
99
|
-
|
|
100
|
-
// Apply preset
|
|
101
|
-
let runtimeConfig = { ...config.runtimeConfig };
|
|
102
|
-
if (preset && config.presets && config.presets[preset]) {
|
|
103
|
-
runtimeConfig = { ...runtimeConfig, ...config.presets[preset].runtimeConfig };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Get sink function
|
|
107
|
-
const sinkFunction = config.sink?.[sinkName];
|
|
108
|
-
if (!sinkFunction) {
|
|
109
|
-
throw new Error(\`Sink \${sinkName} not found\`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await Promise.all(selectedIndexers.map(async (name) => {
|
|
113
|
-
if (!indexers[name]) {
|
|
114
|
-
console.error(\`Indexer \${name} not found\`);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const indexerConfig = indexers[name]
|
|
119
|
-
const indexer = typeof indexerConfig === 'function'
|
|
120
|
-
? await createIndexer(indexerConfig(runtimeConfig))
|
|
121
|
-
: createIndexer(indexerConfig);
|
|
122
|
-
|
|
123
|
-
const client = createClient(indexer.streamConfig, indexer.options.streamUrl);
|
|
124
|
-
const sink = sinkFunction();
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
consola.log("Running Indexer: ", name);
|
|
128
|
-
await run(client, indexer, sink);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
consola.error(\`Error in indexer \${name}:\`, error);
|
|
131
|
-
}
|
|
132
|
-
}));
|
|
133
|
-
},
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
runMain(command);
|
|
137
|
-
`;
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
input: {
|
|
141
|
-
main: "virtual:main.ts",
|
|
142
|
-
},
|
|
143
|
-
output: {
|
|
144
|
-
dir: join(apibara.options.outputDir || "./.apibara/build"),
|
|
145
|
-
format: "esm",
|
|
146
|
-
exports: "auto",
|
|
147
|
-
entryFileNames: "[name].mjs",
|
|
148
|
-
chunkFileNames: "chunks/[name]-[hash].mjs",
|
|
149
|
-
generatedCode: {
|
|
150
|
-
constBindings: true,
|
|
151
|
-
},
|
|
152
|
-
sourcemap: true,
|
|
153
|
-
sourcemapExcludeSources: true,
|
|
154
|
-
sourcemapIgnoreList(relativePath, sourcemapPath) {
|
|
155
|
-
return relativePath.includes("node_modules");
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
plugins: [
|
|
159
|
-
{
|
|
160
|
-
name: "virtual",
|
|
161
|
-
resolveId(id) {
|
|
162
|
-
if (id === "virtual:main.ts") {
|
|
163
|
-
return id;
|
|
164
|
-
}
|
|
165
|
-
return null;
|
|
27
|
+
const rollupConfig: RollupConfig & { plugins: OutputPluginOption[] } = defu(
|
|
28
|
+
// biome-ignore lint/suspicious/noExplicitAny: apibara.options.rollupConfig is typed
|
|
29
|
+
apibara.options.rollupConfig as any,
|
|
30
|
+
<RollupConfig>{
|
|
31
|
+
input: apibara.options.entry,
|
|
32
|
+
output: {
|
|
33
|
+
dir: join(apibara.options.outputDir || "./.apibara/build"),
|
|
34
|
+
format: "esm",
|
|
35
|
+
exports: "auto",
|
|
36
|
+
entryFileNames: "[name].mjs",
|
|
37
|
+
chunkFileNames: "chunks/[name]-[hash].mjs",
|
|
38
|
+
generatedCode: {
|
|
39
|
+
constBindings: true,
|
|
166
40
|
},
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return null;
|
|
41
|
+
sourcemap: true,
|
|
42
|
+
sourcemapExcludeSources: true,
|
|
43
|
+
sourcemapIgnoreList(relativePath, sourcemapPath) {
|
|
44
|
+
return relativePath.includes("node_modules");
|
|
172
45
|
},
|
|
173
46
|
},
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
],
|
|
190
|
-
onwarn(warning, rollupWarn) {
|
|
191
|
-
if (
|
|
192
|
-
!["CIRCULAR_DEPENDENCY", "EVAL"].includes(warning.code || "") &&
|
|
193
|
-
!warning.message.includes("Unsupported source map comment") &&
|
|
194
|
-
!warning.message.includes("@__PURE__")
|
|
195
|
-
) {
|
|
196
|
-
rollupWarn(warning);
|
|
197
|
-
}
|
|
47
|
+
plugins: [],
|
|
48
|
+
onwarn(warning, rollupWarn) {
|
|
49
|
+
if (
|
|
50
|
+
!["CIRCULAR_DEPENDENCY", "EVAL", "THIS_IS_UNDEFINED"].includes(
|
|
51
|
+
warning.code || "",
|
|
52
|
+
) &&
|
|
53
|
+
!warning.message.includes("Unsupported source map comment") &&
|
|
54
|
+
!warning.message.includes("@__PURE__") &&
|
|
55
|
+
!warning.message.includes("/*#__PURE__*/")
|
|
56
|
+
) {
|
|
57
|
+
rollupWarn(warning);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
treeshake: true,
|
|
61
|
+
external: [...builtinModules, ...runtimeDependencies],
|
|
198
62
|
},
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
rollupConfig.plugins.push(esmShim());
|
|
66
|
+
rollupConfig.plugins.push(json());
|
|
67
|
+
|
|
68
|
+
rollupConfig.plugins.push(
|
|
69
|
+
commonjs({
|
|
70
|
+
strictRequires: true,
|
|
71
|
+
requireReturnsDefault: "auto",
|
|
72
|
+
...apibara.options.commonJS,
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
rollupConfig.plugins.push(
|
|
77
|
+
nodeResolve({
|
|
78
|
+
extensions,
|
|
79
|
+
preferBuiltins: true,
|
|
80
|
+
mainFields: ["main"],
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
rollupConfig.plugins.push(indexers(apibara));
|
|
84
|
+
rollupConfig.plugins.push(appConfig(apibara));
|
|
85
|
+
|
|
86
|
+
return rollupConfig;
|
|
87
|
+
}
|
package/src/rollup/index.ts
CHANGED