Skip to content

Commit

Permalink
electron: Fix asset path on forge build
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Nov 2, 2024
1 parent d140449 commit e72eb5f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion forge.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/services/code_block_theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
14 changes: 2 additions & 12 deletions src/services/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down
17 changes: 17 additions & 0 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'});

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e72eb5f

Please sign in to comment.