This commit is contained in:
SrGooglo 2024-03-25 09:27:23 +01:00
parent 4cd09d8f44
commit a95f566518

View File

@ -1,8 +1,8 @@
import sendToRender from "./utils/sendToRender" import sendToRender from "./utils/sendToRender"
global.SettingsStore = new Store({ global.SettingsStore = new Store({
name: "settings", name: "settings",
watch: true, watch: true,
}) })
import path from "node:path" import path from "node:path"
@ -27,265 +27,266 @@ const ProtocolRegistry = require("protocol-registry")
const protocolRegistryNamespace = "rsbundle" const protocolRegistryNamespace = "rsbundle"
class ElectronApp { class ElectronApp {
constructor() { constructor() {
this.pkgManager = new PkgManager() this.pkgManager = new PkgManager()
this.win = null this.win = null
} }
handlers = { handlers = {
"pkg:list": async () => { "pkg:list": async () => {
return await this.pkgManager.getInstalledPackages() return await this.pkgManager.getInstalledPackages()
}, },
"pkg:get": async (event, manifest_id) => { "pkg:get": async (event, manifest_id) => {
return await this.pkgManager.getInstalledPackages(manifest_id) return await this.pkgManager.getInstalledPackages(manifest_id)
}, },
"pkg:read": async (event, manifest_url) => { "pkg:read": async (event, manifest_url) => {
return JSON.stringify(await readManifest(manifest_url)) return JSON.stringify(await readManifest(manifest_url))
}, },
"pkg:install": async (event, manifest) => { "pkg:install": async (event, manifest) => {
this.pkgManager.install(manifest) this.pkgManager.install(manifest)
}, },
"pkg:update": async (event, manifest_id, { execOnFinish = false } = {}) => { "pkg:update": async (event, manifest_id, { execOnFinish = false } = {}) => {
await this.pkgManager.update(manifest_id) await this.pkgManager.update(manifest_id)
if (execOnFinish) { if (execOnFinish) {
await this.pkgManager.execute(manifest_id) await this.pkgManager.execute(manifest_id)
} }
}, },
"pkg:apply": async (event, manifest_id, changes) => { "pkg:apply": async (event, manifest_id, changes) => {
return await this.pkgManager.applyChanges(manifest_id, changes) return await this.pkgManager.applyChanges(manifest_id, changes)
}, },
"pkg:retry_install": async (event, manifest_id) => { "pkg:retry_install": async (event, manifest_id) => {
const pkg = await this.pkgManager.getInstalledPackages(manifest_id) const pkg = await this.pkgManager.getInstalledPackages(manifest_id)
if (!pkg) { if (!pkg) {
return false return false
} }
//await this.pkgManager.uninstall(manifest_id) await this.pkgManager.install(pkg)
await this.pkgManager.install(pkg) },
}, "pkg:cancel_install": async (event, manifest_id) => {
"pkg:cancel_install": async (event, manifest_id) => { return await this.pkgManager.uninstall(manifest_id)
return await this.pkgManager.uninstall(manifest_id) },
}, "pkg:uninstall": async (event, ...args) => {
"pkg:uninstall": async (event, ...args) => { return await this.pkgManager.uninstall(...args)
return await this.pkgManager.uninstall(...args) },
}, "pkg:execute": async (event, ...args) => {
"pkg:execute": async (event, ...args) => { return await this.pkgManager.execute(...args)
return await this.pkgManager.execute(...args) },
}, "pkg:open": async (event, manifest_id) => {
"pkg:open": async (event, manifest_id) => { return await this.pkgManager.open(manifest_id)
return await this.pkgManager.open(manifest_id) },
}, "updater:check": () => {
"updater:check": () => { autoUpdater.checkForUpdates()
autoUpdater.checkForUpdates() },
}, "updater:apply": () => {
"updater:apply": () => { setTimeout(() => {
setTimeout(() => { autoUpdater.quitAndInstall()
autoUpdater.quitAndInstall() }, 3000)
}, 3000) },
}, "settings:get": (e, key) => {
"settings:get": (e, key) => { return global.SettingsStore.get(key)
return global.SettingsStore.get(key) },
}, "settings:set": (e, key, value) => {
"settings:set": (e, key, value) => { return global.SettingsStore.set(key, value)
return global.SettingsStore.set(key, value) },
}, "settings:delete": (e, key) => {
"settings:delete": (e, key) => { return global.SettingsStore.delete(key)
return global.SettingsStore.delete(key) },
}, "settings:has": (e, key) => {
"settings:has": (e, key) => { return global.SettingsStore.has(key)
return global.SettingsStore.has(key) },
}, "app:init": async (event, data) => {
"app:init": async (event, data) => { await setup()
await setup()
// check if can decode google drive token // check if can decode google drive token
const googleDrive_enabled = !!(await GoogleDriveAPI.readCredentials()) const googleDrive_enabled = !!(await GoogleDriveAPI.readCredentials())
return { return {
pkg: pkg, pkg: pkg,
authorizedServices: { authorizedServices: {
drive: googleDrive_enabled drive: googleDrive_enabled
} }
} }
} }
} }
events = { events = {
"open-runtime-path": () => { "open-runtime-path": () => {
return this.pkgManager.openRuntimePath() return this.pkgManager.openRuntimePath()
}, },
"open-dev-logs": () => { "open-dev-logs": () => {
return return sendToRender("new:message", {
} message: "Not implemented yet",
} })
}
}
createWindow() { createWindow() {
this.win = global.win = new BrowserWindow({ this.win = global.win = new BrowserWindow({
width: 450, width: 450,
height: 670, height: 670,
show: false, show: false,
resizable: false, resizable: false,
autoHideMenuBar: true, autoHideMenuBar: true,
icon: "../../resources/icon.png", icon: "../../resources/icon.png",
webPreferences: { webPreferences: {
preload: path.join(__dirname, "../preload/index.js"), preload: path.join(__dirname, "../preload/index.js"),
sandbox: false sandbox: false
} }
}) })
this.win.on("ready-to-show", () => { this.win.on("ready-to-show", () => {
this.win.show() this.win.show()
}) })
this.win.webContents.setWindowOpenHandler((details) => { this.win.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url) shell.openExternal(details.url)
return { action: "deny" } return { action: "deny" }
}) })
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) { if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
this.win.loadURL(process.env["ELECTRON_RENDERER_URL"]) this.win.loadURL(process.env["ELECTRON_RENDERER_URL"])
} else { } else {
this.win.loadFile(path.join(__dirname, "../renderer/index.html")) this.win.loadFile(path.join(__dirname, "../renderer/index.html"))
} }
} }
handleURLProtocol(url) { handleURLProtocol(url) {
const urlStarter = `${protocolRegistryNamespace}://` const urlStarter = `${protocolRegistryNamespace}://`
if (url.startsWith(urlStarter)) { if (url.startsWith(urlStarter)) {
const urlValue = url.split(urlStarter)[1] const urlValue = url.split(urlStarter)[1]
const explicitAction = urlValue.split("%3E") const explicitAction = urlValue.split("%3E")
if (explicitAction[1]) { if (explicitAction[1]) {
const [action, value] = explicitAction const [action, value] = explicitAction
switch (action) { switch (action) {
case "install": { case "install": {
return sendToRender("installation:invoked", value) return sendToRender("installation:invoked", value)
} }
default: { default: {
return sendToRender("new:message", { return sendToRender("new:message", {
message: "Unrecognized URL action" message: "Unrecognized URL action"
}) })
} }
} }
} else { } else {
// by default if no action is specified, assume is a install action // by default if no action is specified, assume is a install action
return sendToRender("installation:invoked", urlValue) return sendToRender("installation:invoked", urlValue)
} }
} }
} }
handleOnSecondInstance = async (event, commandLine, workingDirectory) => { handleOnSecondInstance = async (event, commandLine, workingDirectory) => {
event.preventDefault() event.preventDefault()
// Someone tried to run a second instance, we should focus our window. // Someone tried to run a second instance, we should focus our window.
if (this.win) { if (this.win) {
if (this.win.isMinimized()) { if (this.win.isMinimized()) {
this.win.restore() this.win.restore()
} }
this.win.focus() this.win.focus()
} }
console.log(`Second instance >`, commandLine) console.log(`Second instance >`, commandLine)
const url = commandLine.pop() const url = commandLine.pop()
await this.handleURLProtocol(url) await this.handleURLProtocol(url)
} }
async initialize() { async initialize() {
// Set app user model id for windows // Set app user model id for windows
electronApp.setAppUserModelId("com.electron") electronApp.setAppUserModelId("com.electron")
const gotTheLock = await app.requestSingleInstanceLock() const gotTheLock = await app.requestSingleInstanceLock()
if (!gotTheLock) { if (!gotTheLock) {
return app.quit() return app.quit()
} }
for (const key in this.handlers) { for (const key in this.handlers) {
ipcMain.handle(key, this.handlers[key]) ipcMain.handle(key, this.handlers[key])
} }
for (const key in this.events) { for (const key in this.events) {
ipcMain.on(key, this.events[key]) ipcMain.on(key, this.events[key])
} }
app.on("second-instance", this.handleOnSecondInstance) app.on("second-instance", this.handleOnSecondInstance)
app.on("open-url", (event, url) => { app.on("open-url", (event, url) => {
event.preventDefault() event.preventDefault()
this.handleURLProtocol(url) this.handleURLProtocol(url)
}) })
app.on("browser-window-created", (_, window) => { app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window) optimizer.watchWindowShortcuts(window)
}) })
app.on("activate", () => { app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) { if (BrowserWindow.getAllWindows().length === 0) {
this.createWindow() this.createWindow()
} }
}) })
app.on("window-all-closed", () => { app.on("window-all-closed", () => {
if (process.platform !== "darwin") { if (process.platform !== "darwin") {
app.quit() app.quit()
} }
}) })
await app.whenReady() await app.whenReady()
if (isDev) { if (isDev) {
if (app.isDefaultProtocolClient(protocolRegistryNamespace)) { if (app.isDefaultProtocolClient(protocolRegistryNamespace)) {
app.removeAsDefaultProtocolClient(protocolRegistryNamespace) app.removeAsDefaultProtocolClient(protocolRegistryNamespace)
} }
ProtocolRegistry.register({ ProtocolRegistry.register({
protocol: protocolRegistryNamespace, protocol: protocolRegistryNamespace,
command: `"${process.execPath}" "${path.resolve(process.argv[1])}" $_URL_`, command: `"${process.execPath}" "${path.resolve(process.argv[1])}" $_URL_`,
override: true, override: true,
script: true, script: true,
terminal: false terminal: false
}) })
} else { } else {
if (!app.isDefaultProtocolClient(protocolRegistryNamespace)) { if (!app.isDefaultProtocolClient(protocolRegistryNamespace)) {
app.setAsDefaultProtocolClient(protocolRegistryNamespace) app.setAsDefaultProtocolClient(protocolRegistryNamespace)
} }
} }
await GoogleDriveAPI.init() await GoogleDriveAPI.init()
await this.createWindow() await this.createWindow()
if (!isDev) { if (!isDev) {
autoUpdater.on("update-available", (ev, info) => { autoUpdater.on("update-available", (ev, info) => {
console.log(info) console.log(info)
sendToRender("app:checking_update_downloading", info) sendToRender("app:checking_update_downloading", info)
}) })
autoUpdater.on("error", (ev, err) => { autoUpdater.on("error", (ev, err) => {
console.error(err) console.error(err)
sendToRender("app:checking_update_error") sendToRender("app:checking_update_error")
}) })
autoUpdater.on("update-downloaded", (ev, info) => { autoUpdater.on("update-downloaded", (ev, info) => {
console.log(info) console.log(info)
sendToRender("app:update_available", info) sendToRender("app:update_available", info)
}) })
sendToRender("app:checking_update") sendToRender("app:checking_update")
await autoUpdater.checkForUpdates() await autoUpdater.checkForUpdates()
} }
} }
} }
new ElectronApp().initialize() new ElectronApp().initialize()