diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.yml index 53a7aacc..ccc81163 100644 --- a/desktop/electron-builder.yml +++ b/desktop/electron-builder.yml @@ -1,20 +1,20 @@ appId: re.workadventu.desktop -productName: "WorkAdventure desktop" files: - - "build/**/*" + - "dist/**/*" - "assets/**/*" - "local-app/**/*" +directories: + output: ./build + dmg: icon: false linux: category: "TODO;TODO" packageCategory: "TODO;TODO" - maintainer: "thecodingmachine" icon: "assets/icons/logo.icns" - description: "Desktop app for WorkAdventure" target: - AppImage artifactName: "${productName}-${version}-${arch}.${ext}" diff --git a/desktop/package.json b/desktop/package.json index eb7e3bed..5b501f49 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -1,12 +1,13 @@ { "name": "workadventure-desktop", "version": "1.0.0", - "description": "", - "main": "build/main.js", + "description": "Desktop application for WorkAdventure", + "author": "thecodingmachine", + "main": "dist/main.js", "license": "SEE LICENSE IN LICENSE.txt", "scripts": { - "build": "tsup-node -d build ./src/main.ts ./src/preload-local-app/preload.ts ./src/preload-app/preload.ts", - "dev": "yarn build --watch --onSuccess 'yarn electron build/main.js'", + "build": "tsup-node ./src/main.ts ./src/preload-local-app/preload.ts ./src/preload-app/preload.ts", + "dev": "yarn build --watch --onSuccess 'yarn electron dist/main.js'", "bundle": "electron-builder install-app-deps && electron-builder", "release": "yarn bundle", "typecheck": "tsc --noEmit", diff --git a/desktop/src/app.ts b/desktop/src/app.ts index 39a4fb97..918905bb 100644 --- a/desktop/src/app.ts +++ b/desktop/src/app.ts @@ -5,6 +5,8 @@ import { createTray } from "./tray"; import autoUpdater from "./auto-updater"; import updateAutoLaunch from "./update-auto-launch"; import ipc, { emitMutedKeyPress } from "./ipc"; +import settings from "./settings"; +import { setLogLevel } from "./log"; function init() { const appLock = app.requestSingleInstanceLock(); @@ -32,7 +34,12 @@ function init() { }); // This method will be called when Electron has finished loading - app.on("ready", () => { + app.on("ready", async () => { + await settings.init(); + + const logLevel = settings.get("log_level"); + setLogLevel(logLevel || "info"); + autoUpdater.init(); // enable auto launch diff --git a/desktop/src/ipc.ts b/desktop/src/ipc.ts index 6641f59e..ff993757 100644 --- a/desktop/src/ipc.ts +++ b/desktop/src/ipc.ts @@ -34,7 +34,7 @@ export default () => { ]); } - return settings.get("servers", []); + return settings.get("servers") || []; }); ipcMain.handle("local-app:selectServer", (event, serverId: string) => { @@ -43,7 +43,7 @@ export default () => { throw new Error("App view not found"); } - const servers = settings.get("servers", []); + const servers = settings.get("servers") || []; const selectedServer = servers.find((s) => s._id === serverId); if (!selectedServer) { @@ -55,7 +55,7 @@ export default () => { }); ipcMain.handle("local-app:addServer", (event, serverName: string, serverUrl: string) => { - const servers = settings.get("servers", []); + const servers = settings.get("servers") || []; servers.push({ _id: `${servers.length + 1}`, name: serverName, diff --git a/desktop/src/log.ts b/desktop/src/log.ts index 4da9f8ca..2c9d6e6c 100644 --- a/desktop/src/log.ts +++ b/desktop/src/log.ts @@ -1,15 +1,13 @@ import { dialog, shell } from "electron"; +import ElectronLog from "electron-log"; import log from "electron-log"; -import settings from "./settings"; - function onError(e: Error) { try { log.error(e); dialog.showErrorBox("WorkAdventure - A JavaScript error occurred", e.stack || ""); } catch (logError) { - // eslint-disable-next-line no-console console.error(e); } } @@ -35,11 +33,6 @@ function onRejection(reason: Error) { } function init() { - const logLevel = settings.get("log_level", "info"); - log.transports.console.level = logLevel; - log.transports.file.level = logLevel; - - // eslint-disable-next-line no-console console.log = log.log.bind(log); process.on("uncaughtException", onError); @@ -51,6 +44,11 @@ export async function openLog() { await shell.openPath(logFilePath); } +export function setLogLevel(logLevel: ElectronLog.LogLevel) { + log.transports.console.level = logLevel; + log.transports.file.level = logLevel; +} + export default { init, }; diff --git a/desktop/src/main.ts b/desktop/src/main.ts index eea55629..6d4bcb6a 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -1,11 +1,5 @@ import app from "./app"; import log from "./log"; -import settings from "./settings"; -async function start() { - await settings.init(); - log.init(); -} - -start(); +log.init(); app.init(); diff --git a/desktop/src/settings.ts b/desktop/src/settings.ts index e8e7197b..592f159f 100644 --- a/desktop/src/settings.ts +++ b/desktop/src/settings.ts @@ -14,8 +14,8 @@ async function init() { settings = (await Settings.get()) as SettingsData; } -function get(key: T, fallback?: SettingsData[T]): SettingsData[T] { - if (settings === null) { +function get(key: T): SettingsData[T] | undefined { + if (settings === undefined) { throw new Error("Settings not initialized"); } @@ -23,7 +23,7 @@ function get(key: T, fallback?: SettingsData[T]): } export function set(key: T, value: SettingsData[T]) { - if (settings === null) { + if (settings === undefined) { throw new Error("Settings not initialized"); } diff --git a/desktop/src/tray.ts b/desktop/src/tray.ts index 1ac4597e..9205ee85 100644 --- a/desktop/src/tray.ts +++ b/desktop/src/tray.ts @@ -1,5 +1,5 @@ import { app, Tray, Menu } from "electron"; -import * as path from "path"; +import path from "path"; import { showAboutWindow } from "electron-util"; import * as autoUpdater from "./auto-updater"; @@ -50,7 +50,7 @@ export function createTray() { label: "About", click() { showAboutWindow({ - icon: path.join(__dirname, "..", "assets", "icons", "logo.png"), + icon: path.join(assetsDirectory, "icons", "logo.png"), copyright: "Copyright © WorkAdventure", }); }, diff --git a/desktop/src/update-auto-launch.ts b/desktop/src/update-auto-launch.ts index 5c85f997..678463f4 100644 --- a/desktop/src/update-auto-launch.ts +++ b/desktop/src/update-auto-launch.ts @@ -1,5 +1,5 @@ import AutoLaunch from "auto-launch"; -import * as isDev from "electron-is-dev"; +import isDev from "electron-is-dev"; import { app } from "electron"; import settings from "./settings"; @@ -8,7 +8,7 @@ export default async () => { let isAutoLaunchEnabled = settings.get("auto_launch_enabled"); // set default to enabled - if (isAutoLaunchEnabled === null) { + if (isAutoLaunchEnabled === undefined) { settings.set("auto_launch_enabled", true); isAutoLaunchEnabled = true; } diff --git a/desktop/src/window.ts b/desktop/src/window.ts index cd586123..429a314b 100644 --- a/desktop/src/window.ts +++ b/desktop/src/window.ts @@ -36,7 +36,7 @@ export function createWindow() { autoHideMenuBar: true, show: false, webPreferences: { - preload: path.resolve(__dirname, "..", "build", "preload-local-app", "preload.js"), + preload: path.resolve(__dirname, "..", "dist", "preload-local-app", "preload.js"), }, }); @@ -69,7 +69,7 @@ export function createWindow() { appView = new BrowserView({ webPreferences: { - preload: path.resolve(__dirname, "..", "build", "preload-app", "preload.js"), + preload: path.resolve(__dirname, "..", "dist", "preload-app", "preload.js"), }, }); appView.setBounds({ @@ -89,7 +89,7 @@ export function createWindow() { // mode: "detach", // }); mainWindow?.show(); - mainWindow?.webContents.openDevTools({ mode: "detach" }); + // mainWindow?.webContents.openDevTools({ mode: "detach" }); })(); }); diff --git a/desktop/tsconfig.json b/desktop/tsconfig.json index 2c73c49c..78248218 100644 --- a/desktop/tsconfig.json +++ b/desktop/tsconfig.json @@ -14,7 +14,7 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./build", /* Redirect output structure to the directory. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */