@paimaexample/npm-midnight-indexer 0.3.15 → 0.3.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paimaexample/npm-midnight-indexer",
3
- "version": "0.3.15",
3
+ "version": "0.3.16",
4
4
  "description": "Downloads and runs the Midnight Indexer. It needs a running Midnight Node",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "dependencies": {
20
20
  "axios": "^1.10.0",
21
21
  "axios-proxy-builder": "^0.1.2",
22
- "extract-zip": "^2.0.1"
22
+ "extract-zip": "^2.0.1",
23
+ "js-yaml": "^4.1.0"
23
24
  }
24
25
  }
@@ -1,5 +1,108 @@
1
- const { spawn } = require('child_process');
2
- const path = require('path');
1
+ const { spawn } = require("child_process");
2
+ const path = require("path");
3
+ const fs = require("fs");
4
+ const yaml = require("js-yaml");
5
+
6
+ /**
7
+ * Resolves the SQLite database path using the midnight-indexer configuration rules
8
+ * @param {Object} env - Environment variables
9
+ * @param {string} workingDir - The working directory where the indexer runs
10
+ * @returns {string|null} The resolved SQLite database path, or null if not found
11
+ */
12
+ function resolveSqlitePath(env, workingDir) {
13
+ // First check the APP__INFRA__STORAGE__CNN_URL environment variable
14
+ const envCnnUrl = env.APP__INFRA__STORAGE__CNN_URL;
15
+ if (envCnnUrl) {
16
+ console.log(`Found SQLite path from environment variable: ${envCnnUrl}`);
17
+ return envCnnUrl;
18
+ }
19
+
20
+ // Determine config file path using the configuration resolution rules
21
+ let configPath;
22
+ if (env.CONFIG_FILE) {
23
+ configPath = path.isAbsolute(env.CONFIG_FILE)
24
+ ? env.CONFIG_FILE
25
+ : path.resolve(workingDir, env.CONFIG_FILE);
26
+ } else {
27
+ // Fall back to config.yaml in the current working directory
28
+ configPath = path.join(workingDir, "config.yaml");
29
+ }
30
+
31
+ console.log(`Looking for config file at: ${configPath}`);
32
+
33
+ // Check if config file exists
34
+ if (!fs.existsSync(configPath)) {
35
+ console.warn(`Config file not found at: ${configPath}`);
36
+ return null;
37
+ }
38
+
39
+ try {
40
+ // Parse the YAML config file
41
+ const configContent = fs.readFileSync(configPath, "utf8");
42
+ const config = yaml.load(configContent);
43
+
44
+ // Extract the cnn_url from infra.storage
45
+ const cnnUrl = config?.infra?.storage?.cnn_url;
46
+ if (!cnnUrl) {
47
+ console.warn("No cnn_url found in config file under infra.storage");
48
+ return null;
49
+ }
50
+
51
+ console.log(`Found SQLite path from config file: ${cnnUrl}`);
52
+
53
+ // If the path is relative, make it relative to the config file location
54
+ if (!path.isAbsolute(cnnUrl)) {
55
+ const configDir = path.dirname(configPath);
56
+ const resolvedPath = path.resolve(configDir, cnnUrl);
57
+ console.log(`Resolved relative path to: ${resolvedPath}`);
58
+ return resolvedPath;
59
+ }
60
+
61
+ return cnnUrl;
62
+ } catch (error) {
63
+ console.error(`Failed to parse config file: ${error.message}`);
64
+ return null;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Handles the --clean flag by deleting the SQLite database file
70
+ * @param {Object} env - Environment variables
71
+ * @param {string} workingDir - The working directory where the indexer runs
72
+ */
73
+ function handleCleanFlag(env, workingDir) {
74
+ console.log("Processing --clean flag...");
75
+
76
+ const sqlitePath = resolveSqlitePath(env, workingDir);
77
+
78
+ if (!sqlitePath) {
79
+ console.warn("Could not resolve SQLite database path. Skipping cleanup.");
80
+ return;
81
+ }
82
+
83
+ // Handle sqlite:// URLs and extract the file path
84
+ let filePath = sqlitePath;
85
+ if (sqlitePath.startsWith("sqlite://")) {
86
+ filePath = sqlitePath.replace("sqlite://", "");
87
+ } else if (sqlitePath.startsWith("sqlite:///")) {
88
+ filePath = sqlitePath.replace("sqlite:///", "/");
89
+ }
90
+
91
+ console.log(`Attempting to clean SQLite database at: ${filePath}`);
92
+
93
+ if (fs.existsSync(filePath)) {
94
+ try {
95
+ fs.unlinkSync(filePath);
96
+ console.log(`Successfully deleted SQLite database: ${filePath}`);
97
+ } catch (error) {
98
+ console.error(`Failed to delete SQLite database: ${error.message}`);
99
+ }
100
+ } else {
101
+ console.log(
102
+ `SQLite database does not exist (will be created fresh): ${filePath}`,
103
+ );
104
+ }
105
+ }
3
106
 
4
107
  /**
5
108
  * Executes the midnight-indexer binary as a child process
@@ -8,33 +111,48 @@ const path = require('path');
8
111
  * @returns {ChildProcess} The spawned child process
9
112
  */
10
113
  function runMidnightIndexer(env = process.env, args = []) {
11
- const binaryPath = path.join(__dirname, 'indexer-standalone', 'indexer-standalone');
12
-
13
- console.log(`Starting midnight-indexer binary at: ${binaryPath}`);
14
-
15
- const childProcess = spawn(binaryPath, args, {
16
- env: env,
17
- stdio: 'inherit', // Inherit stdin, stdout, stderr from parent process
18
- cwd: path.join(__dirname, 'indexer-standalone') // Run from inside the midnight-indexer directory
19
- });
20
-
21
- childProcess.on('spawn', () => {
22
- console.log(`midnight-indexer process spawned with PID: ${childProcess.pid}`);
23
- });
24
-
25
- childProcess.on('error', (error) => {
26
- console.error('Failed to start midnight-indexer:', error);
27
- });
28
-
29
- childProcess.on('exit', (code, signal) => {
30
- if (code !== null) {
31
- console.log(`midnight-indexer process exited with code: ${code}`);
32
- } else {
33
- console.log(`midnight-indexer process terminated by signal: ${signal}`);
34
- }
35
- });
36
-
37
- return childProcess;
114
+ const binaryPath = path.join(
115
+ __dirname,
116
+ "indexer-standalone",
117
+ "indexer-standalone",
118
+ );
119
+ const workingDir = path.join(__dirname, "indexer-standalone");
120
+
121
+ // Check for --clean flag and handle it
122
+ const cleanFlagIndex = args.indexOf("--clean");
123
+ if (cleanFlagIndex !== -1) {
124
+ handleCleanFlag(env, workingDir);
125
+ // Remove the --clean flag from args since the binary doesn't expect it
126
+ args.splice(cleanFlagIndex, 1);
127
+ }
128
+
129
+ console.log(`Starting midnight-indexer binary at: ${binaryPath}`);
130
+
131
+ const childProcess = spawn(binaryPath, args, {
132
+ env: env,
133
+ stdio: "inherit", // Inherit stdin, stdout, stderr from parent process
134
+ cwd: workingDir, // Run from inside the indexer-standalone directory
135
+ });
136
+
137
+ childProcess.on("spawn", () => {
138
+ console.log(
139
+ `midnight-indexer process spawned with PID: ${childProcess.pid}`,
140
+ );
141
+ });
142
+
143
+ childProcess.on("error", (error) => {
144
+ console.error("Failed to start midnight-indexer:", error);
145
+ });
146
+
147
+ childProcess.on("exit", (code, signal) => {
148
+ if (code !== null) {
149
+ console.log(`midnight-indexer process exited with code: ${code}`);
150
+ } else {
151
+ console.log(`midnight-indexer process terminated by signal: ${signal}`);
152
+ }
153
+ });
154
+
155
+ return childProcess;
38
156
  }
39
157
 
40
158
  module.exports = { runMidnightIndexer };