apibara 2.1.0-beta.4 → 2.1.0-beta.41
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 +16 -8
- package/dist/chunks/add.mjs.map +1 -0
- package/dist/chunks/build.mjs +4 -2
- package/dist/chunks/build.mjs.map +1 -0
- package/dist/chunks/dev.mjs +55 -21
- package/dist/chunks/dev.mjs.map +1 -0
- package/dist/chunks/init.mjs +11 -7
- package/dist/chunks/init.mjs.map +1 -0
- package/dist/chunks/prepare.mjs +4 -2
- package/dist/chunks/prepare.mjs.map +1 -0
- package/dist/chunks/start.mjs +16 -4
- 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 +33 -0
- package/dist/common/index.d.ts +33 -0
- package/dist/common/index.mjs +91 -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 +134 -69
- 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 +168 -139
- 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 +141 -0
- package/dist/rolldown/index.mjs.map +1 -0
- package/dist/runtime/dev.mjs +39 -17
- package/dist/runtime/internal/app.d.ts +14 -1
- package/dist/runtime/internal/app.mjs +26 -21
- package/dist/runtime/project-info.d.ts +3 -0
- package/dist/runtime/project-info.mjs +67 -0
- package/dist/runtime/start.mjs +78 -11
- 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 -3
- package/src/cli/commands/start.ts +18 -3
- package/src/cli/commands/write-project-info.ts +56 -0
- package/src/cli/index.ts +2 -0
- package/src/common/cli.ts +40 -0
- package/src/common/constants.ts +6 -0
- package/src/common/helper.ts +86 -0
- package/src/common/index.ts +3 -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/core/scan.ts +1 -1
- package/src/create/add.ts +14 -20
- package/src/create/constants.ts +5 -7
- package/src/create/init.ts +8 -5
- package/src/create/templates.ts +144 -116
- package/src/create/types.ts +3 -0
- package/src/create/utils.ts +20 -7
- 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 +86 -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/rolldown/plugins/static-config.ts +21 -0
- package/src/runtime/dev.ts +49 -19
- package/src/runtime/internal/app.ts +42 -29
- package/src/runtime/project-info.ts +90 -0
- package/src/runtime/start.ts +91 -11
- 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/src/types/virtual/static-config.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/cli/common.ts +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
package/src/cli/commands/dev.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { type ChildProcess, spawn } from "node:child_process";
|
|
2
|
+
import { checkForUnknownArgs, commonArgs } from "apibara/common";
|
|
2
3
|
import { build, createApibara, prepare, writeTypes } from "apibara/core";
|
|
3
4
|
import { runtimeDir } from "apibara/runtime/meta";
|
|
4
5
|
import type { Apibara } from "apibara/types";
|
|
5
6
|
import { defineCommand } from "citty";
|
|
6
7
|
import { colors } from "consola/utils";
|
|
7
8
|
import { join, resolve } from "pathe";
|
|
8
|
-
import {
|
|
9
|
+
import { blueBright, gray } from "../../create/colors";
|
|
9
10
|
|
|
10
11
|
// Hot module reloading key regex
|
|
11
|
-
// for only runtimeConfig.* keys
|
|
12
|
-
const hmrKeyRe = /^runtimeConfig
|
|
12
|
+
// for only runtimeConfig.*, presets.* keys & preset key
|
|
13
|
+
const hmrKeyRe = /^(runtimeConfig\.|presets\.|preset$)/;
|
|
13
14
|
|
|
14
15
|
export default defineCommand({
|
|
15
16
|
meta: {
|
|
@@ -26,10 +27,29 @@ export default defineCommand({
|
|
|
26
27
|
type: "string",
|
|
27
28
|
description: "Preset to use",
|
|
28
29
|
},
|
|
30
|
+
"always-reindex": {
|
|
31
|
+
type: "boolean",
|
|
32
|
+
default: false,
|
|
33
|
+
description:
|
|
34
|
+
"Reindex the indexers from the starting block on every restart | default: `false`",
|
|
35
|
+
},
|
|
29
36
|
},
|
|
30
|
-
async run({ args }) {
|
|
37
|
+
async run({ args, data, cmd, rawArgs }) {
|
|
38
|
+
await checkForUnknownArgs(args, cmd);
|
|
39
|
+
|
|
31
40
|
const rootDir = resolve((args.dir || args._dir || ".") as string);
|
|
32
41
|
|
|
42
|
+
if (args["always-reindex"]) {
|
|
43
|
+
process.env.APIBARA_ALWAYS_REINDEX = "true";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const selectedIndexers = new Set(
|
|
47
|
+
args.indexers
|
|
48
|
+
?.split(",")
|
|
49
|
+
.map((i) => i.trim())
|
|
50
|
+
.sort() ?? [],
|
|
51
|
+
);
|
|
52
|
+
|
|
33
53
|
let apibara: Apibara;
|
|
34
54
|
let childProcess: ChildProcess | undefined;
|
|
35
55
|
|
|
@@ -46,6 +66,7 @@ export default defineCommand({
|
|
|
46
66
|
apibara = await createApibara(
|
|
47
67
|
{
|
|
48
68
|
rootDir,
|
|
69
|
+
preset: args.preset,
|
|
49
70
|
},
|
|
50
71
|
{
|
|
51
72
|
watch: true,
|
|
@@ -58,12 +79,12 @@ export default defineCommand({
|
|
|
58
79
|
}
|
|
59
80
|
|
|
60
81
|
apibara.logger.info(
|
|
61
|
-
`Config updated:
|
|
62
|
-
${diff.map((entry) => ` ${entry.toString()}`).join("\n")}`,
|
|
82
|
+
`Config updated: \n${diff.map((entry) => ` ${entry.toString()}`).join("\n")}`,
|
|
63
83
|
);
|
|
64
84
|
|
|
65
85
|
await (diff.every((e) => hmrKeyRe.test(e.key))
|
|
66
|
-
? apibara
|
|
86
|
+
? // in hot reload we only update the runtime values & restart indexers,no build step, apibara instance remains the same
|
|
87
|
+
apibara.updateConfig(newConfig.config || {}) // Hot reload
|
|
67
88
|
: reload()); // Full reload
|
|
68
89
|
},
|
|
69
90
|
},
|
|
@@ -71,7 +92,9 @@ export default defineCommand({
|
|
|
71
92
|
true,
|
|
72
93
|
);
|
|
73
94
|
|
|
74
|
-
apibara.hooks.hookOnce("restart",
|
|
95
|
+
apibara.hooks.hookOnce("restart", async () => {
|
|
96
|
+
await reload();
|
|
97
|
+
});
|
|
75
98
|
|
|
76
99
|
apibara.options.entry = join(runtimeDir, "dev.mjs");
|
|
77
100
|
|
|
@@ -79,38 +102,50 @@ export default defineCommand({
|
|
|
79
102
|
await writeTypes(apibara);
|
|
80
103
|
await build(apibara);
|
|
81
104
|
|
|
82
|
-
apibara.hooks.hook("dev:restart", () => {
|
|
105
|
+
apibara.hooks.hook("dev:restart", async () => {
|
|
83
106
|
if (childProcess) {
|
|
84
107
|
apibara.logger.info("Change detected, stopping indexers to restart");
|
|
85
|
-
childProcess
|
|
108
|
+
await killProcess(childProcess);
|
|
86
109
|
childProcess = undefined;
|
|
87
110
|
}
|
|
88
111
|
});
|
|
89
112
|
|
|
90
|
-
apibara.hooks.hook("dev:reload", () => {
|
|
113
|
+
apibara.hooks.hook("dev:reload", async () => {
|
|
91
114
|
if (childProcess) {
|
|
92
|
-
childProcess
|
|
115
|
+
await killProcess(childProcess);
|
|
116
|
+
childProcess = undefined;
|
|
117
|
+
apibara.logger.info("Restarting indexers");
|
|
93
118
|
} else {
|
|
94
|
-
apibara.logger.
|
|
119
|
+
apibara.logger.info("Starting indexers");
|
|
120
|
+
|
|
121
|
+
const indexersText = apibara.indexers
|
|
122
|
+
.map((i) =>
|
|
123
|
+
selectedIndexers.has(i.name) || selectedIndexers.size === 0
|
|
124
|
+
? blueBright(i.name)
|
|
125
|
+
: gray(i.name),
|
|
126
|
+
)
|
|
127
|
+
.join(", ");
|
|
128
|
+
apibara.logger.info("Indexers:", indexersText);
|
|
95
129
|
}
|
|
96
130
|
|
|
97
131
|
const childArgs = [
|
|
98
132
|
resolve(apibara.options.outputDir || "./.apibara/build", "dev.mjs"),
|
|
99
133
|
"start",
|
|
100
134
|
...(args.indexers ? ["--indexers", args.indexers] : []),
|
|
101
|
-
...(args.preset ? ["--preset", args.preset] : []),
|
|
102
135
|
];
|
|
103
136
|
|
|
104
137
|
childProcess = spawn("node", childArgs, {
|
|
105
138
|
stdio: "inherit",
|
|
106
139
|
});
|
|
107
140
|
|
|
108
|
-
childProcess.on("close", (code) => {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
141
|
+
childProcess.on("close", (code, signal) => {
|
|
142
|
+
childProcess = undefined;
|
|
143
|
+
console.log();
|
|
144
|
+
apibara.logger.info(
|
|
145
|
+
`Indexers process exited${
|
|
146
|
+
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
147
|
+
}`,
|
|
148
|
+
);
|
|
114
149
|
});
|
|
115
150
|
});
|
|
116
151
|
};
|
|
@@ -118,3 +153,12 @@ export default defineCommand({
|
|
|
118
153
|
await reload();
|
|
119
154
|
},
|
|
120
155
|
});
|
|
156
|
+
|
|
157
|
+
async function killProcess(childProcess: ChildProcess | undefined) {
|
|
158
|
+
if (childProcess) {
|
|
159
|
+
await new Promise((resolve) => {
|
|
160
|
+
childProcess.once("exit", resolve);
|
|
161
|
+
childProcess.kill();
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
package/src/cli/commands/init.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { checkForUnknownArgs } from "apibara/common";
|
|
1
2
|
import { initializeProject } from "apibara/create";
|
|
2
3
|
import { defineCommand } from "citty";
|
|
3
4
|
|
|
@@ -14,23 +15,27 @@ export default defineCommand({
|
|
|
14
15
|
},
|
|
15
16
|
language: {
|
|
16
17
|
type: "string",
|
|
17
|
-
description:
|
|
18
|
+
description:
|
|
19
|
+
"Language to use: typescript, ts or javascript, js | default: `ts`",
|
|
18
20
|
default: "ts",
|
|
19
21
|
alias: "l",
|
|
20
22
|
},
|
|
21
|
-
|
|
23
|
+
"create-indexer": {
|
|
22
24
|
type: "boolean",
|
|
23
|
-
|
|
24
|
-
default:
|
|
25
|
+
name: "create-indexer",
|
|
26
|
+
default: true,
|
|
27
|
+
description: "TODO",
|
|
25
28
|
},
|
|
26
29
|
},
|
|
27
|
-
async run({ args }) {
|
|
28
|
-
|
|
30
|
+
async run({ args, cmd }) {
|
|
31
|
+
await checkForUnknownArgs(args, cmd);
|
|
32
|
+
|
|
33
|
+
const { dir: targetDir, "create-indexer": createIndexer, language } = args;
|
|
29
34
|
|
|
30
35
|
await initializeProject({
|
|
31
36
|
argTargetDir: targetDir,
|
|
32
37
|
argLanguage: language,
|
|
33
|
-
argNoCreateIndexer:
|
|
38
|
+
argNoCreateIndexer: !createIndexer,
|
|
34
39
|
});
|
|
35
40
|
},
|
|
36
41
|
});
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { checkForUnknownArgs, commonArgs } from "apibara/common";
|
|
1
2
|
import { createApibara, writeTypes } from "apibara/core";
|
|
2
|
-
import {} from "apibara/types";
|
|
3
3
|
import { defineCommand } from "citty";
|
|
4
4
|
import { resolve } from "pathe";
|
|
5
|
-
import { commonArgs } from "../common";
|
|
6
5
|
|
|
7
6
|
export default defineCommand({
|
|
8
7
|
meta: {
|
|
@@ -12,7 +11,9 @@ export default defineCommand({
|
|
|
12
11
|
args: {
|
|
13
12
|
...commonArgs,
|
|
14
13
|
},
|
|
15
|
-
async run({ args }) {
|
|
14
|
+
async run({ args, cmd }) {
|
|
15
|
+
await checkForUnknownArgs(args, cmd);
|
|
16
|
+
|
|
16
17
|
const rootDir = resolve((args.dir || ".") as string);
|
|
17
18
|
const apibara = await createApibara({ rootDir });
|
|
18
19
|
await writeTypes(apibara);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { checkForUnknownArgs, commonArgs } from "apibara/common";
|
|
2
3
|
import { createApibara } from "apibara/core";
|
|
3
4
|
import { defineCommand } from "citty";
|
|
5
|
+
import { colors } from "consola/utils";
|
|
4
6
|
import fse from "fs-extra";
|
|
5
7
|
import { resolve } from "pathe";
|
|
6
|
-
import { commonArgs } from "../common";
|
|
7
8
|
|
|
8
9
|
export default defineCommand({
|
|
9
10
|
meta: {
|
|
@@ -22,12 +23,15 @@ export default defineCommand({
|
|
|
22
23
|
description: "The preset to use",
|
|
23
24
|
},
|
|
24
25
|
},
|
|
25
|
-
async run({ args }) {
|
|
26
|
+
async run({ args, cmd }) {
|
|
26
27
|
const { indexer, preset } = args;
|
|
27
28
|
const rootDir = resolve((args.dir || args._dir || ".") as string);
|
|
28
29
|
|
|
30
|
+
await checkForUnknownArgs(args, cmd);
|
|
31
|
+
|
|
29
32
|
const apibara = await createApibara({
|
|
30
33
|
rootDir,
|
|
34
|
+
preset,
|
|
31
35
|
});
|
|
32
36
|
|
|
33
37
|
apibara.logger.start(
|
|
@@ -51,11 +55,22 @@ export default defineCommand({
|
|
|
51
55
|
"start",
|
|
52
56
|
"--indexer",
|
|
53
57
|
indexer,
|
|
58
|
+
// important: this is required to run the indexer with apibara cli to load runtime values correctly
|
|
59
|
+
"--no-standalone",
|
|
54
60
|
...(preset ? ["--preset", preset] : []),
|
|
55
61
|
];
|
|
56
62
|
|
|
57
|
-
spawn("node", childArgs, {
|
|
63
|
+
const childProcess = spawn("node", childArgs, {
|
|
58
64
|
stdio: "inherit",
|
|
59
65
|
});
|
|
66
|
+
|
|
67
|
+
childProcess.on("close", (code, signal) => {
|
|
68
|
+
console.log();
|
|
69
|
+
apibara.logger.info(
|
|
70
|
+
`Indexers process exited${
|
|
71
|
+
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
72
|
+
}`,
|
|
73
|
+
);
|
|
74
|
+
});
|
|
60
75
|
},
|
|
61
76
|
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { checkForUnknownArgs, commonArgs } from "apibara/common";
|
|
4
|
+
import { build, createApibara, prepare, writeTypes } from "apibara/core";
|
|
5
|
+
import { runtimeDir } from "apibara/runtime/meta";
|
|
6
|
+
import { defineCommand } from "citty";
|
|
7
|
+
import consola from "consola";
|
|
8
|
+
import { resolve } from "pathe";
|
|
9
|
+
|
|
10
|
+
export default defineCommand({
|
|
11
|
+
meta: {
|
|
12
|
+
name: "write-project-info",
|
|
13
|
+
description: "Write json-encoded information about the project.",
|
|
14
|
+
},
|
|
15
|
+
args: {
|
|
16
|
+
...commonArgs,
|
|
17
|
+
},
|
|
18
|
+
async run({ args, cmd }) {
|
|
19
|
+
await checkForUnknownArgs(args, cmd);
|
|
20
|
+
|
|
21
|
+
consola.start("Generating `project-info.json`");
|
|
22
|
+
|
|
23
|
+
const rootDir = resolve((args.dir || ".") as string);
|
|
24
|
+
const apibara = await createApibara({ rootDir, disableLogs: true });
|
|
25
|
+
|
|
26
|
+
apibara.options.entry = join(runtimeDir, "project-info.mjs");
|
|
27
|
+
|
|
28
|
+
await prepare(apibara);
|
|
29
|
+
await writeTypes(apibara);
|
|
30
|
+
await build(apibara);
|
|
31
|
+
|
|
32
|
+
const childArgs = [
|
|
33
|
+
resolve(
|
|
34
|
+
apibara.options.outputDir || "./.apibara/build",
|
|
35
|
+
"project-info.mjs",
|
|
36
|
+
),
|
|
37
|
+
"start",
|
|
38
|
+
"--build-dir",
|
|
39
|
+
apibara.options.buildDir,
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const child = spawn("node", childArgs, {
|
|
43
|
+
stdio: "inherit",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
child.on("close", (code) => {
|
|
47
|
+
if (code === 0) {
|
|
48
|
+
consola.success("Project info written to `.apibara/project-info.json`");
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
child.on("error", (error) => {
|
|
53
|
+
consola.error(`Failed to write project info: ${error.message}`, error);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
package/src/cli/index.ts
CHANGED
|
@@ -13,6 +13,8 @@ export const mainCli = defineCommand({
|
|
|
13
13
|
prepare: () => import("./commands/prepare").then((r) => r.default),
|
|
14
14
|
init: () => import("./commands/init").then((r) => r.default),
|
|
15
15
|
add: () => import("./commands/add").then((r) => r.default),
|
|
16
|
+
"write-project-info": () =>
|
|
17
|
+
import("./commands/write-project-info").then((r) => r.default),
|
|
16
18
|
},
|
|
17
19
|
});
|
|
18
20
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgsDef,
|
|
3
|
+
type CommandDef,
|
|
4
|
+
type ParsedArgs,
|
|
5
|
+
renderUsage,
|
|
6
|
+
} from "citty";
|
|
7
|
+
import consola from "consola";
|
|
8
|
+
|
|
9
|
+
export const commonArgs = <ArgsDef>{
|
|
10
|
+
dir: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "project root directory",
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const checkForUnknownArgs = async <T extends ArgsDef = ArgsDef>(
|
|
17
|
+
args: ParsedArgs<T>,
|
|
18
|
+
cmd: CommandDef<T>,
|
|
19
|
+
) => {
|
|
20
|
+
// Create a list of defined args including both the main arg names and their aliases
|
|
21
|
+
const definedArgs: string[] = [];
|
|
22
|
+
if (cmd.args) {
|
|
23
|
+
for (const [argName, argDef] of Object.entries(cmd.args)) {
|
|
24
|
+
definedArgs.push(argName);
|
|
25
|
+
// Add alias to definedArgs if it exists
|
|
26
|
+
if (argDef.alias) {
|
|
27
|
+
definedArgs.push(argDef.alias);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const providedArgs = Object.keys(args).filter((arg) => arg !== "_");
|
|
33
|
+
const wrongArgs = providedArgs.filter((arg) => !definedArgs.includes(arg));
|
|
34
|
+
|
|
35
|
+
if (wrongArgs.length > 0) {
|
|
36
|
+
consola.error(`Unknown arguments: ${wrongArgs.join(", ")}`);
|
|
37
|
+
consola.info(await renderUsage(cmd));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const ENV_INTERNAL_APIBARA_RUNTIME = "_APIBARA_RUNTIME";
|
|
2
|
+
export const ENV_INTERNAL_APIBARA_PRESET = "_APIBARA_PRESET";
|
|
3
|
+
export const ENV_INTERNAL_APIBARA_PRESETS = "_APIBARA_PRESETS";
|
|
4
|
+
export const ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME =
|
|
5
|
+
"_APIBARA_PROCESSED_RUNTIME";
|
|
6
|
+
export const USER_ENV_APIBARA_RUNTIME_CONFIG = "APIBARA_RUNTIME_CONFIG";
|
|
@@ -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|.mjs)",
|
|
107
|
+
];
|