Skip to content

Commit

Permalink
added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
amitojsingh366 committed Apr 17, 2021
1 parent 36ddc1c commit 09370ee
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 78 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-rpc",
"version": "2.0.20",
"version": "2.0.21",
"description": "A easy discord rpc",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -66,6 +66,7 @@
"dependencies": {
"auto-launch": "^5.0.5",
"discord-rpc": "^3.2.0",
"electron-log": "^4.3.4",
"electron-prompt": "^1.6.2",
"electron-updater": "^4.3.8"
},
Expand Down
168 changes: 91 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,120 @@
import { app, BrowserWindow, ipcMain, Menu, shell, Tray } from "electron";
import * as path from "path";
import { autoUpdater } from "electron-updater";
import { RPC_STARTED, startHandler, rpc, RPC_TRYING_TO_START } from "./presence";
import {
RPC_STARTED,
startHandler,
rpc,
RPC_TRYING_TO_START,
} from "./presence";
import { HandleTray } from "./tray";
import AutoLaunch from 'auto-launch';
import AutoLaunch from "auto-launch";
import { MENU_TEMPLATE } from "./constants";
import electronLogger from "electron-log";

export let mainWindow: BrowserWindow;
let tray: Tray;
let menu: Menu;
const instanceLock = app.requestSingleInstanceLock();

electronLogger.transports.file.level = "debug";
autoUpdater.logger = electronLogger;
// just in case we have to revert to a build
autoUpdater.allowDowngrade = true;

export const commons = {
shouldDock: true,
autoLaunch: false,
}
shouldDock: true,
autoLaunch: false,
};

const easyRPCAutoLauncher = new AutoLaunch({
name: 'Easy RPC',
name: "Easy RPC",
});

export async function createWindow() {
mainWindow = new BrowserWindow({
height: 825,
width: 600,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
});
mainWindow = new BrowserWindow({
height: 825,
width: 600,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});

await mainWindow.loadFile(path.join(__dirname, "../public/home.html"));
menu = Menu.buildFromTemplate(MENU_TEMPLATE);
Menu.setApplicationMenu(menu);
const handleLinks = (event: any, url: string) => {
event.preventDefault();
shell.openExternal(url);
};
mainWindow.webContents.on("new-window", handleLinks);
mainWindow.webContents.on("will-navigate", handleLinks);
autoUpdater.checkForUpdatesAndNotify();
await mainWindow.loadFile(path.join(__dirname, "../public/home.html"));
menu = Menu.buildFromTemplate(MENU_TEMPLATE);
Menu.setApplicationMenu(menu);
const handleLinks = (event: any, url: string) => {
event.preventDefault();
shell.openExternal(url);
};
mainWindow.webContents.on("new-window", handleLinks);
mainWindow.webContents.on("will-navigate", handleLinks);
autoUpdater.checkForUpdatesAndNotify();
}

app.on("ready", async () => {
await createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
await startHandler()
tray = new Tray(path.join(__dirname, `../icons/tray.png`));
await HandleTray(tray);
ipcMain.on("@app/shouldDock", (event, shouldDock) => {
commons.shouldDock = shouldDock;
})
ipcMain.on("@app/autoLaunch", (event, autoLaunch) => {
if (autoLaunch) {
easyRPCAutoLauncher.enable();
} else {
easyRPCAutoLauncher.disable();
}
commons.autoLaunch = autoLaunch;
})
ipcMain.on("@app/quit", (event, args) => {
if (RPC_STARTED && rpc) {
rpc.removeAllListeners();
rpc.clearActivity();
rpc.destroy();
}
app.quit();
});
ipcMain.on('@app/version', (event, args) => {
event.sender.send('@app/version', app.getVersion());
});
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.send("@app/shouldDock", "");
mainWindow.webContents.send("@app/autoLaunch", "");
mainWindow.webContents.send("@app/started", "");
await createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
await startHandler();
tray = new Tray(path.join(__dirname, `../icons/tray.png`));
await HandleTray(tray);
ipcMain.on("@app/shouldDock", (event, shouldDock) => {
commons.shouldDock = shouldDock;
});
ipcMain.on("@app/autoLaunch", (event, autoLaunch) => {
if (autoLaunch) {
easyRPCAutoLauncher.enable();
} else {
easyRPCAutoLauncher.disable();
}
commons.autoLaunch = autoLaunch;
});
ipcMain.on("@app/quit", (event, args) => {
if (RPC_STARTED && rpc) {
rpc.removeAllListeners();
rpc.clearActivity();
rpc.destroy();
}
app.quit();
});
ipcMain.on("@app/version", (event, args) => {
event.sender.send("@app/version", app.getVersion());
});
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.send("@app/shouldDock", "");
mainWindow.webContents.send("@app/autoLaunch", "");
mainWindow.webContents.send("@app/started", "");
}
});

if (!instanceLock) {
app.quit();
app.quit();
} else {
app.on("second-instance", (event, argv, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isDestroyed()) {
createWindow();
} else if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
} else {
createWindow();
}
});
app.on("second-instance", (event, argv, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isDestroyed()) {
createWindow();
} else if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
} else {
createWindow();
}
});
}

app.on("window-all-closed", () => {
if ((RPC_STARTED && commons.shouldDock) || (RPC_TRYING_TO_START && commons.shouldDock)) {
return;
} else {
app.quit();
}
});
if (
(RPC_STARTED && commons.shouldDock) ||
(RPC_TRYING_TO_START && commons.shouldDock)
) {
return;
} else {
app.quit();
}
});
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ __metadata:
electron: ^12.0.2
electron-builder: ^22.10.5
electron-builder-notarize: ^1.2.0
electron-log: ^4.3.4
electron-prompt: ^1.6.2
electron-updater: ^4.3.8
typescript: ^4.2.3
Expand Down Expand Up @@ -1183,6 +1184,13 @@ __metadata:
languageName: node
linkType: hard

"electron-log@npm:^4.3.4":
version: 4.3.4
resolution: "electron-log@npm:4.3.4"
checksum: 93d53f52aa3f93542deeea831a2a3398b7b53920b87f7ec61f78938b60d8abd7d202abbb7577920544c9b5de1db31330d3460eaa29275a2fca1ffbc91c37b2c5
languageName: node
linkType: hard

"electron-notarize@npm:^0.2.0":
version: 0.2.1
resolution: "electron-notarize@npm:0.2.1"
Expand Down

0 comments on commit 09370ee

Please sign in to comment.