From e72eb5f27c331c4b8a3714bed6bb175c96c2d151 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 2 Nov 2024 11:49:33 +0200 Subject: [PATCH] electron: Fix asset path on forge build --- forge.config.cjs | 3 ++- src/services/code_block_theme.ts | 4 +++- src/services/i18n.ts | 14 ++------------ src/services/utils.ts | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/forge.config.cjs b/forge.config.cjs index 8d2db5da5..3d29c84f0 100644 --- a/forge.config.cjs +++ b/forge.config.cjs @@ -15,7 +15,8 @@ module.exports = { ...getExtraResourcesForPlatform(), // Moved to resources (TriliumNext Notes.app/Contents/Resources on macOS) - "translations/" + "translations/", + "node_modules/@highlightjs/cdn-assets/styles" ], afterComplete: [(buildPath, _electronVersion, platform, _arch, callback) => { const extraResources = getExtraResourcesForPlatform(); diff --git a/src/services/code_block_theme.ts b/src/services/code_block_theme.ts index 2b0757e23..ecf59d9b3 100644 --- a/src/services/code_block_theme.ts +++ b/src/services/code_block_theme.ts @@ -7,6 +7,8 @@ import fs from "fs"; import themeNames from "./code_block_theme_names.json" with { type: "json" } import { t } from "i18next"; +import { join } from "path"; +import { getResourceDir } from "./utils.js"; /** * Represents a color scheme for the code block syntax highlight. @@ -28,7 +30,7 @@ interface ColorTheme { * @returns the supported themes, grouped. */ export function listSyntaxHighlightingThemes() { - const path = "node_modules/@highlightjs/cdn-assets/styles"; + const path = join(getResourceDir(), "styles"); const systemThemes = readThemesFromFileSystem(path); return { diff --git a/src/services/i18n.ts b/src/services/i18n.ts index 84a707b89..b36e3738a 100644 --- a/src/services/i18n.ts +++ b/src/services/i18n.ts @@ -2,10 +2,8 @@ import i18next from "i18next"; import Backend from "i18next-fs-backend"; import options from "./options.js"; import sql_init from "./sql_init.js"; -import { fileURLToPath } from "url"; -import { dirname, join } from "path"; -import utils from "./utils.js"; -import env from "./env.js"; +import { join } from "path"; +import { getResourceDir } from "./utils.js"; export async function initializeTranslations() { const resourceDir = getResourceDir(); @@ -21,14 +19,6 @@ export async function initializeTranslations() { }); } -function getResourceDir() { - if (utils.isElectron() && !env.isDev()) { - return process.resourcesPath; - } else { - return join(dirname(fileURLToPath(import.meta.url)), "..", ".."); - } -} - function getCurrentLanguage() { let language; if (sql_init.isDbInitialized()) { diff --git a/src/services/utils.ts b/src/services/utils.ts index 990df5004..6e2cdab66 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -7,6 +7,9 @@ import escape from "escape-html"; import sanitize from "sanitize-filename"; import mimeTypes from "mime-types"; import path from "path"; +import { fileURLToPath } from "url"; +import env from "./env.js"; +import { dirname, join } from "path"; const randtoken = generator({source: 'crypto'}); @@ -315,6 +318,20 @@ function isString(x: any) { return Object.prototype.toString.call(x) === "[object String]"; } +/** + * Returns the directory for resources. On Electron builds this corresponds to the `resources` subdirectory inside the distributable package. + * On development builds, this simply refers to the root directory of the application. + * + * @returns the resource dir. + */ +export function getResourceDir() { + if (isElectron() && !env.isDev()) { + return process.resourcesPath; + } else { + return join(dirname(fileURLToPath(import.meta.url)), "..", ".."); + } +} + export default { randomSecureToken, randomString,