Skip to content

Commit 1488d57

Browse files
committed
Fixed Streamio service
1 parent 9ac05a7 commit 1488d57

File tree

4 files changed

+176
-51
lines changed

4 files changed

+176
-51
lines changed

copyComponents.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,35 @@ function copyFiles(srcDir, destDir) {
2424
});
2525
}
2626

27-
// Copy the 'version' file from the root directory
28-
const versionFileSrc = path.join(__dirname, 'version');
27+
// Generate version file from package.json (or copy existing if it exists and matches)
28+
const packageJsonPath = path.join(__dirname, 'package.json');
2929
const versionFileDest = path.join(__dirname, 'dist', 'version');
3030

31-
if (fs.existsSync(versionFileSrc)) {
32-
fs.copyFileSync(versionFileSrc, versionFileDest);
33-
console.log(`Copied: ${versionFileSrc} to ${versionFileDest}`);
31+
if (fs.existsSync(packageJsonPath)) {
32+
try {
33+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
34+
if (packageJson.version) {
35+
// Write version to dist/version
36+
fs.writeFileSync(versionFileDest, packageJson.version, 'utf-8');
37+
console.log(`Generated version file from package.json: ${packageJson.version}`);
38+
39+
// Also update root version file if it exists (or create it)
40+
const rootVersionFile = path.join(__dirname, 'version');
41+
fs.writeFileSync(rootVersionFile, packageJson.version, 'utf-8');
42+
}
43+
} catch (error) {
44+
console.error(`Failed to read version from package.json: ${error.message}`);
45+
// Fallback: copy existing version file if it exists
46+
const versionFileSrc = path.join(__dirname, 'version');
47+
if (fs.existsSync(versionFileSrc)) {
48+
fs.copyFileSync(versionFileSrc, versionFileDest);
49+
console.log(`Copied existing version file: ${versionFileSrc} to ${versionFileDest}`);
50+
} else {
51+
console.log('No version file found and failed to read from package.json.');
52+
}
53+
}
3454
} else {
35-
console.log('No version file found in the root directory.');
55+
console.log('No package.json found in the root directory.');
3656
}
3757

3858
const srcDir = 'src/components';

src/core/Updater.ts

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { join } from "path";
55
import { getUpdateModalTemplate } from "../components/update-modal/updateModal";
66
import { URLS } from "../constants";
77

8+
// Try to import app, but handle if we're in renderer process
9+
let app: typeof import("electron").app | undefined;
10+
try {
11+
app = require("electron").app;
12+
} catch {
13+
// app is not available in renderer process
14+
}
15+
816
class Updater {
917
private static logger = getLogger("Updater");
1018
private static versionCache: string | null = null;
@@ -83,16 +91,72 @@ class Updater {
8391
return this.versionCache;
8492
}
8593

86-
try {
87-
this.versionCache = readFileSync(
88-
join(__dirname, "../", "../", "version"),
89-
"utf-8"
90-
).trim();
91-
return this.versionCache;
92-
} catch (error) {
93-
this.logger.error(`Failed to read version file: ${(error as Error).message}`);
94-
return "0.0.0";
94+
const isPackaged = app ? app.isPackaged : false;
95+
96+
// Try multiple paths to find package.json or version file
97+
const pathsToTry: string[] = [];
98+
99+
if (isPackaged && app) {
100+
// In packaged app, try different locations
101+
if (process.resourcesPath) {
102+
pathsToTry.push(join(process.resourcesPath, "app.asar", "package.json"));
103+
pathsToTry.push(join(process.resourcesPath, "package.json"));
104+
}
105+
if (app.getAppPath) {
106+
pathsToTry.push(join(app.getAppPath(), "package.json"));
107+
}
108+
} else {
109+
// In development, it's relative to __dirname
110+
pathsToTry.push(join(__dirname, "../", "../", "package.json"));
111+
}
112+
113+
// Try to read from package.json first
114+
for (const packageJsonPath of pathsToTry) {
115+
try {
116+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
117+
if (packageJson.version) {
118+
this.versionCache = packageJson.version;
119+
this.logger.info(`Version read from package.json: v${this.versionCache}`);
120+
return this.versionCache;
121+
}
122+
} catch (error) {
123+
// Try next path
124+
continue;
125+
}
95126
}
127+
128+
// Fallback: try to read from version file
129+
const versionPathsToTry: string[] = [];
130+
131+
if (isPackaged && app) {
132+
if (process.resourcesPath) {
133+
versionPathsToTry.push(join(process.resourcesPath, "app.asar", "dist", "version"));
134+
versionPathsToTry.push(join(process.resourcesPath, "version"));
135+
}
136+
if (app.getAppPath) {
137+
versionPathsToTry.push(join(app.getAppPath(), "dist", "version"));
138+
versionPathsToTry.push(join(app.getAppPath(), "version"));
139+
}
140+
} else {
141+
versionPathsToTry.push(join(__dirname, "../", "version"));
142+
versionPathsToTry.push(join(__dirname, "../", "../", "version"));
143+
}
144+
145+
for (const versionFilePath of versionPathsToTry) {
146+
try {
147+
this.versionCache = readFileSync(versionFilePath, "utf-8").trim();
148+
this.logger.info(`Version read from version file: v${this.versionCache}`);
149+
return this.versionCache;
150+
} catch (error) {
151+
// Try next path
152+
continue;
153+
}
154+
}
155+
156+
// Last resort: return 0.0.0
157+
this.logger.error("Failed to read version from any location. Using default: 0.0.0");
158+
this.versionCache = "0.0.0";
159+
return this.versionCache;
96160
}
97161

98162
/**

src/main.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ async function createWindow() {
266266
async function useStremioService() {
267267
if(await StremioService.isServiceInstalled()) {
268268
logger.info("Found installation of Stremio Service.");
269-
await StremioService.start();
269+
try {
270+
await StremioService.start();
271+
} catch (err) {
272+
logger.error(`Failed to start Stremio Service: ${(err as Error).message}`);
273+
}
270274
} else {
271275
const result = await Helpers.showAlert(
272276
"warning",
@@ -275,7 +279,23 @@ async function useStremioService() {
275279
["YES", "NO"]
276280
);
277281
if (result === 0) {
278-
await StremioService.downloadAndInstallService();
282+
logger.info("User chose to download Stremio Service. Starting download...");
283+
const success = await StremioService.downloadAndInstallService();
284+
if (success) {
285+
await Helpers.showAlert(
286+
"info",
287+
"Installation Successful",
288+
"Stremio Service has been installed and started successfully. Streaming features are now available.",
289+
["OK"]
290+
);
291+
} else {
292+
await Helpers.showAlert(
293+
"error",
294+
"Installation Failed",
295+
"Failed to install Stremio Service. Please try again or install it manually from https://github.com/Stremio/stremio-service/releases",
296+
["OK"]
297+
);
298+
}
279299
} else {
280300
logger.info("User declined to download Stremio Service.");
281301
}
@@ -327,6 +347,8 @@ app.on("ready", async () => {
327347
logger.error(`Failed to auto-start Stremio Service: ${(err as Error).message}`);
328348
}
329349
} else if(existsSync(useStremioServiceFlagPath)) {
350+
// User previously chose Stremio Service but it's not installed - show popup again
351+
logger.info("Stremio Service was previously selected but is not installed. Showing installation prompt...");
330352
await useStremioService();
331353
} else if(existsSync(useServerJSFlagPath)) {
332354
await useServerJS();
@@ -342,10 +364,12 @@ app.on("ready", async () => {
342364
await StremioService.start();
343365
} catch (err) {
344366
logger.error(`Failed to auto-start Stremio Service: ${(err as Error).message}`);
345-
await useServerJS();
367+
// Service exists but failed to start - show popup to reinstall
368+
await useStremioService();
346369
}
347370
} else {
348-
await useServerJS();
371+
// Service not installed - show popup to install
372+
await useStremioService();
349373
}
350374
}
351375
} else logger.info("Stremio Service is already running.");

src/utils/StremioService.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,58 @@ class StremioService {
6161
});
6262
}
6363

64-
public static async downloadAndInstallService() {
64+
public static async downloadAndInstallService(): Promise<boolean> {
6565
const platform = process.platform;
6666

67-
const release = await this.fetchLatestRelease();
68-
if (!release) {
69-
this.logger.error("Failed to fetch latest release info");
70-
return;
71-
}
72-
73-
const assetUrl = this.getAssetUrlForPlatform(release, platform);
74-
if (!assetUrl) {
75-
this.logger.error("No suitable asset found for platform: " + platform);
76-
return;
77-
}
78-
79-
const tempDir = app.getPath("temp");
80-
const fileName = basename(assetUrl);
81-
const destPath = join(tempDir, fileName);
82-
83-
this.logger.info(`Downloading latest Stremio Service (${release.tag_name}) in ${destPath}`);
84-
await this.downloadFile(assetUrl, destPath);
85-
this.logger.info("Download complete. Installing...");
67+
try {
68+
const release = await this.fetchLatestRelease();
69+
if (!release) {
70+
this.logger.error("Failed to fetch latest release info");
71+
return false;
72+
}
8673

87-
switch (platform) {
88-
case "win32":
89-
await this.installWindows(destPath);
90-
break;
91-
case "darwin":
92-
await this.installMac(destPath);
93-
break;
94-
case "linux":
95-
await this.installLinux(destPath);
96-
break;
97-
default:
98-
this.logger.warn("No install routine defined for: " + platform);
74+
const assetUrl = this.getAssetUrlForPlatform(release, platform);
75+
if (!assetUrl) {
76+
this.logger.error("No suitable asset found for platform: " + platform);
77+
return false;
78+
}
79+
80+
const tempDir = app.getPath("temp");
81+
const fileName = basename(assetUrl);
82+
const destPath = join(tempDir, fileName);
83+
84+
this.logger.info(`Downloading latest Stremio Service (${release.tag_name}) in ${destPath}`);
85+
await this.downloadFile(assetUrl, destPath);
86+
this.logger.info("Download complete. Installing...");
87+
88+
switch (platform) {
89+
case "win32":
90+
await this.installWindows(destPath);
91+
break;
92+
case "darwin":
93+
await this.installMac(destPath);
94+
break;
95+
case "linux":
96+
await this.installLinux(destPath);
97+
break;
98+
default:
99+
this.logger.warn("No install routine defined for: " + platform);
100+
return false;
101+
}
102+
103+
// Verify installation was successful
104+
const installed = await this.isServiceInstalled();
105+
if (installed) {
106+
this.logger.info("Stremio Service installed successfully. Starting service...");
107+
await this.start();
108+
return true;
109+
} else {
110+
this.logger.warn("Installation completed but service not detected as installed.");
111+
return false;
112+
}
113+
} catch (error) {
114+
this.logger.error(`Failed to download and install Stremio Service: ${(error as Error).message}`);
115+
return false;
99116
}
100117
}
101118

0 commit comments

Comments
 (0)