@officebeats/matrix-iptv-cli 3.0.8 → 3.1.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 (2) hide show
  1. package/bin/cli.js +107 -17
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -4,29 +4,119 @@ const { spawn } = require("child_process");
4
4
  const path = require("path");
5
5
  const os = require("os");
6
6
  const fs = require("fs");
7
+ const https = require("https");
7
8
 
8
9
  const binaryName =
9
10
  os.platform() === "win32" ? "matrix-iptv.exe" : "matrix-iptv";
10
11
  const binaryPath = path.join(__dirname, binaryName);
11
12
 
12
- if (!fs.existsSync(binaryPath)) {
13
- console.error("\n❌ Matrix IPTV binary not found.");
14
- console.log(
15
- "Please try reinstalling the package: npm install -g @officebeats/matrix-iptv-cli\n"
16
- );
17
- process.exit(1);
13
+ const platformMap = {
14
+ win32: "windows.exe",
15
+ linux: "linux",
16
+ darwin: "macos",
17
+ };
18
+
19
+ function download(url, dest) {
20
+ return new Promise((resolve, reject) => {
21
+ const file = fs.createWriteStream(dest);
22
+ https
23
+ .get(url, (response) => {
24
+ if (response.statusCode === 302 || response.statusCode === 301) {
25
+ download(response.headers.location, dest).then(resolve).catch(reject);
26
+ return;
27
+ }
28
+ if (response.statusCode !== 200) {
29
+ reject(new Error(`Failed to download: ${response.statusCode}`));
30
+ return;
31
+ }
32
+ response.pipe(file);
33
+ file.on("finish", () => {
34
+ file.close();
35
+ resolve();
36
+ });
37
+ })
38
+ .on("error", (err) => {
39
+ fs.unlink(dest, () => {});
40
+ reject(err);
41
+ });
42
+ });
43
+ }
44
+
45
+ async function performUpdate() {
46
+ const platform = platformMap[os.platform()];
47
+ if (!platform) {
48
+ throw new Error(`Unsupported platform for auto-update: ${os.platform()}`);
49
+ }
50
+ const releaseUrl = `https://github.com/officebeats/matrix-iptv/releases/latest/download/matrix-iptv-${platform}`;
51
+
52
+ console.log(`\n[*] Initiating Phase 4: System Update...`);
53
+ console.log(`[*] Downloading: ${releaseUrl}`);
54
+
55
+ const tempPath = binaryPath + ".tmp";
56
+
57
+ try {
58
+ await download(releaseUrl, tempPath);
59
+
60
+ if (os.platform() !== "win32") {
61
+ fs.chmodSync(tempPath, "755");
62
+ }
63
+
64
+ // Replace old binary
65
+ // On Windows, sometimes the OS still has a lock for a split second
66
+ let attempts = 0;
67
+ while (attempts < 5) {
68
+ try {
69
+ if (fs.existsSync(binaryPath)) {
70
+ fs.unlinkSync(binaryPath);
71
+ }
72
+ fs.renameSync(tempPath, binaryPath);
73
+ break;
74
+ } catch (e) {
75
+ attempts++;
76
+ if (attempts === 5) throw e;
77
+ await new Promise((r) => setTimeout(r, 500));
78
+ }
79
+ }
80
+
81
+ console.log(`[+] Update complete. Rebooting system...\n`);
82
+ } catch (err) {
83
+ if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath);
84
+ throw err;
85
+ }
18
86
  }
19
87
 
20
- const child = spawn(binaryPath, process.argv.slice(2), {
21
- stdio: "inherit",
22
- windowsHide: false,
23
- });
88
+ function launchApp() {
89
+ if (!fs.existsSync(binaryPath)) {
90
+ console.error("\n❌ Matrix IPTV binary not found.");
91
+ console.log(
92
+ "Please try reinstalling the package: npm install -g @officebeats/matrix-iptv-cli\n"
93
+ );
94
+ process.exit(1);
95
+ }
24
96
 
25
- child.on("error", (err) => {
26
- console.error("Failed to start Matrix IPTV:", err);
27
- process.exit(1);
28
- });
97
+ const child = spawn(binaryPath, process.argv.slice(2), {
98
+ stdio: "inherit",
99
+ windowsHide: false,
100
+ });
101
+
102
+ child.on("error", (err) => {
103
+ console.error("Failed to start Matrix IPTV:", err);
104
+ process.exit(1);
105
+ });
106
+
107
+ child.on("exit", async (code) => {
108
+ if (code === 42) {
109
+ try {
110
+ await performUpdate();
111
+ launchApp(); // Relaunch
112
+ } catch (err) {
113
+ console.error(`\n❌ Update failed: ${err.message}`);
114
+ process.exit(1);
115
+ }
116
+ } else {
117
+ process.exit(code || 0);
118
+ }
119
+ });
120
+ }
29
121
 
30
- child.on("exit", (code) => {
31
- process.exit(code || 0);
32
- });
122
+ launchApp();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@officebeats/matrix-iptv-cli",
3
- "version": "3.0.8",
3
+ "version": "3.1.0",
4
4
  "description": "The premium Terminal IPTV Decoder",
5
5
  "main": "index.js",
6
6
  "bin": {