From 30a464fcdc6bae4f6955649f318426b2af03d812 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 31 Mar 2025 16:09:57 +0100 Subject: [PATCH 1/6] Update config logging to specify config file path --- src/electron-main.ts | 21 ++++++++++++++++----- src/utils.ts | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/electron-main.ts b/src/electron-main.ts index b2263a65..a9b0e470 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -32,7 +32,7 @@ import { getProfileFromDeeplink, protocolInit } from "./protocol.js"; import { _t, AppLocalization } from "./language-helper.js"; import { setDisplayMediaCallback } from "./displayMediaCallback.js"; import { setupMacosTitleBar } from "./macos-titlebar.js"; -import { loadJsonFile } from "./utils.js"; +import { Json, loadJsonFile } from "./utils.js"; import { setupMediaAuth } from "./media-auth.js"; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -63,6 +63,7 @@ if (argv["help"]) { } const LocalConfigLocation = process.env.ELEMENT_DESKTOP_CONFIG_JSON ?? argv["config"]; +const LocalConfigFilename = "config.json"; // Electron creates the user data directory (with just an empty 'Dictionaries' directory...) // as soon as the app path is set, so pick a random path in it that must exist if it's a @@ -141,6 +142,17 @@ function getAsarPath(): Promise { return asarPathPromise; } +function loadLocalConfigFile(): Json { + if (LocalConfigLocation) { + console.log("Loading local config: " + LocalConfigLocation); + return loadJsonFile(LocalConfigLocation); + } else { + const configDir = app.getPath("userData"); + console.log(`Loading local config: ${path.join(configDir, LocalConfigFilename)}`); + return loadJsonFile(configDir, LocalConfigFilename); + } +} + // Loads the config from asar, and applies a config.json from userData atop if one exists // Writes config to `global.vectorConfig`. Does nothing if `global.vectorConfig` is already set. async function loadConfig(): Promise { @@ -149,7 +161,8 @@ async function loadConfig(): Promise { const asarPath = await getAsarPath(); try { - global.vectorConfig = loadJsonFile(asarPath, "config.json"); + console.log(`Loading global config: ${path.join(asarPath, LocalConfigFilename)}`); + global.vectorConfig = loadJsonFile(asarPath, LocalConfigFilename); } catch { // it would be nice to check the error code here and bail if the config // is unparsable, but we get MODULE_NOT_FOUND in the case of a missing @@ -160,9 +173,7 @@ async function loadConfig(): Promise { try { // Load local config and use it to override values from the one baked with the build - const localConfig = LocalConfigLocation - ? loadJsonFile(LocalConfigLocation) - : loadJsonFile(app.getPath("userData"), "config.json"); + const localConfig = loadLocalConfigFile(); // If the local config has a homeserver defined, don't use the homeserver from the build // config. This is to avoid a problem where Riot thinks there are multiple homeservers diff --git a/src/utils.ts b/src/utils.ts index 0b1f41fd..c8ab2916 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,7 +26,7 @@ type JsonArray = Array; interface JsonObject { [key: string]: JsonObject | JsonArray | JsonValue; } -type Json = JsonArray | JsonObject; +export type Json = JsonArray | JsonObject; /** * Synchronously load a JSON file from the local filesystem. From bff9c344b6c5b30419e29168811895407fee33c2 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 14 Apr 2025 11:22:11 +0100 Subject: [PATCH 2/6] Log when attempting to load from nonexisting JSON file --- src/utils.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index c8ab2916..0b0e3dcd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -34,6 +34,13 @@ export type Json = JsonArray | JsonObject; * @param paths - An array of path segments which will be joined using the system's path delimiter. */ export function loadJsonFile(...paths: string[]): T { - const file = fs.readFileSync(path.join(...paths), { encoding: "utf-8" }); + const joinedPaths = path.join(...paths); + + if (!fs.existsSync(joinedPaths)) { + console.debug(`Skipping nonexisting file: ${joinedPaths}`); + return {} as T; + } + + const file = fs.readFileSync(joinedPaths, { encoding: "utf-8" }); return JSON.parse(file); } From f59c38a07e730a12739f5a0513c73cfa9e39092e Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 14 Apr 2025 13:53:29 +0100 Subject: [PATCH 3/6] Fix type import --- src/electron-main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electron-main.ts b/src/electron-main.ts index a9b0e470..7ea57a2a 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -32,7 +32,7 @@ import { getProfileFromDeeplink, protocolInit } from "./protocol.js"; import { _t, AppLocalization } from "./language-helper.js"; import { setDisplayMediaCallback } from "./displayMediaCallback.js"; import { setupMacosTitleBar } from "./macos-titlebar.js"; -import { Json, loadJsonFile } from "./utils.js"; +import { type Json, loadJsonFile } from "./utils.js"; import { setupMediaAuth } from "./media-auth.js"; const __dirname = dirname(fileURLToPath(import.meta.url)); From 0559886b7779deca14ec232acc61129d99afebb9 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Tue, 15 Apr 2025 10:33:38 +0100 Subject: [PATCH 4/6] Reword log line to use 'app' instead of 'global' Co-authored-by: David Baker --- src/electron-main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electron-main.ts b/src/electron-main.ts index 7ea57a2a..ab38f9c2 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -161,7 +161,7 @@ async function loadConfig(): Promise { const asarPath = await getAsarPath(); try { - console.log(`Loading global config: ${path.join(asarPath, LocalConfigFilename)}`); + console.log(`Loading app config: ${path.join(asarPath, LocalConfigFilename)}`); global.vectorConfig = loadJsonFile(asarPath, LocalConfigFilename); } catch { // it would be nice to check the error code here and bail if the config From db4c544b72ef16dbab010a0408cfb47fbc0a5a12 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 Apr 2025 11:54:11 +0100 Subject: [PATCH 5/6] Use console.log Co-authored-by: Nick Bolton --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 0b0e3dcd..2eabad35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,7 +37,7 @@ export function loadJsonFile(...paths: string[]): T { const joinedPaths = path.join(...paths); if (!fs.existsSync(joinedPaths)) { - console.debug(`Skipping nonexisting file: ${joinedPaths}`); + console.log(`Skipping nonexisting file: ${joinedPaths}`); return {} as T; } From 42ac29359c1f08e68370a92e0bba7b7fffe098b5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 Apr 2025 11:58:55 +0100 Subject: [PATCH 6/6] nonexistent --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 2eabad35..77cefb8a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,7 +37,7 @@ export function loadJsonFile(...paths: string[]): T { const joinedPaths = path.join(...paths); if (!fs.existsSync(joinedPaths)) { - console.log(`Skipping nonexisting file: ${joinedPaths}`); + console.log(`Skipping nonexistent file: ${joinedPaths}`); return {} as T; }