desktop: add support for automatic updates

This commit is contained in:
Abdullah Atta
2023-07-06 10:02:38 +05:00
committed by Abdullah Atta
parent aba2b0ea19
commit 13b8580b2f
7 changed files with 53 additions and 14 deletions

View File

@@ -42,17 +42,18 @@ const sodiumNativePrebuildPath = (arch) =>
`${os.platform()}-${arch}`
);
const webAppPath = path.resolve(path.join(__dirname, "..", "..", "web"));
if (args.rebuild) {
await fs.rm("./build/", { force: true, recursive: true });
await fs.rm("./build/", { force: true, recursive: true });
if (args.rebuild || !existsSync(path.join(webAppPath, "build"))) {
await exec(`cd ${webAppPath} && npm run build:desktop`);
await fs.cp(path.join(webAppPath, "build"), "build", {
recursive: true,
force: true
});
}
await fs.cp(path.join(webAppPath, "build"), "build", {
recursive: true,
force: true
});
if (args.variant === "mas") {
await exec(`npm run bundle:mas`);
} else {

View File

@@ -21,6 +21,8 @@ import { initTRPC } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import { CancellationToken, autoUpdater } from "electron-updater";
import type { AppUpdaterEvents } from "electron-updater/out/AppUpdater";
import { z } from "zod";
import { config } from "../utils/config";
type UpdateInfo = { version: string };
type Progress = { percent: number };
@@ -28,6 +30,7 @@ type Progress = { percent: number };
const t = initTRPC.create();
export const updaterRouter = t.router({
autoUpdates: t.procedure.query(() => config.automaticUpdates),
install: t.procedure.query(() => autoUpdater.quitAndInstall()),
download: t.procedure.query(async () => {
const cancellationToken = new CancellationToken();
@@ -37,6 +40,13 @@ export const updaterRouter = t.router({
await autoUpdater.checkForUpdates();
}),
toggleAutoUpdates: t.procedure
.input(z.boolean().optional())
.mutation(({ input }) => {
config.automaticUpdates =
input === undefined ? !config.automaticUpdates : input;
}),
onChecking: createSubscription("checking-for-update"),
onDownloaded: createSubscription<"update-downloaded", UpdateInfo>(
"update-downloaded"

View File

@@ -34,10 +34,6 @@ import { router, api } from "./api";
import { config } from "./utils/config";
import path from "path";
if (!RELEASE) {
require("electron-reloader")(module);
}
if (process.platform == "win32" && process.env.PORTABLE_EXECUTABLE_DIR) {
console.log("Portable app: true");
const root = path.join(process.env.PORTABLE_EXECUTABLE_DIR, "Notesnook");

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { autoUpdater } from "electron-updater";
import { config } from "./config";
async function configureAutoUpdater() {
autoUpdater.setFeedURL({
@@ -26,7 +27,7 @@ async function configureAutoUpdater() {
useMultipleRangeRequest: false
});
autoUpdater.autoDownload = false;
autoUpdater.autoDownload = config.automaticUpdates;
autoUpdater.allowDowngrade = false;
autoUpdater.allowPrerelease = false;
autoUpdater.autoInstallOnAppQuit = true;

View File

@@ -40,7 +40,8 @@ export const config = {
privacyMode: false,
isSpellCheckerEnabled: false,
zoomFactor: 0,
theme: nativeTheme.themeSource
theme: nativeTheme.themeSource,
automaticUpdates: true
};
type ConfigKey = keyof typeof config;

View File

@@ -134,5 +134,27 @@ export const BehaviourSettings: SettingsGroup[] = [
]
}
]
},
{
key: "updates",
section: "behaviour",
header: "Updates",
settings: [
{
key: "auto-updates",
title: "Automatic updates",
description:
"Automatically download & install updates in the background without prompting first.",
onStateChange: (listener) =>
useSettingStore.subscribe((s) => s.autoUpdates, listener),
components: [
{
type: "toggle",
isToggled: () => useSettingStore.getState().autoUpdates,
toggle: () => useSettingStore.getState().toggleAutoUpdates()
}
]
}
]
}
];

View File

@@ -53,6 +53,7 @@ class SettingStore extends BaseStore {
* @type {DesktopIntegrationSettings | undefined}
*/
desktopIntegrationSettings = undefined;
autoUpdates = true;
refresh = async () => {
this.set({
@@ -63,7 +64,8 @@ class SettingStore extends BaseStore {
desktopIntegrationSettings:
await desktop?.integration.desktopIntegration.query(),
privacyMode: await desktop?.integration.privacyMode.query(),
zoomFactor: await desktop?.integration.zoomFactor.query()
zoomFactor: await desktop?.integration.zoomFactor.query(),
autoUpdates: await desktop?.updater.autoUpdates.query()
});
};
@@ -160,6 +162,12 @@ class SettingStore extends BaseStore {
this.set({ privacyMode: !privacyMode });
await desktop?.integration.setPrivacyMode.mutate(!privacyMode);
};
toggleAutoUpdates = async () => {
const autoUpdates = this.get().autoUpdates;
this.set({ autoUpdates: !autoUpdates });
await desktop?.updater.toggleAutoUpdates.mutate(!autoUpdates);
};
}
const [useStore, store] = createStore(SettingStore);