Refactor config and simplify error handling

- Moved several utility and configuration files (rpc.js, store.js, dimensions.js, and menu.js) to a new `config` directory to improve project organization.

- This refactor does not affect the functionality of the app.
This commit is contained in:
Agampreet Singh
2024-07-22 13:21:44 +05:30
parent 67d61a4cc9
commit dc5b3c86d5
6 changed files with 94 additions and 103 deletions

View File

@@ -1,16 +1,14 @@
import { app, screen } from "electron";
let screenWidth, screenHeight;
app.on("ready", () => {
({ width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize);
});
function getScreenWidth() {
export function getScreenWidth() {
return screenWidth;
}
function getScreenHeight() {
export function getScreenHeight() {
return screenHeight;
}
export { getScreenWidth, getScreenHeight };

View File

@@ -1,97 +1,12 @@
import { app, dialog, BrowserWindow, ShareMenu, clipboard } from "electron";
import { dialog, BrowserWindow, ShareMenu, clipboard } from "electron";
import { getValue, setValue } from "./store.js";
import { ElectronBlocker } from "@cliqz/adblocker-electron";
import { clearActivity, setActivity } from "./rpc.js";
import prompt from "electron-prompt";
import { fileURLToPath } from "url";
import { shell } from "electron";
import { dirname } from "path";
import { getScreenWidth, getScreenHeight } from "./dimensions.js";
import useragents from "./useragents.json" with { type: "json" };
import updaterpkg from "electron-updater";
import fetch from "cross-fetch";
import axios from "axios";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const { autoUpdater } = updaterpkg;
async function checkForUpdates() {
try {
const res = await axios.get(
"https://api.github.com/repos/agam778/MS-365-Electron/releases/latest"
);
const data = res.data;
const currentVersion = "v" + app.getVersion();
const latestVersion = data.tag_name;
if (currentVersion !== latestVersion) {
if (process.platform === "win32" || process.platform === "darwin") {
autoUpdater.checkForUpdatesAndNotify().then((result) => {
if (result === null) {
dialog.showMessageBoxSync({
type: "info",
title: "No Update Available",
message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`,
buttons: ["OK"],
});
}
});
return;
} else {
const updatedialog = dialog.showMessageBoxSync({
type: "info",
title: "Update Available",
message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nPlease update to the latest version.`,
buttons: ["Download", "Close"],
});
if (updatedialog === 0) {
shell.openExternal("https://github.com/agam778/MS-365-Electron/releases/latest");
}
}
} else {
dialog.showMessageBoxSync({
type: "info",
title: "No Update Available",
message: `Your App's version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`,
buttons: ["OK"],
});
}
} catch (error) {
console.error("Error checking for updates:", error);
}
}
async function openExternalLink(url) {
await shell.openExternal(url);
}
async function openLogsFolder() {
if (process.platform === "win32") {
await shell.openPath(
"C:\\Users\\" + process.env.USERNAME + "\\AppData\\Roaming\\ms-365-electron\\logs\\"
);
} else if (process.platform === "darwin") {
await shell.openPath("/Users/" + process.env.USER + "/Library/Logs/ms-365-electron/");
} else if (process.platform === "linux") {
await shell.openPath("/home/" + process.env.USER + "/.config/ms-365-electron/logs/");
}
}
function setUserAgent(useragent) {
setValue("useragentstring", useragent);
const updatedialog = dialog.showMessageBoxSync({
type: "info",
title: "User-Agent string changed",
message: `You have switched to the ${useragent} User-Agent string.\n\nPlease restart the app for the changes to take effect.`,
buttons: ["Later", "Restart"],
});
if (updatedialog === 1) {
app.relaunch();
app.exit();
}
}
import { checkForUpdates, openExternalLink, openLogsFolder, setUserAgent } from "./utils.js";
import useragents from "../useragents.json" with { type: "json" };
const commonPreferencesSubmenu = [
{

View File

@@ -1,4 +1,4 @@
import useragents from "./useragents.json" with { type: "json" };
import useragents from "../useragents.json" with { type: "json" };
import Store from "electron-store";
const store = new Store();

82
app/config/utils.js Normal file
View File

@@ -0,0 +1,82 @@
import { app, dialog, shell } from "electron";
import axios from "axios";
import { setValue } from "./store.js";
import updaterpkg from "electron-updater";
const { autoUpdater } = updaterpkg;
export async function checkForUpdates() {
try {
const res = await axios.get(
"https://api.github.com/repos/agam778/MS-365-Electron/releases/latest"
);
const data = res.data;
const currentVersion = "v" + app.getVersion();
const latestVersion = data.tag_name;
if (currentVersion !== latestVersion) {
if (process.platform === "win32" || process.platform === "darwin") {
autoUpdater.checkForUpdatesAndNotify().then((result) => {
if (result === null) {
dialog.showMessageBoxSync({
type: "info",
title: "No Update Available",
message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`,
buttons: ["OK"],
});
}
});
return;
} else {
const updatedialog = dialog.showMessageBoxSync({
type: "info",
title: "Update Available",
message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nPlease update to the latest version.`,
buttons: ["Download", "Close"],
});
if (updatedialog === 0) {
shell.openExternal("https://github.com/agam778/MS-365-Electron/releases/latest");
}
}
} else {
dialog.showMessageBoxSync({
type: "info",
title: "No Update Available",
message: `Your App's version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`,
buttons: ["OK"],
});
}
} catch (error) {
console.error("Error checking for updates:", error);
}
}
export async function openExternalLink(url) {
await shell.openExternal(url);
}
export async function openLogsFolder() {
if (process.platform === "win32") {
await shell.openPath(
"C:\\Users\\" + process.env.USERNAME + "\\AppData\\Roaming\\ms-365-electron\\logs\\"
);
} else if (process.platform === "darwin") {
await shell.openPath("/Users/" + process.env.USER + "/Library/Logs/ms-365-electron/");
} else if (process.platform === "linux") {
await shell.openPath("/home/" + process.env.USER + "/.config/ms-365-electron/logs/");
}
}
export function setUserAgent(useragent) {
setValue("useragentstring", useragent);
const updatedialog = dialog.showMessageBoxSync({
type: "info",
title: "User-Agent string changed",
message: `You have switched to the ${useragent} User-Agent string.\n\nPlease restart the app for the changes to take effect.`,
buttons: ["Later", "Restart"],
});
if (updatedialog === 1) {
app.relaunch();
app.exit();
}
}

View File

@@ -1,19 +1,19 @@
import { app, Menu, BrowserWindow, dialog, nativeImage, screen } from "electron";
import { clearActivity, setActivity, loginToRPC } from "./rpc.js";
import { clearActivity, setActivity, loginToRPC } from "./config/rpc.js";
import { initialize, trackEvent } from "@aptabase/electron/main";
import { ElectronBlocker } from "@cliqz/adblocker-electron";
import { getValue } from "./store.js";
import { getValue } from "./config/store.js";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { join } from "path";
import { getScreenWidth, getScreenHeight } from "./dimensions.js";
import { getScreenWidth, getScreenHeight } from "./config/dimensions.js";
import Windows from "./useragents.json" with { type: "json" };
import checkInternetConnected from "check-internet-connected";
import contextMenu from "electron-context-menu";
import updaterpkg from "electron-updater";
import ElectronDl from "electron-dl";
import menulayout from "./menu.js";
import menulayout from "./config/menu.js";
import logpkg from "electron-log";
const { transports, log: _log, functions } = logpkg;
@@ -122,11 +122,7 @@ app.on("web-contents-created", (event, contents) => {
if (url.includes("page=Download")) {
return { action: "allow" };
} else {
BrowserWindow.getFocusedWindow()
.loadURL(url)
.catch((err) => {
// do not show error
});
BrowserWindow.getFocusedWindow().loadURL(url).catch();
if (getValue("discordrpcstatus") === "true") {
setActivity(`On "${BrowserWindow.getFocusedWindow().webContents.getTitle()}"`);
}