Skip to content

Commit

Permalink
feat: store and load user selected channel
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Feb 10, 2025
1 parent 8ddbd08 commit 1b76f18
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const logger = createLogger('background.ts');
autoUpdater.logger = electronLogger;

// Let the app downgrade if the latest version has been removed from the CDN
const appHome = getAppHome(os.platform(), os.homedir(), path.join);

autoUpdater.allowDowngrade = true;
autoUpdater.channel = loadChannel();

const logAndEmit = (event: string, ...args: any[]) => {
logger.debug("Emitting downloader event", event, args)
Expand Down Expand Up @@ -60,7 +63,6 @@ function getAppSettings(appSettingsPath: string) {
}
}

const appHome = getAppHome(os.platform(), os.homedir(), path.join);
logger.debug('App home is', appHome)

let cachedProcessData = null as {
Expand Down Expand Up @@ -144,6 +146,8 @@ ipcMain.handle("app.change-channel", async (event, data) => {
logger.debug("Changing app channel", data)

autoUpdater.channel = data;
updateChannel(data);

const updateResult = await autoUpdater.checkForUpdates();
if (updateResult?.downloadPromise) {
const version = await updateResult.downloadPromise;
Expand Down Expand Up @@ -847,3 +851,50 @@ if (isDevelopment) {
});
}
}

function loadChannel() {
try {
const channelFile = path.join(appHome, 'storage', 'electron-settings.json');
if (!fs.existsSync(channelFile)) {
return 'stable';
}

try {
const channelData = JSON.parse(fs.readFileSync(channelFile, 'utf-8'));
if (channelData.channel) {
return channelData.channel;
}
} catch (e) {
logger.error("Failed to load channel data", e)
}

} catch (e) {
logger.error("Failed to load channel", e)
}

return 'stable';
}

function updateChannel(channel: string) {
try {
const parent = path.join(appHome, 'storage');
const channelFile = path.join(parent, 'electron-settings.json');
if (!fs.existsSync(parent)) {
fs.mkdirSync(parent, { recursive: true });
}

let existingData: any | null = null;
if (fs.existsSync(channelFile)) {
existingData = JSON.parse(fs.readFileSync(channelFile, 'utf-8'));
}

const newData = {
...existingData,
channel
};

fs.writeFileSync(channelFile, JSON.stringify(newData, null, 2));
} catch (e) {
logger.error("Failed to update channel", e)
}
}

0 comments on commit 1b76f18

Please sign in to comment.