@paimaexample/npm-midnight-node 0.6.0 → 0.7.0

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 +28 -0
  2. package/index.js +63 -3
  3. package/package.json +4 -2
package/binary.js CHANGED
@@ -108,7 +108,35 @@ async function binary() {
108
108
  await unzipBinary();
109
109
  }
110
110
 
111
+ async function cleanBinaries() {
112
+ const binaryDir = path.join(__dirname, "midnight-node");
113
+ const zipPath = path.join(__dirname, FILE_NAME);
114
+
115
+ let deletedFiles = [];
116
+
117
+ if (fs.existsSync(binaryDir)) {
118
+ try {
119
+ fs.rmSync(binaryDir, { recursive: true, force: true });
120
+ deletedFiles.push(binaryDir);
121
+ } catch (error) {
122
+ console.error(`Error removing directory ${binaryDir}:`, error.message);
123
+ }
124
+ }
125
+
126
+ if (fs.existsSync(zipPath)) {
127
+ try {
128
+ fs.unlinkSync(zipPath);
129
+ deletedFiles.push(zipPath);
130
+ } catch (error) {
131
+ console.error(`Error removing file ${zipPath}:`, error.message);
132
+ }
133
+ }
134
+
135
+ return deletedFiles;
136
+ }
137
+
111
138
  module.exports = {
112
139
  binary,
113
140
  getPlatform,
141
+ cleanBinaries,
114
142
  };
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { binary } = require("./binary");
1
+ const { binary, cleanBinaries } = require("./binary");
2
2
  const { runMidnightNode } = require("./run_midnight_node");
3
3
  const fs = require("fs");
4
4
  const path = require("path");
@@ -11,11 +11,71 @@ function checkIfBinaryExists() {
11
11
  );
12
12
  }
13
13
 
14
+ function showUsage() {
15
+ console.log(`
16
+ Usage: node index.js [options] [args...]
17
+
18
+ Options:
19
+ --clean-binaries Delete downloaded binaries and download them again
20
+ --only-clean Only delete downloaded binaries without downloading them again
21
+ --help, -h Show this help message
22
+ `);
23
+ }
24
+
25
+ function parseFlags(args) {
26
+ const flags = {
27
+ cleanBinaries: false,
28
+ onlyClean: false,
29
+ showHelp: false,
30
+ remainingArgs: [],
31
+ };
32
+
33
+ for (let i = 0; i < args.length; i++) {
34
+ if (args[i] === "--clean-binaries") {
35
+ flags.cleanBinaries = true;
36
+ } else if (args[i] === "--only-clean") {
37
+ flags.onlyClean = true;
38
+ } else if (args[i] === "--help" || args[i] === "-h") {
39
+ flags.showHelp = true;
40
+ } else {
41
+ flags.remainingArgs.push(args[i]);
42
+ }
43
+ }
44
+
45
+ return flags;
46
+ }
47
+
14
48
  async function main(args) {
15
- if (!checkIfBinaryExists()) {
49
+ const flags = parseFlags(args);
50
+
51
+ if (flags.showHelp) {
52
+ showUsage();
53
+ process.exit(0);
54
+ }
55
+
56
+ if (flags.onlyClean) {
57
+ console.log("Cleaning downloaded binaries...");
58
+ const deletedFiles = await cleanBinaries();
59
+ if (deletedFiles.length > 0) {
60
+ console.log("Deleted:", deletedFiles.join(", "));
61
+ } else {
62
+ console.log("No downloaded binaries found to delete.");
63
+ }
64
+ process.exit(0);
65
+ }
66
+
67
+ if (flags.cleanBinaries || !checkIfBinaryExists()) {
68
+ if (flags.cleanBinaries) {
69
+ console.log("Cleaning downloaded binaries...");
70
+ await cleanBinaries();
71
+ }
16
72
  await binary();
17
73
  }
18
- runMidnightNode(process.env, args);
74
+ runMidnightNode(process.env, flags.remainingArgs);
19
75
  }
20
76
 
77
+ module.exports = {
78
+ cleanBinaries,
79
+ };
80
+
21
81
  main(process.argv.slice(2));
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@paimaexample/npm-midnight-node",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Downloads and starts the binary for 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-node": "index.js"