refactor: use debug package for logging from main and utility processes
This commit is contained in:
@@ -91,6 +91,18 @@ Run the app:
|
||||
npm start
|
||||
```
|
||||
|
||||
The [debug](https://www.npmjs.com/package/debug) package is used for some general logging. If you want to see its output, you need to set the `DEBUG` environment variable.
|
||||
|
||||
```sh
|
||||
DEBUG=* npm start
|
||||
```
|
||||
|
||||
In PowerShell you need to write this instead:
|
||||
|
||||
```sh
|
||||
$env:DEBUG='*'; npm start
|
||||
```
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
Lint all project TS files using ESLint.
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
import Debug from "debug"
|
||||
import { dirname, join } from "node:path"
|
||||
import { parentPort } from "node:process"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import { getAllValidData } from "optolith-database-schema"
|
||||
import { getAbsoluteEntityPaths } from "./contents/src/config.js"
|
||||
const debug = Debug("util:database")
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), "contents")
|
||||
|
||||
debug("loading database ...")
|
||||
|
||||
getAllValidData(getAbsoluteEntityPaths(root))
|
||||
.then(database => {
|
||||
debug("database loaded")
|
||||
parentPort.postMessage(database)
|
||||
})
|
||||
.catch(() => {})
|
||||
|
||||
+15
-16
@@ -1,50 +1,49 @@
|
||||
import Debug from "debug"
|
||||
import { app, ipcMain, utilityProcess } from "electron"
|
||||
import { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS, installExtension } from "electron-extension-installer"
|
||||
import { autoUpdater } from "electron-updater"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import { join } from "node:path"
|
||||
import * as process from "node:process"
|
||||
import type { Database } from "../database/index.ts"
|
||||
import { createMainWindow, showMainWindow } from "./mainWindow.ts"
|
||||
import { ensureUserDataPathExists } from "./saveData.ts"
|
||||
import { checkForUpdatesOnRequest, checkForUpdatesOnStartup } from "./updater.ts"
|
||||
const debug = Debug("main")
|
||||
|
||||
app.setAppUserModelId("lukasobermann.optolith")
|
||||
|
||||
console.log("main: loading database ...")
|
||||
debug("loading database ...")
|
||||
const databaseProcess = utilityProcess.fork(join(__dirname, "./database.js"))
|
||||
const databaseLoading = new Promise<Database>(resolve => {
|
||||
databaseProcess.on("message", (message: Database) => {
|
||||
console.log("main: database loaded")
|
||||
debug("database received")
|
||||
resolve(message)
|
||||
})
|
||||
})
|
||||
|
||||
console.log(process.platform)
|
||||
|
||||
const runAsync = (fn: () => Promise<void>) => () => {
|
||||
fn().catch(err => console.error("main: unexpected error", err))
|
||||
fn().catch(err => debug("unexpected error: %O", err))
|
||||
}
|
||||
|
||||
const setUserDataPath = async () => {
|
||||
console.log("main: set user data path ...")
|
||||
debug("setting user data path ...")
|
||||
app.setPath("userData", await ensureUserDataPathExists())
|
||||
}
|
||||
|
||||
const installExtensions = async () => {
|
||||
console.log("main: install extensions ...")
|
||||
debug("install extensions ...")
|
||||
|
||||
const installedExtensions = await installExtension([
|
||||
REACT_DEVELOPER_TOOLS,
|
||||
REDUX_DEVTOOLS,
|
||||
])
|
||||
const installedExtensions: string[] | string | undefined =
|
||||
await Promise.resolve(undefined as string[] | string | undefined) /* await installExtension([
|
||||
REACT_DEVELOPER_TOOLS,
|
||||
REDUX_DEVTOOLS,
|
||||
]) */
|
||||
|
||||
const installedExtensionsString =
|
||||
Array.isArray(installedExtensions)
|
||||
? installedExtensions.join(", ")
|
||||
: installedExtensions
|
||||
: installedExtensions ?? "none"
|
||||
|
||||
console.log(`main: installed extensions: ${installedExtensionsString}`)
|
||||
debug("installed extensions: %s", installedExtensionsString)
|
||||
}
|
||||
|
||||
const readFileInAppPath = (...path: string[]) => readFile(join(app.getAppPath(), ...path), "utf-8")
|
||||
@@ -64,7 +63,7 @@ app.whenReady().then(async () => {
|
||||
})
|
||||
|
||||
const installUpdateInsteadOfStartup = await checkForUpdatesOnStartup()
|
||||
console.log("main: install update instead of startup", installUpdateInsteadOfStartup)
|
||||
debug("skip startup because of update?", installUpdateInsteadOfStartup ? "yes" : "no")
|
||||
if (!installUpdateInsteadOfStartup) {
|
||||
await setUserDataPath()
|
||||
await installExtensions()
|
||||
|
||||
+11
-9
@@ -1,20 +1,22 @@
|
||||
import Debug from "debug"
|
||||
import { BrowserWindow, app, ipcMain, shell } from "electron"
|
||||
import windowStateKeeper from "electron-window-state"
|
||||
import * as path from "node:path"
|
||||
import * as url from "node:url"
|
||||
import type { Database } from "../database/index.ts"
|
||||
const debug = Debug("main:main")
|
||||
|
||||
export const createMainWindow = async () => {
|
||||
console.log("main: Create Window ...")
|
||||
debug("Create Window ...")
|
||||
|
||||
console.log("main (window): Initialize window state keeper")
|
||||
debug("Initialize window state keeper")
|
||||
const mainWindowState = windowStateKeeper({
|
||||
defaultHeight: 720,
|
||||
defaultWidth: 1280,
|
||||
file: "window.json",
|
||||
})
|
||||
|
||||
console.log("main (window): Initialize browser window")
|
||||
debug("Initialize browser window")
|
||||
const mainWindow = new BrowserWindow({
|
||||
x: mainWindowState.x,
|
||||
y: mainWindowState.y,
|
||||
@@ -38,24 +40,24 @@ export const createMainWindow = async () => {
|
||||
|
||||
mainWindow.webContents.setWindowOpenHandler(details => {
|
||||
shell.openExternal(details.url)
|
||||
.catch(console.error)
|
||||
.catch(err => debug("unexpected error: %O", err))
|
||||
return { action: "deny" }
|
||||
})
|
||||
|
||||
console.log("main (window): Manage browser window with state keeper")
|
||||
debug("Manage browser window with state keeper")
|
||||
mainWindowState.manage(mainWindow)
|
||||
|
||||
console.log("main (window): Load url")
|
||||
debug("Load url")
|
||||
await mainWindow.loadURL(url.format({
|
||||
pathname: path.join(__dirname, "renderer_main.html"),
|
||||
protocol: "file:",
|
||||
slashes: true,
|
||||
}))
|
||||
|
||||
mainWindow.webContents.openDevTools()
|
||||
// mainWindow.webContents.openDevTools()
|
||||
|
||||
if (mainWindowState.isMaximized) {
|
||||
console.log("main (window): Maximize window ...")
|
||||
debug("Maximize window ...")
|
||||
mainWindow.maximize()
|
||||
}
|
||||
|
||||
@@ -89,6 +91,6 @@ export const createMainWindow = async () => {
|
||||
|
||||
export const showMainWindow = (mainWindow: BrowserWindow, database: Database) => {
|
||||
mainWindow.webContents.send("database-available", database)
|
||||
console.log("main (window): Show window once database is available")
|
||||
debug("Show window once database is available")
|
||||
mainWindow.show()
|
||||
}
|
||||
|
||||
+16
-13
@@ -1,8 +1,10 @@
|
||||
import Debug from "debug"
|
||||
import { BrowserWindow, ipcMain } from "electron"
|
||||
import { CancellationToken, UpdateCheckResult, autoUpdater } from "electron-updater"
|
||||
import * as path from "node:path"
|
||||
import * as url from "node:url"
|
||||
import appIconMacOS from "../assets/icon/AppIcon.icns"
|
||||
const debug = Debug("main:updater")
|
||||
|
||||
type AvailableUpdateCheckResult = {
|
||||
cancellationToken: CancellationToken
|
||||
@@ -24,6 +26,7 @@ const checkForUpdates = async (): Promise<AvailableUpdateCheckResult | undefined
|
||||
}
|
||||
|
||||
const createUpdaterWindow = async () => {
|
||||
debug("create window")
|
||||
const updaterWindow = new BrowserWindow({
|
||||
icon: appIconMacOS,
|
||||
center: true,
|
||||
@@ -46,7 +49,7 @@ const createUpdaterWindow = async () => {
|
||||
updaterWindow.webContents.openDevTools()
|
||||
|
||||
autoUpdater.on("error", (err: Error) => {
|
||||
console.log("updater: error", err)
|
||||
debug("error %O", err)
|
||||
updaterWindow.webContents.send("auto-updater-error", err)
|
||||
})
|
||||
|
||||
@@ -73,47 +76,47 @@ const prepareUpdaterWindowForAvailableUpdate = (
|
||||
updaterWindow.webContents.send("update-available", updateInfo)
|
||||
|
||||
autoUpdater.signals.progress(progressObj => {
|
||||
console.log(`updater: download progress at ${progressObj.percent} %`)
|
||||
debug("download progress at %d %", progressObj.percent)
|
||||
updaterWindow.webContents.send("download-progress", progressObj)
|
||||
})
|
||||
|
||||
autoUpdater.signals.updateDownloaded(info => {
|
||||
console.log(`updater: update downloaded to "${info.downloadedFile}"`)
|
||||
debug("update downloaded to %s", info.downloadedFile)
|
||||
updaterWindow.webContents.send("update-downloaded", info)
|
||||
})
|
||||
|
||||
ipcMain.on("download-update-later", () => {
|
||||
console.log(`updater: download update later`)
|
||||
debug("download update later")
|
||||
onCancelUpdate?.()
|
||||
updaterWindow.close()
|
||||
})
|
||||
|
||||
ipcMain.on("download-update", () => {
|
||||
console.log(`updater: downloading update ...`)
|
||||
debug("downloading update ...")
|
||||
autoUpdater.downloadUpdate(cancellationToken).catch(console.error)
|
||||
})
|
||||
|
||||
ipcMain.on("install-update-later", () => {
|
||||
console.log(`updater: install update later`)
|
||||
debug("install update later")
|
||||
onCancelUpdate?.()
|
||||
updaterWindow.close()
|
||||
})
|
||||
|
||||
ipcMain.on("quit-and-install-update", () => {
|
||||
console.log(`updater: quit and install update`)
|
||||
debug("quit and install update")
|
||||
onApplyUpdate?.()
|
||||
autoUpdater.quitAndInstall()
|
||||
})
|
||||
}
|
||||
|
||||
export const checkForUpdatesOnStartup = async () => {
|
||||
console.log("updater: checking for updates ...")
|
||||
debug("checking for updates ...")
|
||||
|
||||
const checkResult = await checkForUpdates()
|
||||
const isUpdateAvailable = checkResult !== undefined
|
||||
|
||||
if (isUpdateAvailable) {
|
||||
console.log("updater: update is available")
|
||||
debug("update is available")
|
||||
const updaterWindow = await createUpdaterWindow()
|
||||
const updatePromise = new Promise<boolean>(resolve => {
|
||||
prepareUpdaterWindowForAvailableUpdate(
|
||||
@@ -127,24 +130,24 @@ export const checkForUpdatesOnStartup = async () => {
|
||||
return updatePromise
|
||||
}
|
||||
else {
|
||||
console.log("updater: no update available")
|
||||
debug("no update available")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export const checkForUpdatesOnRequest = async () => {
|
||||
console.log("updater: checking for updates ...")
|
||||
debug("checking for updates ...")
|
||||
const updaterWindow = await createUpdaterWindow()
|
||||
updaterWindow.show()
|
||||
const checkResult = await checkForUpdates()
|
||||
const isUpdateAvailable = checkResult !== undefined
|
||||
|
||||
if (isUpdateAvailable) {
|
||||
console.log("updater: update is available")
|
||||
debug("update is available")
|
||||
prepareUpdaterWindowForAvailableUpdate(updaterWindow, checkResult)
|
||||
}
|
||||
else {
|
||||
console.log("updater: no update available")
|
||||
debug("no update available")
|
||||
updaterWindow.webContents.send("no-update-available")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user