apibara 2.1.0-beta.1 → 2.1.0-beta.4
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/init.mjs +3 -7
- package/dist/create/index.mjs +60 -40
- package/package.json +5 -4
- package/src/cli/commands/init.ts +3 -7
- package/src/create/add.ts +17 -4
- package/src/create/constants.ts +9 -9
- package/src/create/init.ts +21 -10
- package/src/create/templates.ts +29 -26
- package/src/create/utils.ts +10 -0
package/dist/chunks/init.mjs
CHANGED
|
@@ -18,22 +18,18 @@ const init = defineCommand({
|
|
|
18
18
|
default: "ts",
|
|
19
19
|
alias: "l"
|
|
20
20
|
},
|
|
21
|
-
|
|
21
|
+
noIndexer: {
|
|
22
22
|
type: "boolean",
|
|
23
23
|
description: "Do not create an indexer after initialization",
|
|
24
24
|
default: false
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
async run({ args }) {
|
|
28
|
-
const {
|
|
29
|
-
dir: targetDir,
|
|
30
|
-
"no-create-indexer": noCreateIndexer,
|
|
31
|
-
language
|
|
32
|
-
} = args;
|
|
28
|
+
const { dir: targetDir, noIndexer, language } = args;
|
|
33
29
|
await initializeProject({
|
|
34
30
|
argTargetDir: targetDir,
|
|
35
31
|
argLanguage: language,
|
|
36
|
-
argNoCreateIndexer:
|
|
32
|
+
argNoCreateIndexer: noIndexer
|
|
37
33
|
});
|
|
38
34
|
}
|
|
39
35
|
});
|
package/dist/create/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import path, { basename } from 'node:path';
|
|
1
2
|
import consola$1, { consola } from 'consola';
|
|
2
3
|
import prompts from 'prompts';
|
|
3
4
|
import colors from 'picocolors';
|
|
4
5
|
import fs from 'node:fs';
|
|
5
|
-
import path, { basename } from 'node:path';
|
|
6
6
|
import { Project, SyntaxKind } from 'ts-morph';
|
|
7
|
+
import * as prettier from 'prettier';
|
|
7
8
|
|
|
8
9
|
const {
|
|
9
10
|
blue,
|
|
@@ -56,17 +57,17 @@ const storages = [
|
|
|
56
57
|
];
|
|
57
58
|
const packageVersions = {
|
|
58
59
|
// Required Dependencies
|
|
59
|
-
apibara: "
|
|
60
|
-
"@apibara/indexer": "
|
|
61
|
-
"@apibara/protocol": "
|
|
60
|
+
apibara: "next",
|
|
61
|
+
"@apibara/indexer": "next",
|
|
62
|
+
"@apibara/protocol": "next",
|
|
62
63
|
// Chain Dependencies
|
|
63
|
-
"@apibara/evm": "
|
|
64
|
-
"@apibara/beaconchain": "
|
|
65
|
-
"@apibara/starknet": "
|
|
64
|
+
"@apibara/evm": "next",
|
|
65
|
+
"@apibara/beaconchain": "next",
|
|
66
|
+
"@apibara/starknet": "next",
|
|
66
67
|
// Storage Dependencies
|
|
67
|
-
"@apibara/plugin-drizzle": "
|
|
68
|
-
"@apibara/plugin-mongo": "
|
|
69
|
-
"@apibara/plugin-sqlite": "
|
|
68
|
+
"@apibara/plugin-drizzle": "next",
|
|
69
|
+
"@apibara/plugin-mongo": "next",
|
|
70
|
+
"@apibara/plugin-sqlite": "next",
|
|
70
71
|
// Postgres Dependencies
|
|
71
72
|
"@electric-sql/pglite": "^0.2.17",
|
|
72
73
|
"drizzle-orm": "^0.37.0",
|
|
@@ -369,6 +370,14 @@ function pkgFromUserAgent(userAgent) {
|
|
|
369
370
|
version: pkgSpecArr[1]
|
|
370
371
|
};
|
|
371
372
|
}
|
|
373
|
+
async function formatFile(path2) {
|
|
374
|
+
const file = fs.readFileSync(path2, "utf8");
|
|
375
|
+
const formatted = await prettier.format(file, {
|
|
376
|
+
filepath: path2,
|
|
377
|
+
tabWidth: 2
|
|
378
|
+
});
|
|
379
|
+
fs.writeFileSync(path2, formatted);
|
|
380
|
+
}
|
|
372
381
|
|
|
373
382
|
function generatePackageJson(isTypeScript) {
|
|
374
383
|
return {
|
|
@@ -434,11 +443,11 @@ function generateIndexer({
|
|
|
434
443
|
chain,
|
|
435
444
|
language
|
|
436
445
|
}) {
|
|
437
|
-
return
|
|
438
|
-
import {
|
|
446
|
+
return `import { defineIndexer } from "@apibara/indexer";
|
|
447
|
+
import { useLogger } from "@apibara/indexer/plugins";
|
|
439
448
|
${storage === "postgres" ? `import { drizzleStorage } from "@apibara/plugin-drizzle";` : ""}
|
|
449
|
+
${chain === "ethereum" ? `import { EvmStream } from "@apibara/evm";` : chain === "beaconchain" ? `import { BeaconChainStream } from "@apibara/beaconchain";` : chain === "starknet" ? `import { StarknetStream } from "@apibara/starknet";` : ""}
|
|
440
450
|
${language === "typescript" ? `import type { ApibaraRuntimeConfig } from "apibara/types";` : ""}
|
|
441
|
-
import { useLogger } from "@apibara/indexer/plugins";
|
|
442
451
|
${storage === "postgres" ? `import { getDrizzlePgDatabase } from "../lib/db";` : ""}
|
|
443
452
|
|
|
444
453
|
|
|
@@ -493,16 +502,16 @@ async function createIndexerFile(options) {
|
|
|
493
502
|
const indexerContent = generateIndexer(options);
|
|
494
503
|
fs.mkdirSync(path.dirname(indexerFilePath), { recursive: true });
|
|
495
504
|
fs.writeFileSync(indexerFilePath, indexerContent);
|
|
505
|
+
await formatFile(indexerFilePath);
|
|
496
506
|
}
|
|
497
|
-
function updatePackageJson({
|
|
507
|
+
async function updatePackageJson({
|
|
498
508
|
cwd,
|
|
499
509
|
chain,
|
|
500
510
|
storage,
|
|
501
511
|
language
|
|
502
512
|
}) {
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
);
|
|
513
|
+
const packageJsonPath = path.join(cwd, "package.json");
|
|
514
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
506
515
|
if (chain === "ethereum") {
|
|
507
516
|
packageJson.dependencies["@apibara/evm"] = packageVersions["@apibara/evm"];
|
|
508
517
|
} else if (chain === "beaconchain") {
|
|
@@ -522,12 +531,10 @@ function updatePackageJson({
|
|
|
522
531
|
packageJson.devDependencies["@types/pg"] = packageVersions["@types/pg"];
|
|
523
532
|
}
|
|
524
533
|
}
|
|
525
|
-
fs.writeFileSync(
|
|
526
|
-
|
|
527
|
-
JSON.stringify(packageJson, null, 2)
|
|
528
|
-
);
|
|
534
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
535
|
+
await formatFile(packageJsonPath);
|
|
529
536
|
}
|
|
530
|
-
function updateApibaraConfigFile({
|
|
537
|
+
async function updateApibaraConfigFile({
|
|
531
538
|
indexerId,
|
|
532
539
|
cwd,
|
|
533
540
|
chain,
|
|
@@ -571,11 +578,7 @@ function updateApibaraConfigFile({
|
|
|
571
578
|
});
|
|
572
579
|
}
|
|
573
580
|
sourceFile.saveSync();
|
|
574
|
-
|
|
575
|
-
tabSize: 2,
|
|
576
|
-
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: true,
|
|
577
|
-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true
|
|
578
|
-
});
|
|
581
|
+
await formatFile(pathToConfig);
|
|
579
582
|
}
|
|
580
583
|
async function createDrizzleStorageFiles(options) {
|
|
581
584
|
const { cwd, language, storage } = options;
|
|
@@ -600,6 +603,7 @@ export default {
|
|
|
600
603
|
},
|
|
601
604
|
}${language === "typescript" ? " satisfies Config" : ""};`;
|
|
602
605
|
fs.writeFileSync(drizzleConfigPath, drizzleConfigContent);
|
|
606
|
+
await formatFile(drizzleConfigPath);
|
|
603
607
|
consola.success(`Created ${cyan(drizzleConfigFileName)}`);
|
|
604
608
|
}
|
|
605
609
|
const schemaFileName = `schema.${fileExtension}`;
|
|
@@ -624,6 +628,7 @@ export {};
|
|
|
624
628
|
`;
|
|
625
629
|
fs.mkdirSync(path.dirname(schemaPath), { recursive: true });
|
|
626
630
|
fs.writeFileSync(schemaPath, schemaContent);
|
|
631
|
+
await formatFile(schemaPath);
|
|
627
632
|
consola.success(`Created ${cyan("lib/schema.ts")}`);
|
|
628
633
|
}
|
|
629
634
|
const dbFileName = `db.${fileExtension}`;
|
|
@@ -665,6 +670,7 @@ export function getDrizzlePgDatabase(connectionString${language === "typescript"
|
|
|
665
670
|
}`;
|
|
666
671
|
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
667
672
|
fs.writeFileSync(dbPath, dbContent);
|
|
673
|
+
await formatFile(dbPath);
|
|
668
674
|
consola.success(`Created ${cyan(`lib/${dbFileName}`)}`);
|
|
669
675
|
}
|
|
670
676
|
console.log("\n");
|
|
@@ -776,26 +782,32 @@ async function initializeProject({
|
|
|
776
782
|
consola$1.info(`Initializing project in ${argTargetDir}
|
|
777
783
|
|
|
778
784
|
`);
|
|
785
|
+
const packageJsonPath = path.join(root, "package.json");
|
|
779
786
|
const packageJson = generatePackageJson(isTs);
|
|
780
787
|
fs.writeFileSync(
|
|
781
|
-
|
|
788
|
+
packageJsonPath,
|
|
782
789
|
JSON.stringify(packageJson, null, 2) + "\n"
|
|
783
790
|
);
|
|
791
|
+
await formatFile(packageJsonPath);
|
|
784
792
|
consola$1.success("Created ", cyan("package.json"));
|
|
785
793
|
if (isTs) {
|
|
794
|
+
const tsConfigPath = path.join(root, "tsconfig.json");
|
|
786
795
|
const tsConfig = generateTsConfig();
|
|
787
|
-
fs.writeFileSync(
|
|
788
|
-
|
|
789
|
-
JSON.stringify(tsConfig, null, 2) + "\n"
|
|
790
|
-
);
|
|
796
|
+
fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2) + "\n");
|
|
797
|
+
await formatFile(tsConfigPath);
|
|
791
798
|
consola$1.success("Created ", cyan("tsconfig.json"));
|
|
792
799
|
}
|
|
800
|
+
const apibaraConfigPath = path.join(root, `apibara.config.${configExt}`);
|
|
793
801
|
const apibaraConfig = generateApibaraConfig(isTs);
|
|
794
|
-
fs.writeFileSync(
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
);
|
|
798
|
-
|
|
802
|
+
fs.writeFileSync(apibaraConfigPath, apibaraConfig);
|
|
803
|
+
await formatFile(apibaraConfigPath);
|
|
804
|
+
consola$1.success("Created ", cyan(`apibara.config.${configExt}`));
|
|
805
|
+
const indexersDir = path.join(root, "indexers");
|
|
806
|
+
if (!fs.existsSync(indexersDir)) {
|
|
807
|
+
fs.mkdirSync(indexersDir, { recursive: true });
|
|
808
|
+
consola$1.success(`Created ${cyan("indexers")} directory`);
|
|
809
|
+
}
|
|
810
|
+
console.log("\n");
|
|
799
811
|
consola$1.ready(green("Project initialized successfully"));
|
|
800
812
|
console.log();
|
|
801
813
|
if (!argNoCreateIndexer) {
|
|
@@ -855,7 +867,15 @@ async function addIndexer({
|
|
|
855
867
|
name: "prompt_indexerId",
|
|
856
868
|
message: reset("Indexer ID:"),
|
|
857
869
|
initial: argIndexerId ?? "my-indexer",
|
|
858
|
-
validate: (id) => validateIndexerId(id)
|
|
870
|
+
validate: (id) => validateIndexerId(id) ? checkFileExists(
|
|
871
|
+
path.join(
|
|
872
|
+
process.cwd(),
|
|
873
|
+
"indexers",
|
|
874
|
+
`${id}.indexer.${language === "typescript" ? "ts" : "js"}`
|
|
875
|
+
)
|
|
876
|
+
).then(
|
|
877
|
+
({ exists }) => exists ? `Indexer ${cyan(`${id}.indexer.${language === "typescript" ? "ts" : "js"}`)} already exists` : true
|
|
878
|
+
) : "Invalid indexer ID, it cannot be empty and must be in kebab-case format"
|
|
859
879
|
},
|
|
860
880
|
{
|
|
861
881
|
type: argChain ? null : "select",
|
|
@@ -941,11 +961,11 @@ async function addIndexer({
|
|
|
941
961
|
language,
|
|
942
962
|
packageManager: pkgManager.name
|
|
943
963
|
};
|
|
944
|
-
updateApibaraConfigFile(options);
|
|
964
|
+
await updateApibaraConfigFile(options);
|
|
945
965
|
consola$1.success(
|
|
946
966
|
`Updated ${cyan("apibara.config." + (language === "typescript" ? "ts" : "js"))}`
|
|
947
967
|
);
|
|
948
|
-
updatePackageJson(options);
|
|
968
|
+
await updatePackageJson(options);
|
|
949
969
|
consola$1.success(`Updated ${cyan("package.json")}`);
|
|
950
970
|
await createIndexerFile(options);
|
|
951
971
|
consola$1.success(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apibara",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/core/index.mjs",
|
|
6
6
|
"exports": {
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"playground:start": "pnpm playground start --dir playground --indexer starknet"
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
|
-
"@apibara/starknet": "2.1.0-beta.
|
|
79
|
+
"@apibara/starknet": "2.1.0-beta.4",
|
|
80
80
|
"@types/fs-extra": "^11.0.4",
|
|
81
81
|
"@types/node": "^20.14.0",
|
|
82
82
|
"@types/prompts": "^2.4.9",
|
|
@@ -87,8 +87,8 @@
|
|
|
87
87
|
"vitest": "^1.6.0"
|
|
88
88
|
},
|
|
89
89
|
"dependencies": {
|
|
90
|
-
"@apibara/indexer": "2.1.0-beta.
|
|
91
|
-
"@apibara/protocol": "2.1.0-beta.
|
|
90
|
+
"@apibara/indexer": "2.1.0-beta.4",
|
|
91
|
+
"@apibara/protocol": "2.1.0-beta.4",
|
|
92
92
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
93
93
|
"@rollup/plugin-json": "^6.1.0",
|
|
94
94
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
@@ -109,6 +109,7 @@
|
|
|
109
109
|
"perfect-debounce": "^1.0.0",
|
|
110
110
|
"picocolors": "^1.1.1",
|
|
111
111
|
"pkg-types": "^1.1.3",
|
|
112
|
+
"prettier": "^3.5.2",
|
|
112
113
|
"prompts": "^2.4.2",
|
|
113
114
|
"rollup": "^4.18.1",
|
|
114
115
|
"rollup-plugin-esbuild": "^6.1.1",
|
package/src/cli/commands/init.ts
CHANGED
|
@@ -18,23 +18,19 @@ export default defineCommand({
|
|
|
18
18
|
default: "ts",
|
|
19
19
|
alias: "l",
|
|
20
20
|
},
|
|
21
|
-
|
|
21
|
+
noIndexer: {
|
|
22
22
|
type: "boolean",
|
|
23
23
|
description: "Do not create an indexer after initialization",
|
|
24
24
|
default: false,
|
|
25
25
|
},
|
|
26
26
|
},
|
|
27
27
|
async run({ args }) {
|
|
28
|
-
const {
|
|
29
|
-
dir: targetDir,
|
|
30
|
-
"no-create-indexer": noCreateIndexer,
|
|
31
|
-
language,
|
|
32
|
-
} = args;
|
|
28
|
+
const { dir: targetDir, noIndexer, language } = args;
|
|
33
29
|
|
|
34
30
|
await initializeProject({
|
|
35
31
|
argTargetDir: targetDir,
|
|
36
32
|
argLanguage: language,
|
|
37
|
-
argNoCreateIndexer:
|
|
33
|
+
argNoCreateIndexer: noIndexer,
|
|
38
34
|
});
|
|
39
35
|
},
|
|
40
36
|
});
|
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,
|
|
@@ -88,8 +90,19 @@ export async function addIndexer({
|
|
|
88
90
|
message: reset("Indexer ID:"),
|
|
89
91
|
initial: argIndexerId ?? "my-indexer",
|
|
90
92
|
validate: (id) =>
|
|
91
|
-
validateIndexerId(id)
|
|
92
|
-
|
|
93
|
+
validateIndexerId(id)
|
|
94
|
+
? checkFileExists(
|
|
95
|
+
path.join(
|
|
96
|
+
process.cwd(),
|
|
97
|
+
"indexers",
|
|
98
|
+
`${id}.indexer.${language === "typescript" ? "ts" : "js"}`,
|
|
99
|
+
),
|
|
100
|
+
).then(({ exists }) =>
|
|
101
|
+
exists
|
|
102
|
+
? `Indexer ${cyan(`${id}.indexer.${language === "typescript" ? "ts" : "js"}`)} already exists`
|
|
103
|
+
: true,
|
|
104
|
+
)
|
|
105
|
+
: "Invalid indexer ID, it cannot be empty and must be in kebab-case format",
|
|
93
106
|
},
|
|
94
107
|
{
|
|
95
108
|
type: argChain ? null : "select",
|
|
@@ -195,13 +208,13 @@ export async function addIndexer({
|
|
|
195
208
|
packageManager: pkgManager.name,
|
|
196
209
|
};
|
|
197
210
|
|
|
198
|
-
updateApibaraConfigFile(options);
|
|
211
|
+
await updateApibaraConfigFile(options);
|
|
199
212
|
|
|
200
213
|
consola.success(
|
|
201
214
|
`Updated ${cyan("apibara.config." + (language === "typescript" ? "ts" : "js"))}`,
|
|
202
215
|
);
|
|
203
216
|
|
|
204
|
-
updatePackageJson(options);
|
|
217
|
+
await updatePackageJson(options);
|
|
205
218
|
|
|
206
219
|
consola.success(`Updated ${cyan("package.json")}`);
|
|
207
220
|
|
package/src/create/constants.ts
CHANGED
|
@@ -66,17 +66,17 @@ export const storages: StorageDataType[] = [
|
|
|
66
66
|
|
|
67
67
|
export const packageVersions = {
|
|
68
68
|
// Required Dependencies
|
|
69
|
-
apibara: "
|
|
70
|
-
"@apibara/indexer": "
|
|
71
|
-
"@apibara/protocol": "
|
|
69
|
+
apibara: "next",
|
|
70
|
+
"@apibara/indexer": "next",
|
|
71
|
+
"@apibara/protocol": "next",
|
|
72
72
|
// Chain Dependencies
|
|
73
|
-
"@apibara/evm": "
|
|
74
|
-
"@apibara/beaconchain": "
|
|
75
|
-
"@apibara/starknet": "
|
|
73
|
+
"@apibara/evm": "next",
|
|
74
|
+
"@apibara/beaconchain": "next",
|
|
75
|
+
"@apibara/starknet": "next",
|
|
76
76
|
// Storage Dependencies
|
|
77
|
-
"@apibara/plugin-drizzle": "
|
|
78
|
-
"@apibara/plugin-mongo": "
|
|
79
|
-
"@apibara/plugin-sqlite": "
|
|
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",
|
package/src/create/init.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { Language } from "./types";
|
|
|
13
13
|
import {
|
|
14
14
|
cancelOperation,
|
|
15
15
|
emptyDir,
|
|
16
|
+
formatFile,
|
|
16
17
|
getLanguageFromAlias,
|
|
17
18
|
getPackageManager,
|
|
18
19
|
isEmpty,
|
|
@@ -121,30 +122,40 @@ export async function initializeProject({
|
|
|
121
122
|
consola.info(`Initializing project in ${argTargetDir}\n\n`);
|
|
122
123
|
|
|
123
124
|
// Generate package.json
|
|
125
|
+
const packageJsonPath = path.join(root, "package.json");
|
|
124
126
|
const packageJson = generatePackageJson(isTs);
|
|
125
127
|
fs.writeFileSync(
|
|
126
|
-
|
|
128
|
+
packageJsonPath,
|
|
127
129
|
JSON.stringify(packageJson, null, 2) + "\n",
|
|
128
130
|
);
|
|
131
|
+
await formatFile(packageJsonPath);
|
|
129
132
|
consola.success("Created ", cyan("package.json"));
|
|
130
133
|
|
|
131
134
|
// Generate tsconfig.json if TypeScript
|
|
132
135
|
if (isTs) {
|
|
136
|
+
const tsConfigPath = path.join(root, "tsconfig.json");
|
|
133
137
|
const tsConfig = generateTsConfig();
|
|
134
|
-
fs.writeFileSync(
|
|
135
|
-
|
|
136
|
-
JSON.stringify(tsConfig, null, 2) + "\n",
|
|
137
|
-
);
|
|
138
|
+
fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2) + "\n");
|
|
139
|
+
await formatFile(tsConfigPath);
|
|
138
140
|
consola.success("Created ", cyan("tsconfig.json"));
|
|
139
141
|
}
|
|
140
142
|
|
|
143
|
+
const apibaraConfigPath = path.join(root, `apibara.config.${configExt}`);
|
|
141
144
|
// Generate apibara.config
|
|
142
145
|
const apibaraConfig = generateApibaraConfig(isTs);
|
|
143
|
-
fs.writeFileSync(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
fs.writeFileSync(apibaraConfigPath, apibaraConfig);
|
|
147
|
+
await formatFile(apibaraConfigPath);
|
|
148
|
+
consola.success("Created ", cyan(`apibara.config.${configExt}`));
|
|
149
|
+
|
|
150
|
+
// Create "indexers" directory if not exists
|
|
151
|
+
const indexersDir = path.join(root, "indexers");
|
|
152
|
+
if (!fs.existsSync(indexersDir)) {
|
|
153
|
+
fs.mkdirSync(indexersDir, { recursive: true });
|
|
154
|
+
consola.success(`Created ${cyan("indexers")} directory`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
console.log("\n");
|
|
158
|
+
|
|
148
159
|
consola.ready(green("Project initialized successfully"));
|
|
149
160
|
|
|
150
161
|
console.log();
|
package/src/create/templates.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { type ObjectLiteralExpression, Project, SyntaxKind } from "ts-morph";
|
|
|
5
5
|
import { cyan, green, magenta, yellow } from "./colors";
|
|
6
6
|
import { packageVersions } from "./constants";
|
|
7
7
|
import type { IndexerOptions } from "./types";
|
|
8
|
-
import { checkFileExists, getDnaUrl } from "./utils";
|
|
8
|
+
import { checkFileExists, formatFile, getDnaUrl } from "./utils";
|
|
9
9
|
|
|
10
10
|
export function generatePackageJson(isTypeScript: boolean) {
|
|
11
11
|
return {
|
|
@@ -78,19 +78,19 @@ export function generateIndexer({
|
|
|
78
78
|
chain,
|
|
79
79
|
language,
|
|
80
80
|
}: IndexerOptions) {
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
? `import { EvmStream } from "@apibara/evm";`
|
|
84
|
-
: chain === "beaconchain"
|
|
85
|
-
? `import { BeaconChainStream } from "@apibara/beaconchain";`
|
|
86
|
-
: chain === "starknet"
|
|
87
|
-
? `import { StarknetStream } from "@apibara/starknet";`
|
|
88
|
-
: ""
|
|
89
|
-
}
|
|
90
|
-
import { defineIndexer } from "@apibara/indexer";
|
|
81
|
+
return `import { defineIndexer } from "@apibara/indexer";
|
|
82
|
+
import { useLogger } from "@apibara/indexer/plugins";
|
|
91
83
|
${storage === "postgres" ? `import { drizzleStorage } from "@apibara/plugin-drizzle";` : ""}
|
|
84
|
+
${
|
|
85
|
+
chain === "ethereum"
|
|
86
|
+
? `import { EvmStream } from "@apibara/evm";`
|
|
87
|
+
: chain === "beaconchain"
|
|
88
|
+
? `import { BeaconChainStream } from "@apibara/beaconchain";`
|
|
89
|
+
: chain === "starknet"
|
|
90
|
+
? `import { StarknetStream } from "@apibara/starknet";`
|
|
91
|
+
: ""
|
|
92
|
+
}
|
|
92
93
|
${language === "typescript" ? `import type { ApibaraRuntimeConfig } from "apibara/types";` : ""}
|
|
93
|
-
import { useLogger } from "@apibara/indexer/plugins";
|
|
94
94
|
${
|
|
95
95
|
storage === "postgres"
|
|
96
96
|
? `import { getDrizzlePgDatabase } from "../lib/db";`
|
|
@@ -169,17 +169,19 @@ export async function createIndexerFile(options: IndexerOptions) {
|
|
|
169
169
|
|
|
170
170
|
fs.mkdirSync(path.dirname(indexerFilePath), { recursive: true });
|
|
171
171
|
fs.writeFileSync(indexerFilePath, indexerContent);
|
|
172
|
+
|
|
173
|
+
await formatFile(indexerFilePath);
|
|
172
174
|
}
|
|
173
175
|
|
|
174
|
-
export function updatePackageJson({
|
|
176
|
+
export async function updatePackageJson({
|
|
175
177
|
cwd,
|
|
176
178
|
chain,
|
|
177
179
|
storage,
|
|
178
180
|
language,
|
|
179
181
|
}: IndexerOptions) {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
);
|
|
182
|
+
const packageJsonPath = path.join(cwd, "package.json");
|
|
183
|
+
|
|
184
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
183
185
|
|
|
184
186
|
if (chain === "ethereum") {
|
|
185
187
|
packageJson.dependencies["@apibara/evm"] = packageVersions["@apibara/evm"];
|
|
@@ -212,13 +214,12 @@ export function updatePackageJson({
|
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
216
|
|
|
215
|
-
fs.writeFileSync(
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
);
|
|
217
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
218
|
+
|
|
219
|
+
await formatFile(packageJsonPath);
|
|
219
220
|
}
|
|
220
221
|
|
|
221
|
-
export function updateApibaraConfigFile({
|
|
222
|
+
export async function updateApibaraConfigFile({
|
|
222
223
|
indexerId,
|
|
223
224
|
cwd,
|
|
224
225
|
chain,
|
|
@@ -279,11 +280,7 @@ export function updateApibaraConfigFile({
|
|
|
279
280
|
// Save the changes
|
|
280
281
|
sourceFile.saveSync();
|
|
281
282
|
|
|
282
|
-
|
|
283
|
-
tabSize: 2,
|
|
284
|
-
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: true,
|
|
285
|
-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
|
286
|
-
});
|
|
283
|
+
await formatFile(pathToConfig);
|
|
287
284
|
}
|
|
288
285
|
|
|
289
286
|
export async function createDrizzleStorageFiles(options: IndexerOptions) {
|
|
@@ -325,6 +322,8 @@ export default {
|
|
|
325
322
|
|
|
326
323
|
fs.writeFileSync(drizzleConfigPath, drizzleConfigContent);
|
|
327
324
|
|
|
325
|
+
await formatFile(drizzleConfigPath);
|
|
326
|
+
|
|
328
327
|
consola.success(`Created ${cyan(drizzleConfigFileName)}`);
|
|
329
328
|
}
|
|
330
329
|
|
|
@@ -365,6 +364,8 @@ export {};
|
|
|
365
364
|
fs.mkdirSync(path.dirname(schemaPath), { recursive: true });
|
|
366
365
|
fs.writeFileSync(schemaPath, schemaContent);
|
|
367
366
|
|
|
367
|
+
await formatFile(schemaPath);
|
|
368
|
+
|
|
368
369
|
consola.success(`Created ${cyan("lib/schema.ts")}`);
|
|
369
370
|
}
|
|
370
371
|
|
|
@@ -420,6 +421,8 @@ export function getDrizzlePgDatabase(connectionString${language === "typescript"
|
|
|
420
421
|
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
421
422
|
fs.writeFileSync(dbPath, dbContent);
|
|
422
423
|
|
|
424
|
+
await formatFile(dbPath);
|
|
425
|
+
|
|
423
426
|
consola.success(`Created ${cyan(`lib/${dbFileName}`)}`);
|
|
424
427
|
}
|
|
425
428
|
|
package/src/create/utils.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path, { basename } from "node:path";
|
|
3
|
+
import * as prettier from "prettier";
|
|
3
4
|
import prompts from "prompts";
|
|
4
5
|
import { blue, cyan, red, yellow } from "./colors";
|
|
5
6
|
import { dnaUrls, networks } from "./constants";
|
|
@@ -410,3 +411,12 @@ function pkgFromUserAgent(userAgent: string | undefined): PkgInfo | undefined {
|
|
|
410
411
|
version: pkgSpecArr[1],
|
|
411
412
|
};
|
|
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
|
+
}
|