@paimaexample/npm-midnight-indexer 0.6.1 → 0.7.1

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.
Files changed (3) hide show
  1. package/binary.js +61 -0
  2. package/index.js +52 -13
  3. package/package.json +4 -2
package/binary.js CHANGED
@@ -150,7 +150,68 @@ async function binary() {
150
150
  await unzipBinary();
151
151
  }
152
152
 
153
+ async function cleanBinaries() {
154
+ const binaryDir = path.join(__dirname, "indexer-standalone");
155
+ const binaryPath = path.join(binaryDir, FINAL_BINARY_NAME);
156
+ const zipPath = path.join(__dirname, "indexer-standalone.zip");
157
+
158
+ let deletedFiles = [];
159
+
160
+ // Delete the binary executable if it exists
161
+ if (fs.existsSync(binaryPath)) {
162
+ try {
163
+ fs.unlinkSync(binaryPath);
164
+ deletedFiles.push(binaryPath);
165
+ } catch (error) {
166
+ console.error(`Error removing binary ${binaryPath}:`, error.message);
167
+ }
168
+ }
169
+
170
+ // Delete the zip file if it exists
171
+ if (fs.existsSync(zipPath)) {
172
+ try {
173
+ fs.unlinkSync(zipPath);
174
+ deletedFiles.push(zipPath);
175
+ } catch (error) {
176
+ console.error(`Error removing file ${zipPath}:`, error.message);
177
+ }
178
+ }
179
+
180
+ // Delete any other binary-related files from the directory, preserving config files
181
+ if (fs.existsSync(binaryDir)) {
182
+ const files = fs.readdirSync(binaryDir);
183
+ for (const file of files) {
184
+ const filePath = path.join(binaryDir, file);
185
+ const stat = fs.statSync(filePath);
186
+
187
+ // Preserve config files and readme
188
+ const isConfigFile =
189
+ file === "config.yaml" ||
190
+ file === "config.yml" ||
191
+ file.endsWith(".yaml") ||
192
+ file.endsWith(".yml") ||
193
+ file.endsWith(".toml") ||
194
+ file.endsWith(".json") ||
195
+ file.toLowerCase().startsWith("readme") ||
196
+ file.toLowerCase().startsWith("license") ||
197
+ file.toLowerCase().startsWith("changelog");
198
+
199
+ if (!isConfigFile && stat.isFile() && fs.existsSync(filePath)) {
200
+ try {
201
+ fs.unlinkSync(filePath);
202
+ deletedFiles.push(filePath);
203
+ } catch (error) {
204
+ console.error(`Error removing file ${filePath}:`, error.message);
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ return deletedFiles;
211
+ }
212
+
153
213
  module.exports = {
154
214
  binary,
155
215
  getPlatform,
216
+ cleanBinaries,
156
217
  };
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { binary, getPlatform } = require("./binary");
1
+ const { binary, getPlatform, cleanBinaries } = require("./binary");
2
2
  const { runMidnightIndexer } = require("./run_midnight_indexer");
3
3
  const { checkIfDockerExists, pullDockerImage, runDockerContainer } = require(
4
4
  "./docker",
@@ -56,9 +56,12 @@ function showUsage() {
56
56
  Usage: node index.js [options] [args...]
57
57
 
58
58
  Options:
59
- --docker Force use of Docker container
60
- --binary Force use of binary execution (supports Linux and macOS arm64)
61
- --help Show this help message
59
+ --docker Force use of Docker container
60
+ --binary Force use of binary execution (supports Linux and macOS arm64)
61
+ --clean-binaries Delete downloaded binaries and download them again
62
+ --only-clean Only delete downloaded binaries without downloading them again
63
+ --clean Clean the SQLite database (existing flag)
64
+ --help Show this help message
62
65
 
63
66
  Environment Variables:
64
67
  APP__INFRA__SECRET Required: Secret key for the application
@@ -74,10 +77,12 @@ Environment Variables:
74
77
  Note: macOS Intel users will automatically use Docker. macOS arm64 supports both binary and Docker execution.
75
78
 
76
79
  Examples:
77
- APP__INFRA__SECRET=mysecret node index.js --docker # Use Docker
78
- APP__INFRA__SECRET=mysecret node index.js --binary # Use binary (if supported)
79
- APP__INFRA__SECRET=mysecret node index.js # Interactive mode (or auto-Docker on unsupported platforms)
80
- node index.js --help # Show this help
80
+ APP__INFRA__SECRET=mysecret node index.js --docker # Use Docker
81
+ APP__INFRA__SECRET=mysecret node index.js --binary # Use binary (if supported)
82
+ APP__INFRA__SECRET=mysecret node index.js --clean-binaries # Delete and redownload binaries
83
+ node index.js --only-clean # Only delete downloaded binaries
84
+ APP__INFRA__SECRET=mysecret node index.js --clean # Clean SQLite database
85
+ node index.js --help # Show this help
81
86
  `);
82
87
  }
83
88
 
@@ -90,6 +95,8 @@ function parseFlags(args) {
90
95
  const flags = {
91
96
  useDocker: false,
92
97
  useBinary: false,
98
+ cleanBinaries: false,
99
+ onlyClean: false,
93
100
  showHelp: false,
94
101
  remainingArgs: [],
95
102
  };
@@ -99,6 +106,10 @@ function parseFlags(args) {
99
106
  flags.useDocker = true;
100
107
  } else if (args[i] === "--binary") {
101
108
  flags.useBinary = true;
109
+ } else if (args[i] === "--clean-binaries") {
110
+ flags.cleanBinaries = true;
111
+ } else if (args[i] === "--only-clean") {
112
+ flags.onlyClean = true;
102
113
  } else if (args[i] === "--help" || args[i] === "-h") {
103
114
  flags.showHelp = true;
104
115
  } else {
@@ -153,7 +164,7 @@ function setBinaryDefaults(env) {
153
164
  return binaryEnv;
154
165
  }
155
166
 
156
- async function runWithBinary(env, args) {
167
+ async function runWithBinary(env, args, forceClean = false) {
157
168
  console.log("Using binary to run midnight indexer...");
158
169
 
159
170
  // Check for required environment variable
@@ -163,8 +174,12 @@ async function runWithBinary(env, args) {
163
174
  process.exit(1);
164
175
  }
165
176
 
166
- if (!checkIfBinaryExists()) {
167
- console.log("Binary not found, downloading...");
177
+ if (forceClean || !checkIfBinaryExists()) {
178
+ if (forceClean) {
179
+ console.log("Cleaning downloaded binaries...");
180
+ await cleanBinaries();
181
+ }
182
+ console.log("Downloading binary...");
168
183
  await binary();
169
184
  }
170
185
 
@@ -184,6 +199,18 @@ async function main(args) {
184
199
  process.exit(0);
185
200
  }
186
201
 
202
+ // Handle --only-clean flag
203
+ if (flags.onlyClean) {
204
+ console.log("Cleaning downloaded binaries...");
205
+ const deletedFiles = await cleanBinaries();
206
+ if (deletedFiles.length > 0) {
207
+ console.log("Deleted:", deletedFiles.join(", "));
208
+ } else {
209
+ console.log("No downloaded binaries found to delete.");
210
+ }
211
+ process.exit(0);
212
+ }
213
+
187
214
  // If both flags are provided, show error
188
215
  if (flags.useDocker && flags.useBinary) {
189
216
  console.error(
@@ -192,6 +219,14 @@ async function main(args) {
192
219
  process.exit(1);
193
220
  }
194
221
 
222
+ // Validate clean flag usage
223
+ if (flags.cleanBinaries && flags.useDocker) {
224
+ console.error(
225
+ "Error: --clean-binaries flag cannot be used with --docker flag",
226
+ );
227
+ process.exit(1);
228
+ }
229
+
195
230
  // If --docker flag is explicitly provided
196
231
  if (flags.useDocker) {
197
232
  const dockerAvailable = await checkIfDockerExists();
@@ -229,7 +264,7 @@ async function main(args) {
229
264
  );
230
265
  process.exit(1);
231
266
  }
232
- return runWithBinary(env, flags.remainingArgs);
267
+ return runWithBinary(env, flags.remainingArgs, flags.cleanBinaries);
233
268
  }
234
269
 
235
270
  // No explicit flag provided - determine best execution method
@@ -285,7 +320,7 @@ async function main(args) {
285
320
  }
286
321
 
287
322
  // Default to binary execution (only on supported platforms)
288
- return runWithBinary(env, flags.remainingArgs);
323
+ return runWithBinary(env, flags.remainingArgs, flags.clean);
289
324
  }
290
325
 
291
326
  // Handle unhandled promise rejections
@@ -300,4 +335,8 @@ process.on("SIGINT", () => {
300
335
  process.exit(0);
301
336
  });
302
337
 
338
+ module.exports = {
339
+ cleanBinaries,
340
+ };
341
+
303
342
  main(process.argv.slice(2));
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@paimaexample/npm-midnight-indexer",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "Downloads and runs the Midnight Indexer. It needs a running Midnight Node",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "clean": "node index.js --clean-binaries",
9
+ "clean-only": "node index.js --only-clean"
8
10
  },
9
11
  "bin": {
10
12
  "npm-midnight-indexer": "index.js"