Skip to content

Commit 9863c3b

Browse files
committed
fix(desktop): read AppImage paths through the host environment
1 parent bc91c91 commit 9863c3b

2 files changed

Lines changed: 56 additions & 49 deletions

File tree

‎apps/desktop/src/electron/ElectronUpdater.launchPaths.test.ts‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import * as NodeOS from "node:os";
66
import * as NodePath from "node:path";
77

88
import { assert, describe, it } from "@effect/vitest";
9+
import * as HostProcess from "@t3tools/shared/HostProcess";
910
import * as Effect from "effect/Effect";
1011
import * as Exit from "effect/Exit";
1112
import { vi } from "vite-plus/test";
1213
import type { AppImageUpdater as AppImageUpdaterType } from "electron-updater";
1314

1415
const { getUpdater } = vi.hoisted(() => ({ getUpdater: vi.fn<() => AppImageUpdaterType>() }));
15-
vi.mock("electron-updater", () => ({
16-
get autoUpdater() {
17-
return getUpdater();
18-
},
19-
}));
16+
vi.mock("electron-updater", async (importOriginal) => {
17+
const { AppImageUpdater } = await importOriginal<typeof import("electron-updater")>();
18+
return {
19+
AppImageUpdater,
20+
get autoUpdater() {
21+
return getUpdater();
22+
},
23+
};
24+
});
2025

2126
import * as ElectronUpdater from "./ElectronUpdater.ts";
2227

@@ -40,15 +45,6 @@ class FixtureUpdater extends AppImageUpdater {
4045
override quitAndInstall(isSilent = false, isForceRunAfter = false) {
4146
this.doInstall({ isSilent, isForceRunAfter, isAdminRightsRequired: false });
4247
}
43-
44-
protected override spawnLog(destination: string) {
45-
assert.equal(
46-
NodeChildProcess.execFileSync(process.env.APPIMAGE!, { encoding: "utf8" }),
47-
"updated\n",
48-
);
49-
assert.equal(NodeChildProcess.execFileSync(destination, { encoding: "utf8" }), "updated\n");
50-
return Promise.resolve(true);
51-
}
5248
}
5349

5450
function fixture(oldName: string, newName: string) {
@@ -64,6 +60,17 @@ function fixture(oldName: string, newName: string) {
6460
NodeFS.symlinkSync(oldPath, launcher);
6561
const updater = new FixtureUpdater(download);
6662
getUpdater.mockReturnValue(updater);
63+
const prototype = AppImageUpdater.prototype as unknown as {
64+
spawnLog: (destination: string, args?: Array<string>) => Promise<boolean>;
65+
};
66+
const spawnLog = vi.spyOn(prototype, "spawnLog").mockImplementation((destination, args = []) => {
67+
assert.equal(NodeChildProcess.execFileSync(oldPath, { encoding: "utf8" }), "updated\n");
68+
assert.equal(
69+
NodeChildProcess.execFileSync(destination, args, { encoding: "utf8" }),
70+
"updated\n",
71+
);
72+
return Promise.resolve(true);
73+
});
6774
const originalAppImage = process.env.APPIMAGE;
6875
process.env.APPIMAGE = oldPath;
6976
return {
@@ -74,6 +81,7 @@ function fixture(oldName: string, newName: string) {
7481
launcher,
7582
updater,
7683
[Symbol.dispose]() {
84+
spawnLog.mockRestore();
7785
if (originalAppImage === undefined) delete process.env.APPIMAGE;
7886
else process.env.APPIMAGE = originalAppImage;
7987
NodeFS.rmSync(directory, { recursive: true, force: true });
@@ -98,10 +106,10 @@ describe.skipIf(process.platform !== "linux")("AppImage launch paths", () => {
98106
Effect.gen(function* () {
99107
using f = fixture("My T3-0.1.7.AppImage", "T3-Code-x86_64.AppImage");
100108
yield* ElectronUpdater.make.quitAndInstall({ isSilent: true, isForceRunAfter: true });
101-
process.env.APPIMAGE = f.newPath;
109+
const env = yield* HostProcess.Environment;
110+
env.APPIMAGE = f.newPath;
102111
const nextDownload = NodePath.join(f.directory, "cache", "T3-Code-x86_64.AppImage");
103112
NodeFS.writeFileSync(nextDownload, "#!/bin/sh\necho updated\n", { mode: 0o755 });
104-
process.env.APPIMAGE = NodePath.join(f.directory, "T3-Code-x86_64.AppImage");
105113
getUpdater.mockReturnValue(new FixtureUpdater(nextDownload));
106114
yield* ElectronUpdater.make.quitAndInstall({ isSilent: true, isForceRunAfter: true });
107115
assert.equal(NodeChildProcess.execFileSync(f.launcher, { encoding: "utf8" }), "updated\n");

‎apps/desktop/src/electron/ElectronUpdater.ts‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as NodeFS from "node:fs";
33
import * as NodePath from "node:path";
44

5+
import * as HostProcess from "@t3tools/shared/HostProcess";
56
import * as Context from "effect/Context";
67
import * as Effect from "effect/Effect";
78
import * as Layer from "effect/Layer";
@@ -171,44 +172,42 @@ export const make = ElectronUpdater.of({
171172
catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }),
172173
}).pipe(Effect.asVoid);
173174
}),
174-
quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function ({
175+
quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function* ({
175176
isSilent,
176177
isForceRunAfter,
177178
}) {
178-
return Effect.suspend(() => {
179-
const channel = autoUpdater.channel;
180-
if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater);
181-
return Effect.try({
182-
try: () => {
183-
const appImagePath = process.env.APPIMAGE;
184-
const preserveAppImagePath = (destination: string) => {
185-
const filename = NodePath.basename(destination);
186-
// Only bridge to a name the updater will keep, avoiding growing alias chains.
187-
if (appImagePath === undefined || /\d+\.\d+\.\d+/.test(filename)) return;
188-
try {
189-
// 6.8.9 emits synchronously after moving the file, before relaunch/exit.
190-
NodeFS.symlinkSync(filename, appImagePath);
191-
} catch {
192-
autoUpdater.logger?.warn("Could not preserve the previous AppImage launch path.");
193-
}
194-
};
195-
if (appImagePath !== undefined) {
196-
autoUpdater.on("appimage-filename-updated", preserveAppImagePath);
197-
}
179+
const appImagePath = (yield* HostProcess.Environment).APPIMAGE;
180+
const channel = autoUpdater.channel;
181+
if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater);
182+
return yield* Effect.try({
183+
try: () => {
184+
const preserveAppImagePath = (destination: string) => {
185+
const filename = NodePath.basename(destination);
186+
// Only bridge to a name the updater will keep, avoiding growing alias chains.
187+
if (appImagePath === undefined || /\d+\.\d+\.\d+/.test(filename)) return;
198188
try {
199-
autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
200-
} finally {
201-
autoUpdater.removeListener("appimage-filename-updated", preserveAppImagePath);
189+
// 6.8.9 emits synchronously after moving the file, before relaunch/exit.
190+
NodeFS.symlinkSync(filename, appImagePath);
191+
} catch {
192+
autoUpdater.logger?.warn("Could not preserve the previous AppImage launch path.");
202193
}
203-
},
204-
catch: (cause) =>
205-
new ElectronUpdaterQuitAndInstallError({
206-
channel,
207-
isSilent,
208-
isForceRunAfter,
209-
cause,
210-
}),
211-
});
194+
};
195+
if (appImagePath !== undefined) {
196+
autoUpdater.on("appimage-filename-updated", preserveAppImagePath);
197+
}
198+
try {
199+
autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
200+
} finally {
201+
autoUpdater.removeListener("appimage-filename-updated", preserveAppImagePath);
202+
}
203+
},
204+
catch: (cause) =>
205+
new ElectronUpdaterQuitAndInstallError({
206+
channel,
207+
isSilent,
208+
isForceRunAfter,
209+
cause,
210+
}),
212211
});
213212
}),
214213
on: Effect.fn("ElectronUpdater.on")(function (eventName, listener) {

0 commit comments

Comments
 (0)