apibara 2.1.0-beta.3 → 2.1.0-beta.31
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 +17 -8
- package/dist/chunks/add.mjs.map +1 -0
- package/dist/chunks/build.mjs +5 -2
- package/dist/chunks/build.mjs.map +1 -0
- package/dist/chunks/dev.mjs +56 -21
- package/dist/chunks/dev.mjs.map +1 -0
- package/dist/chunks/init.mjs +12 -7
- package/dist/chunks/init.mjs.map +1 -0
- package/dist/chunks/prepare.mjs +5 -2
- package/dist/chunks/prepare.mjs.map +1 -0
- package/dist/chunks/start.mjs +16 -11
- package/dist/chunks/start.mjs.map +1 -0
- package/dist/chunks/write-project-info.mjs +51 -0
- package/dist/chunks/write-project-info.mjs.map +1 -0
- package/dist/cli/index.mjs +3 -1
- package/dist/cli/index.mjs.map +1 -0
- package/dist/common/index.d.mts +28 -0
- package/dist/common/index.d.ts +28 -0
- package/dist/common/index.mjs +64 -0
- package/dist/common/index.mjs.map +1 -0
- package/dist/config/index.mjs +1 -0
- package/dist/config/index.mjs.map +1 -0
- package/dist/core/index.mjs +133 -68
- package/dist/core/index.mjs.map +1 -0
- package/dist/create/index.d.mts +2 -1
- package/dist/create/index.d.ts +2 -1
- package/dist/create/index.mjs +144 -115
- package/dist/create/index.mjs.map +1 -0
- package/dist/hooks/index.mjs +6 -1
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/indexer/index.d.ts +1 -0
- package/dist/indexer/index.mjs +1 -0
- package/dist/indexer/plugins.d.ts +1 -0
- package/dist/indexer/plugins.mjs +1 -0
- package/dist/indexer/testing.d.ts +1 -0
- package/dist/indexer/testing.mjs +1 -0
- package/dist/indexer/vcr.d.ts +1 -0
- package/dist/indexer/vcr.mjs +1 -0
- package/dist/rolldown/index.d.mts +7 -0
- package/dist/rolldown/index.d.ts +7 -0
- package/dist/rolldown/index.mjs +122 -0
- package/dist/rolldown/index.mjs.map +1 -0
- package/dist/runtime/dev.mjs +23 -10
- package/dist/runtime/internal/app.d.ts +16 -1
- package/dist/runtime/internal/app.mjs +51 -21
- package/dist/runtime/project-info.d.ts +3 -0
- package/dist/runtime/project-info.mjs +67 -0
- package/dist/runtime/start.mjs +27 -10
- package/dist/shared/apibara.63c9a277.mjs +30 -0
- package/dist/shared/apibara.63c9a277.mjs.map +1 -0
- package/dist/shared/apibara.730bb1e4.mjs +18 -0
- package/dist/shared/apibara.730bb1e4.mjs.map +1 -0
- package/dist/types/index.d.mts +24 -20
- package/dist/types/index.d.ts +24 -20
- package/dist/types/index.mjs +1 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +33 -16
- package/src/cli/commands/add.ts +16 -7
- package/src/cli/commands/build.ts +5 -2
- package/src/cli/commands/dev.ts +64 -20
- package/src/cli/commands/init.ts +12 -7
- package/src/cli/commands/prepare.ts +4 -2
- package/src/cli/commands/start.ts +17 -10
- package/src/cli/commands/write-project-info.ts +56 -0
- package/src/cli/common.ts +33 -1
- package/src/cli/index.ts +2 -0
- package/src/common/constants.ts +6 -0
- package/src/common/helper.ts +86 -0
- package/src/common/index.ts +2 -0
- package/src/core/apibara.ts +7 -2
- package/src/core/build/build.ts +13 -5
- package/src/core/build/dev.ts +46 -23
- package/src/core/build/error.ts +9 -14
- package/src/core/build/prepare.ts +5 -3
- package/src/core/build/prod.ts +25 -16
- package/src/core/build/types.ts +11 -1
- package/src/core/config/defaults.ts +3 -0
- package/src/core/config/loader.ts +15 -7
- package/src/core/config/resolvers/runtime.resolver.ts +44 -0
- package/src/core/config/update.ts +6 -2
- package/src/create/add.ts +10 -9
- package/src/create/constants.ts +9 -11
- package/src/create/init.ts +8 -5
- package/src/create/templates.ts +132 -105
- package/src/hooks/useRuntimeConfig.ts +4 -1
- package/src/indexer/index.ts +1 -0
- package/src/indexer/plugins.ts +1 -0
- package/src/indexer/testing.ts +1 -0
- package/src/indexer/vcr.ts +1 -0
- package/src/rolldown/config.ts +84 -0
- package/src/rolldown/index.ts +2 -0
- package/src/{rollup → rolldown}/plugins/indexers.ts +3 -3
- package/src/rolldown/plugins/instrumentation.ts +68 -0
- package/src/runtime/dev.ts +25 -9
- package/src/runtime/internal/app.ts +75 -29
- package/src/runtime/project-info.ts +90 -0
- package/src/runtime/start.ts +30 -9
- package/src/types/config.ts +27 -13
- package/src/types/hooks.ts +8 -5
- package/src/types/index.ts +1 -1
- package/src/types/rolldown.ts +5 -0
- package/src/types/virtual/indexers.d.ts +4 -1
- package/src/types/virtual/instrumentation.d.ts +4 -0
- package/dist/rollup/index.d.mts +0 -6
- package/dist/rollup/index.d.ts +0 -6
- package/dist/rollup/index.mjs +0 -150
- package/dist/shared/apibara.1b515d04.mjs +0 -8
- package/src/core/config/resolvers/preset.resolver.ts +0 -9
- package/src/core/config/resolvers/runtime-config.resolver.ts +0 -6
- package/src/rollup/config.ts +0 -87
- package/src/rollup/index.ts +0 -2
- package/src/rollup/plugins/config.ts +0 -12
- package/src/rollup/plugins/esm-shim.ts +0 -69
- package/src/types/rollup.ts +0 -8
- package/src/types/virtual/config.d.ts +0 -3
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import defu from "defu";
|
|
2
|
+
import {
|
|
3
|
+
ENV_INTERNAL_APIBARA_PRESET,
|
|
4
|
+
ENV_INTERNAL_APIBARA_PRESETS,
|
|
5
|
+
ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME,
|
|
6
|
+
ENV_INTERNAL_APIBARA_RUNTIME,
|
|
7
|
+
USER_ENV_APIBARA_RUNTIME_CONFIG,
|
|
8
|
+
} from "./constants";
|
|
9
|
+
|
|
10
|
+
export function getRuntimeDataFromEnv() {
|
|
11
|
+
const processedRuntimeConfig: Record<string, unknown> = JSON.parse(
|
|
12
|
+
process.env[ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME] ?? "{}",
|
|
13
|
+
);
|
|
14
|
+
const preset: string | undefined = process.env[ENV_INTERNAL_APIBARA_PRESET];
|
|
15
|
+
const presets: Record<string, unknown> | undefined = JSON.parse(
|
|
16
|
+
process.env[ENV_INTERNAL_APIBARA_PRESETS] ?? "{}",
|
|
17
|
+
);
|
|
18
|
+
const runtimeConfig: Record<string, unknown> | undefined = JSON.parse(
|
|
19
|
+
process.env[ENV_INTERNAL_APIBARA_RUNTIME] ?? "{}",
|
|
20
|
+
);
|
|
21
|
+
const userEnvRuntimeConfig: Record<string, unknown> | undefined = JSON.parse(
|
|
22
|
+
process.env[USER_ENV_APIBARA_RUNTIME_CONFIG] ?? "{}",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
userEnvRuntimeConfig,
|
|
27
|
+
processedRuntimeConfig,
|
|
28
|
+
preset,
|
|
29
|
+
presets,
|
|
30
|
+
runtimeConfig,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Get the merged runtime config from the user env overrided runtime config, presets and defaults.
|
|
36
|
+
* Priority (Highest to lowest):
|
|
37
|
+
* 1. User env overrided runtime config
|
|
38
|
+
* 2. Preset
|
|
39
|
+
* 3. Defaults
|
|
40
|
+
*/
|
|
41
|
+
export function getProcessedRuntimeConfig({
|
|
42
|
+
preset,
|
|
43
|
+
presets,
|
|
44
|
+
runtimeConfig,
|
|
45
|
+
userEnvRuntimeConfig,
|
|
46
|
+
}: {
|
|
47
|
+
preset?: string;
|
|
48
|
+
presets?: Record<string, unknown>;
|
|
49
|
+
runtimeConfig?: Record<string, unknown>;
|
|
50
|
+
userEnvRuntimeConfig?: Record<string, unknown>;
|
|
51
|
+
}) {
|
|
52
|
+
let _runtimeConfig: Record<string, unknown> = { ...runtimeConfig };
|
|
53
|
+
|
|
54
|
+
if (preset) {
|
|
55
|
+
if (presets === undefined) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Specified preset "${preset}" but no presets were defined`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (presets[preset] === undefined) {
|
|
62
|
+
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const presetValue = presets[preset] as {
|
|
66
|
+
runtimeConfig: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Preset applied
|
|
70
|
+
_runtimeConfig = defu(presetValue.runtimeConfig, _runtimeConfig);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (userEnvRuntimeConfig) {
|
|
74
|
+
try {
|
|
75
|
+
// Environment runtime config applied
|
|
76
|
+
_runtimeConfig = defu(userEnvRuntimeConfig, _runtimeConfig);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
"Failed to parse runtime config from process.env.APIBARA_RUNTIME_CONFIG. Please ensure it is a valid JSON string.",
|
|
80
|
+
{ cause: error },
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return _runtimeConfig;
|
|
86
|
+
}
|
package/src/core/apibara.ts
CHANGED
|
@@ -17,14 +17,19 @@ export async function createApibara(
|
|
|
17
17
|
): Promise<Apibara> {
|
|
18
18
|
const options = await loadOptions(config, opts, dev);
|
|
19
19
|
|
|
20
|
+
// Enable source map support in Node
|
|
21
|
+
process.env.NODE_OPTIONS = process.env.NODE_OPTIONS
|
|
22
|
+
? `${process.env.NODE_OPTIONS} --enable-source-maps`
|
|
23
|
+
: "--enable-source-maps";
|
|
24
|
+
|
|
20
25
|
const apibara: Apibara = {
|
|
21
26
|
options,
|
|
22
27
|
indexers: [],
|
|
23
28
|
hooks: createHooks(),
|
|
24
29
|
close: () => apibara.hooks.callHook("close"),
|
|
25
30
|
logger: consola.withTag("apibara"),
|
|
26
|
-
async updateConfig(
|
|
27
|
-
updateApibaraConfig(apibara,
|
|
31
|
+
async updateConfig(newConfig: ApibaraDynamicConfig) {
|
|
32
|
+
updateApibaraConfig(apibara, newConfig);
|
|
28
33
|
},
|
|
29
34
|
};
|
|
30
35
|
|
package/src/core/build/build.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getRolldownConfig } from "apibara/rolldown";
|
|
2
2
|
import type { Apibara } from "apibara/types";
|
|
3
|
+
import { colors } from "consola/utils";
|
|
3
4
|
import { watchDev } from "./dev";
|
|
4
5
|
import { buildProduction } from "./prod";
|
|
5
6
|
|
|
6
7
|
export async function build(apibara: Apibara) {
|
|
7
|
-
const
|
|
8
|
+
const rolldownConfig = getRolldownConfig(apibara);
|
|
8
9
|
|
|
9
|
-
await apibara.hooks.callHook("
|
|
10
|
+
await apibara.hooks.callHook("rolldown:before", apibara, rolldownConfig);
|
|
11
|
+
|
|
12
|
+
if (apibara.options.rollupConfig) {
|
|
13
|
+
apibara.logger.error(
|
|
14
|
+
`\n${colors.cyan("apibara.config:")} rollupConfig is deprecated. Use rolldownConfig instead`,
|
|
15
|
+
);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
10
18
|
|
|
11
19
|
return apibara.options.dev
|
|
12
|
-
? await watchDev(apibara,
|
|
13
|
-
: await buildProduction(apibara,
|
|
20
|
+
? await watchDev(apibara, rolldownConfig)
|
|
21
|
+
: await buildProduction(apibara, rolldownConfig);
|
|
14
22
|
}
|
package/src/core/build/dev.ts
CHANGED
|
@@ -1,59 +1,67 @@
|
|
|
1
|
-
import type { Apibara
|
|
1
|
+
import type { Apibara } from "apibara/types";
|
|
2
2
|
import { watch } from "chokidar";
|
|
3
3
|
import defu from "defu";
|
|
4
4
|
import { join } from "pathe";
|
|
5
5
|
import { debounce } from "perfect-debounce";
|
|
6
|
-
import * as
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
export async function watchDev(apibara: Apibara, rollupConfig: RollupConfig) {
|
|
10
|
-
let rollupWatcher: rollup.RollupWatcher;
|
|
6
|
+
import * as rolldown from "rolldown";
|
|
7
|
+
import { formatRolldownError } from "./error";
|
|
11
8
|
|
|
9
|
+
export async function watchDev(
|
|
10
|
+
apibara: Apibara,
|
|
11
|
+
rolldownConfig: rolldown.RolldownOptions,
|
|
12
|
+
) {
|
|
13
|
+
let rolldownWatcher: rolldown.RolldownWatcher;
|
|
12
14
|
async function load() {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
apibara.logger.start("Setting up a dev server");
|
|
16
|
+
if (rolldownWatcher) {
|
|
17
|
+
await rolldownWatcher.close();
|
|
15
18
|
}
|
|
16
|
-
|
|
19
|
+
rolldownWatcher = startRolldownWatcher(apibara, rolldownConfig);
|
|
17
20
|
}
|
|
18
|
-
const reload = debounce(load);
|
|
21
|
+
const reload = debounce(async () => await load());
|
|
19
22
|
|
|
20
|
-
const watchPatterns =
|
|
23
|
+
const watchPatterns = getWatchPatterns(apibara);
|
|
21
24
|
|
|
22
25
|
const watchReloadEvents = new Set(["add", "addDir", "unlink", "unlinkDir"]);
|
|
23
26
|
const reloadWatcher = watch(watchPatterns, { ignoreInitial: true }).on(
|
|
24
27
|
"all",
|
|
25
|
-
(event) => {
|
|
28
|
+
async (event) => {
|
|
26
29
|
if (watchReloadEvents.has(event)) {
|
|
27
|
-
reload();
|
|
30
|
+
await reload();
|
|
28
31
|
}
|
|
29
32
|
},
|
|
30
33
|
);
|
|
31
34
|
|
|
32
35
|
apibara.hooks.hook("close", () => {
|
|
33
|
-
|
|
36
|
+
rolldownWatcher.close();
|
|
34
37
|
reloadWatcher.close();
|
|
35
38
|
});
|
|
36
39
|
|
|
37
|
-
apibara.hooks.hook("
|
|
40
|
+
apibara.hooks.hook("rolldown:reload", async () => await reload());
|
|
38
41
|
|
|
39
42
|
await load();
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
function startRolldownWatcher(
|
|
46
|
+
apibara: Apibara,
|
|
47
|
+
rolldownConfig: rolldown.RolldownOptions,
|
|
48
|
+
) {
|
|
49
|
+
const ignorePatterns = getIgnorePatterns(apibara);
|
|
50
|
+
const watcher = rolldown.watch(
|
|
51
|
+
defu(rolldownConfig, {
|
|
45
52
|
watch: {
|
|
46
|
-
|
|
53
|
+
exclude: ignorePatterns,
|
|
54
|
+
...((apibara.options.watchOptions ?? {}) as rolldown.WatchOptions),
|
|
47
55
|
},
|
|
48
56
|
}),
|
|
49
57
|
);
|
|
50
58
|
let start: number;
|
|
51
59
|
|
|
52
|
-
watcher.on("event", (event) => {
|
|
60
|
+
watcher.on("event", async (event) => {
|
|
53
61
|
switch (event.code) {
|
|
54
62
|
// The watcher is (re)starting
|
|
55
63
|
case "START": {
|
|
56
|
-
apibara.hooks.callHook("dev:restart");
|
|
64
|
+
await apibara.hooks.callHook("dev:restart");
|
|
57
65
|
return;
|
|
58
66
|
}
|
|
59
67
|
|
|
@@ -70,15 +78,30 @@ function startRollupWatcher(apibara: Apibara, rollupConfig: RollupConfig) {
|
|
|
70
78
|
"Indexers built",
|
|
71
79
|
start ? `in ${Date.now() - start} ms` : "",
|
|
72
80
|
);
|
|
73
|
-
apibara.hooks.callHook("dev:reload");
|
|
81
|
+
await apibara.hooks.callHook("dev:reload");
|
|
74
82
|
return;
|
|
75
83
|
}
|
|
76
84
|
|
|
77
85
|
// Encountered an error while bundling
|
|
78
86
|
case "ERROR": {
|
|
79
|
-
apibara.logger.error(
|
|
87
|
+
apibara.logger.error(formatRolldownError(event.error));
|
|
80
88
|
}
|
|
81
89
|
}
|
|
82
90
|
});
|
|
83
91
|
return watcher;
|
|
84
92
|
}
|
|
93
|
+
|
|
94
|
+
const getWatchPatterns = (apibara: Apibara) => [
|
|
95
|
+
join(apibara.options.rootDir, "indexers"),
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
const getIgnorePatterns = (apibara: Apibara) => [
|
|
99
|
+
"**/.apibara/**",
|
|
100
|
+
"**/.git/**",
|
|
101
|
+
"**/.DS_Store",
|
|
102
|
+
"**/node_modules/**",
|
|
103
|
+
"**/dist/**",
|
|
104
|
+
"**/.turbo/**",
|
|
105
|
+
// changes to apibara.config is handled by c12 itself so we dont need rolldown to handle this
|
|
106
|
+
"**/apibara.config?(.ts|.js)",
|
|
107
|
+
];
|
package/src/core/build/error.ts
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
import type esbuild from "esbuild";
|
|
2
1
|
import { isAbsolute, relative } from "pathe";
|
|
3
|
-
import type
|
|
2
|
+
import type * as rolldown from "rolldown";
|
|
4
3
|
|
|
5
|
-
export function
|
|
6
|
-
_error: rollup.RollupError | esbuild.OnResolveResult,
|
|
7
|
-
) {
|
|
4
|
+
export function formatRolldownError(_error: rolldown.RollupError) {
|
|
8
5
|
try {
|
|
9
6
|
const logs: string[] = [_error.toString()];
|
|
10
7
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
11
|
-
const errors = (_error as any)?.errors || [_error as
|
|
8
|
+
const errors = (_error as any)?.errors || [_error as rolldown.RollupError];
|
|
12
9
|
for (const error of errors) {
|
|
13
|
-
const id = error.path || error.id || (_error as
|
|
10
|
+
const id = error.path || error.id || (_error as rolldown.RollupError).id;
|
|
14
11
|
let path = isAbsolute(id) ? relative(process.cwd(), id) : id;
|
|
15
|
-
const location =
|
|
16
|
-
(error as rollup.RollupError).loc ||
|
|
17
|
-
(error as esbuild.PartialMessage).location;
|
|
12
|
+
const location = (error as rolldown.RollupError).loc;
|
|
18
13
|
if (location) {
|
|
19
14
|
path += `:${location.line}:${location.column}`;
|
|
20
15
|
}
|
|
21
|
-
const text =
|
|
22
|
-
(error as esbuild.PartialMessage).text ||
|
|
23
|
-
(error as rollup.RollupError).frame;
|
|
16
|
+
const text = (error as rolldown.RollupError).frame;
|
|
24
17
|
|
|
25
18
|
logs.push(
|
|
26
|
-
`
|
|
19
|
+
`Rolldown error while processing \`${path}\`` + text
|
|
20
|
+
? "\n\n" + text
|
|
21
|
+
: "",
|
|
27
22
|
);
|
|
28
23
|
}
|
|
29
24
|
return logs.join("\n");
|
|
@@ -7,9 +7,11 @@ export async function prepare(apibara: Apibara) {
|
|
|
7
7
|
await prepareDir(apibara.options.buildDir);
|
|
8
8
|
await prepareDir(apibara.options.outputDir);
|
|
9
9
|
|
|
10
|
-
apibara.
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
if (!apibara.options.disableLogs) {
|
|
11
|
+
apibara.logger.success(
|
|
12
|
+
`Output directory ${prettyPath(apibara.options.outputDir)} cleaned`,
|
|
13
|
+
);
|
|
14
|
+
}
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
async function prepareDir(dir: string) {
|
package/src/core/build/prod.ts
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
|
-
import type { Apibara
|
|
1
|
+
import type { Apibara } from "apibara/types";
|
|
2
2
|
import { colors } from "consola/utils";
|
|
3
|
-
import
|
|
3
|
+
import * as rolldown from "rolldown";
|
|
4
4
|
|
|
5
5
|
export async function buildProduction(
|
|
6
6
|
apibara: Apibara,
|
|
7
|
-
|
|
7
|
+
rolldownConfig: rolldown.RolldownOptions,
|
|
8
8
|
) {
|
|
9
|
-
apibara.
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
if (!apibara.options.disableLogs) {
|
|
10
|
+
apibara.logger.start(
|
|
11
|
+
`Building ${colors.cyan(apibara.indexers.length)} indexers`,
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const startTime = Date.now();
|
|
12
16
|
|
|
13
17
|
try {
|
|
14
|
-
const bundle = await
|
|
18
|
+
const bundle = await rolldown.rolldown(rolldownConfig);
|
|
15
19
|
|
|
16
|
-
if (Array.isArray(
|
|
17
|
-
for (const outputOptions of
|
|
20
|
+
if (Array.isArray(rolldownConfig.output)) {
|
|
21
|
+
for (const outputOptions of rolldownConfig.output) {
|
|
18
22
|
await bundle.write(outputOptions);
|
|
19
23
|
}
|
|
20
|
-
} else if (
|
|
21
|
-
await bundle.write(
|
|
24
|
+
} else if (rolldownConfig.output) {
|
|
25
|
+
await bundle.write(rolldownConfig.output as rolldown.OutputOptions);
|
|
22
26
|
} else {
|
|
23
|
-
throw new Error("No output options specified in
|
|
27
|
+
throw new Error("No output options specified in Rolldown config");
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
await bundle.close();
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
32
|
+
const endTime = Date.now();
|
|
33
|
+
const duration = endTime - startTime;
|
|
34
|
+
|
|
35
|
+
if (!apibara.options.disableLogs) {
|
|
36
|
+
apibara.logger.success(`Build succeeded in ${duration}ms`);
|
|
37
|
+
apibara.logger.info(
|
|
38
|
+
`You can start the indexers with ${colors.cyan("apibara start")}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
32
41
|
} catch (error) {
|
|
33
42
|
apibara.logger.error("Build failed", error);
|
|
34
43
|
throw error;
|
package/src/core/build/types.ts
CHANGED
|
@@ -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 = [
|
|
@@ -47,5 +55,7 @@ declare module "apibara/types" {`,
|
|
|
47
55
|
}),
|
|
48
56
|
);
|
|
49
57
|
|
|
50
|
-
apibara.
|
|
58
|
+
if (!apibara.options.disableLogs) {
|
|
59
|
+
apibara.logger.success(`Types written to ${prettyPath(typesDir)}`);
|
|
60
|
+
}
|
|
51
61
|
}
|
|
@@ -7,14 +7,10 @@ import { loadConfig, watchConfig } from "c12";
|
|
|
7
7
|
import { klona } from "klona/full";
|
|
8
8
|
import { ApibaraDefaults } from "./defaults";
|
|
9
9
|
import { resolvePathOptions } from "./resolvers/paths.resolver";
|
|
10
|
-
import {
|
|
11
|
-
import { resolveRuntimeConfigOptions } from "./resolvers/runtime-config.resolver";
|
|
10
|
+
import { runtimeConfigResolver } from "./resolvers/runtime.resolver";
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
resolveRuntimeConfigOptions,
|
|
16
|
-
presetResolver,
|
|
17
|
-
] as const;
|
|
12
|
+
// runtimeConfigResolver handles assigning runtime values to env
|
|
13
|
+
const configResolvers = [resolvePathOptions, runtimeConfigResolver] as const;
|
|
18
14
|
|
|
19
15
|
export async function loadOptions(
|
|
20
16
|
configOverrides: ApibaraConfig = {},
|
|
@@ -22,9 +18,21 @@ export async function loadOptions(
|
|
|
22
18
|
dev = false,
|
|
23
19
|
): Promise<ApibaraOptions> {
|
|
24
20
|
const options = await _loadUserConfig(configOverrides, opts, dev);
|
|
21
|
+
|
|
22
|
+
// Check if the runtimeConfig is serializable
|
|
23
|
+
try {
|
|
24
|
+
JSON.stringify(options.runtimeConfig);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Non-serializable runtimeConfig. Please ensure the config is serializable.",
|
|
28
|
+
{ cause: error },
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
for (const resolver of configResolvers) {
|
|
26
33
|
await resolver(options);
|
|
27
34
|
}
|
|
35
|
+
|
|
28
36
|
return options;
|
|
29
37
|
}
|
|
30
38
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ENV_INTERNAL_APIBARA_PRESET,
|
|
3
|
+
ENV_INTERNAL_APIBARA_PRESETS,
|
|
4
|
+
ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME,
|
|
5
|
+
ENV_INTERNAL_APIBARA_RUNTIME,
|
|
6
|
+
USER_ENV_APIBARA_RUNTIME_CONFIG,
|
|
7
|
+
getProcessedRuntimeConfig,
|
|
8
|
+
} from "apibara/common";
|
|
9
|
+
import type { ApibaraOptions } from "apibara/types";
|
|
10
|
+
|
|
11
|
+
export function runtimeConfigResolver(options: Partial<ApibaraOptions>) {
|
|
12
|
+
try {
|
|
13
|
+
const { runtimeConfig, preset, presets } = options;
|
|
14
|
+
|
|
15
|
+
if (runtimeConfig) {
|
|
16
|
+
process.env[ENV_INTERNAL_APIBARA_RUNTIME] = JSON.stringify(runtimeConfig);
|
|
17
|
+
}
|
|
18
|
+
if (preset) {
|
|
19
|
+
process.env[ENV_INTERNAL_APIBARA_PRESET] = preset;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (presets) {
|
|
23
|
+
process.env[ENV_INTERNAL_APIBARA_PRESETS] = JSON.stringify(presets);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const userEnvRuntimeConfig = JSON.parse(
|
|
27
|
+
process.env[USER_ENV_APIBARA_RUNTIME_CONFIG] ?? "{}",
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// This is final processed runtime config that will be used by the indexer and useRuntimeConfig hook
|
|
31
|
+
process.env[ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME] = JSON.stringify(
|
|
32
|
+
getProcessedRuntimeConfig({
|
|
33
|
+
preset,
|
|
34
|
+
presets,
|
|
35
|
+
runtimeConfig,
|
|
36
|
+
userEnvRuntimeConfig,
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error("Failed to process & set runtime environment variables", {
|
|
41
|
+
cause: error,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { Apibara, ApibaraDynamicConfig } from "apibara/types";
|
|
2
|
+
import { runtimeConfigResolver } from "./resolvers/runtime.resolver";
|
|
2
3
|
|
|
3
4
|
export async function updateApibaraConfig(
|
|
4
5
|
apibara: Apibara,
|
|
5
|
-
|
|
6
|
+
newConfig: ApibaraDynamicConfig,
|
|
6
7
|
) {
|
|
7
|
-
|
|
8
|
+
// applies new config values to env again during hot reload
|
|
9
|
+
runtimeConfigResolver(newConfig);
|
|
8
10
|
apibara.logger.success("Apibara config hot reloaded!");
|
|
11
|
+
// we simply stop indexers and restart them with updated runtime values
|
|
12
|
+
await apibara.hooks.callHook("dev:reload");
|
|
9
13
|
}
|
package/src/create/add.ts
CHANGED
|
@@ -37,6 +37,7 @@ type Options = {
|
|
|
37
37
|
argNetwork?: string;
|
|
38
38
|
argStorage?: string;
|
|
39
39
|
argDnaUrl?: string;
|
|
40
|
+
argRootDir?: string;
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
export async function addIndexer({
|
|
@@ -45,8 +46,10 @@ export async function addIndexer({
|
|
|
45
46
|
argNetwork,
|
|
46
47
|
argStorage,
|
|
47
48
|
argDnaUrl,
|
|
49
|
+
argRootDir,
|
|
48
50
|
}: Options) {
|
|
49
|
-
const
|
|
51
|
+
const cwd = path.join(process.cwd(), argRootDir ?? ".");
|
|
52
|
+
const configExists = hasApibaraConfig(cwd);
|
|
50
53
|
|
|
51
54
|
if (!configExists) {
|
|
52
55
|
consola.error("No apibara.config found in the current directory.");
|
|
@@ -74,7 +77,7 @@ export async function addIndexer({
|
|
|
74
77
|
}
|
|
75
78
|
}
|
|
76
79
|
|
|
77
|
-
const language = getApibaraConfigLanguage(
|
|
80
|
+
const language = getApibaraConfigLanguage(cwd);
|
|
78
81
|
|
|
79
82
|
validateIndexerId(argIndexerId, true);
|
|
80
83
|
validateChain(argChain, true);
|
|
@@ -93,7 +96,7 @@ export async function addIndexer({
|
|
|
93
96
|
validateIndexerId(id)
|
|
94
97
|
? checkFileExists(
|
|
95
98
|
path.join(
|
|
96
|
-
|
|
99
|
+
cwd,
|
|
97
100
|
"indexers",
|
|
98
101
|
`${id}.indexer.${language === "typescript" ? "ts" : "js"}`,
|
|
99
102
|
),
|
|
@@ -197,7 +200,7 @@ export async function addIndexer({
|
|
|
197
200
|
const pkgManager = getPackageManager();
|
|
198
201
|
|
|
199
202
|
const options: IndexerOptions = {
|
|
200
|
-
cwd:
|
|
203
|
+
cwd: cwd,
|
|
201
204
|
indexerFileId,
|
|
202
205
|
indexerId: convertKebabToCamelCase(indexerFileId),
|
|
203
206
|
chain: (argChain as Chain) ?? prompt_chain?.name!,
|
|
@@ -228,11 +231,9 @@ export async function addIndexer({
|
|
|
228
231
|
|
|
229
232
|
console.log();
|
|
230
233
|
|
|
234
|
+
const baseCommand = `${options.packageManager} install`;
|
|
235
|
+
const tsCommand = `${baseCommand} && ${options.packageManager} run prepare`;
|
|
231
236
|
consola.info(
|
|
232
|
-
`Before running the indexer, run ${cyan(
|
|
233
|
-
language === "typescript"
|
|
234
|
-
? " & " + cyan(`${options.packageManager} run prepare`)
|
|
235
|
-
: ""
|
|
236
|
-
}`,
|
|
237
|
+
`Before running the indexer, run ${cyan(language === "typescript" ? tsCommand : baseCommand)}`,
|
|
237
238
|
);
|
|
238
239
|
}
|
package/src/create/constants.ts
CHANGED
|
@@ -66,26 +66,24 @@ export const storages: StorageDataType[] = [
|
|
|
66
66
|
|
|
67
67
|
export const packageVersions = {
|
|
68
68
|
// Required Dependencies
|
|
69
|
-
apibara: "
|
|
70
|
-
"@apibara/
|
|
71
|
-
"@apibara/protocol": "^2.1.0-beta.2",
|
|
69
|
+
apibara: "next",
|
|
70
|
+
"@apibara/protocol": "next",
|
|
72
71
|
// Chain Dependencies
|
|
73
|
-
"@apibara/evm": "
|
|
74
|
-
"@apibara/beaconchain": "
|
|
75
|
-
"@apibara/starknet": "
|
|
72
|
+
"@apibara/evm": "next",
|
|
73
|
+
"@apibara/beaconchain": "next",
|
|
74
|
+
"@apibara/starknet": "next",
|
|
76
75
|
// Storage Dependencies
|
|
77
|
-
"@apibara/plugin-drizzle": "
|
|
78
|
-
"@apibara/plugin-mongo": "
|
|
79
|
-
"@apibara/plugin-sqlite": "
|
|
76
|
+
"@apibara/plugin-drizzle": "next",
|
|
77
|
+
"@apibara/plugin-mongo": "next",
|
|
78
|
+
"@apibara/plugin-sqlite": "next",
|
|
80
79
|
// Postgres Dependencies
|
|
81
80
|
"@electric-sql/pglite": "^0.2.17",
|
|
82
|
-
"drizzle-orm": "^0.
|
|
81
|
+
"drizzle-orm": "^0.40.1",
|
|
83
82
|
pg: "^8.13.1",
|
|
84
83
|
"@types/pg": "^8.11.10",
|
|
85
84
|
"drizzle-kit": "^0.29.0",
|
|
86
85
|
// Typescript Dependencies
|
|
87
86
|
typescript: "^5.6.2",
|
|
88
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
89
87
|
"@types/node": "^20.5.2",
|
|
90
88
|
};
|
|
91
89
|
|
package/src/create/init.ts
CHANGED
|
@@ -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,
|
|
@@ -129,7 +130,7 @@ export async function initializeProject({
|
|
|
129
130
|
JSON.stringify(packageJson, null, 2) + "\n",
|
|
130
131
|
);
|
|
131
132
|
await formatFile(packageJsonPath);
|
|
132
|
-
consola.success("Created
|
|
133
|
+
consola.success("Created", cyan("package.json"));
|
|
133
134
|
|
|
134
135
|
// Generate tsconfig.json if TypeScript
|
|
135
136
|
if (isTs) {
|
|
@@ -137,7 +138,7 @@ export async function initializeProject({
|
|
|
137
138
|
const tsConfig = generateTsConfig();
|
|
138
139
|
fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2) + "\n");
|
|
139
140
|
await formatFile(tsConfigPath);
|
|
140
|
-
consola.success("Created
|
|
141
|
+
consola.success("Created", cyan("tsconfig.json"));
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
const apibaraConfigPath = path.join(root, `apibara.config.${configExt}`);
|
|
@@ -145,7 +146,7 @@ export async function initializeProject({
|
|
|
145
146
|
const apibaraConfig = generateApibaraConfig(isTs);
|
|
146
147
|
fs.writeFileSync(apibaraConfigPath, apibaraConfig);
|
|
147
148
|
await formatFile(apibaraConfigPath);
|
|
148
|
-
consola.success("Created
|
|
149
|
+
consola.success("Created", cyan(`apibara.config.${configExt}`));
|
|
149
150
|
|
|
150
151
|
// Create "indexers" directory if not exists
|
|
151
152
|
const indexersDir = path.join(root, "indexers");
|
|
@@ -154,6 +155,8 @@ export async function initializeProject({
|
|
|
154
155
|
consola.success(`Created ${cyan("indexers")} directory`);
|
|
155
156
|
}
|
|
156
157
|
|
|
158
|
+
await createGitIgnoreFile(root);
|
|
159
|
+
|
|
157
160
|
console.log("\n");
|
|
158
161
|
|
|
159
162
|
consola.ready(green("Project initialized successfully"));
|
|
@@ -163,12 +166,12 @@ export async function initializeProject({
|
|
|
163
166
|
if (!argNoCreateIndexer) {
|
|
164
167
|
consola.info("Let's create an indexer\n");
|
|
165
168
|
|
|
166
|
-
await addIndexer({});
|
|
169
|
+
await addIndexer({ argRootDir: argTargetDir });
|
|
167
170
|
} else {
|
|
168
171
|
const pkgManager = getPackageManager();
|
|
169
172
|
consola.info(
|
|
170
173
|
"Run ",
|
|
171
|
-
green(`${pkgManager.name}
|
|
174
|
+
green(`${pkgManager.name} install`),
|
|
172
175
|
" to install all dependencies",
|
|
173
176
|
);
|
|
174
177
|
}
|