From b6c5880484eabea2499e414fef859e895538cb89 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 19 Jul 2024 00:18:35 +0300 Subject: [PATCH] server-esm: Fix use of __dirname --- package-lock.json | 1 - package.json | 1 - spec/etapi/import.spec.ts | 5 ++++- src/app.ts | 14 +++++++++----- src/etapi/spec.ts | 3 ++- src/routes/assets.ts | 3 ++- src/services/resource_dir.ts | 3 ++- src/services/tray.ts | 4 +++- src/services/window.ts | 5 ++++- tsconfig.json | 32 +++++++++++++++++++++++++++++++- 10 files changed, 57 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 097bcd9ca3..c0258773cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -105,7 +105,6 @@ "@types/jsdom": "^21.1.6", "@types/mime-types": "^2.1.4", "@types/multer": "^1.4.11", - "@types/node": "^20.11.19", "@types/safe-compare": "^1.1.2", "@types/sanitize-html": "^2.11.0", "@types/sax": "^1.2.7", diff --git a/package.json b/package.json index e5e1949023..9d8503b082 100644 --- a/package.json +++ b/package.json @@ -128,7 +128,6 @@ "@types/jsdom": "^21.1.6", "@types/mime-types": "^2.1.4", "@types/multer": "^1.4.11", - "@types/node": "^20.11.19", "@types/safe-compare": "^1.1.2", "@types/sanitize-html": "^2.11.0", "@types/sax": "^1.2.7", diff --git a/spec/etapi/import.spec.ts b/spec/etapi/import.spec.ts index 88c9e0a889..942a9871fd 100644 --- a/spec/etapi/import.spec.ts +++ b/spec/etapi/import.spec.ts @@ -1,12 +1,15 @@ import etapi from "../support/etapi.js"; import fs from "fs"; import path from "path"; +import { fileURLToPath } from "url"; etapi.describeEtapi("import", () => { // temporarily skip this test since test-export.zip is missing xit("import", async () => { + const scriptDir = path.dirname(fileURLToPath(import.meta.url)); + const zipFileBuffer = fs.readFileSync( - path.resolve(__dirname, "test-export.zip") + path.resolve(scriptDir, "test-export.zip") ); const response = await etapi.postEtapiContent( diff --git a/src/app.ts b/src/app.ts index 181f3aff14..49f856fd20 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,8 @@ import favicon from "serve-favicon"; import cookieParser from "cookie-parser"; import helmet from "helmet"; import compression from "compression"; +import { fileURLToPath } from "url"; +import { dirname } from "path"; import sessionParser from "./routes/session_parser.js"; import utils from "./services/utils.js"; import assets from "./routes/assets.js"; @@ -16,8 +18,10 @@ await import('./becca/becca_loader'); const app = express(); +const scriptDir = dirname(fileURLToPath(import.meta.url)); + // view engine setup -app.set('views', path.join(__dirname, 'views')); +app.set('views', path.join(scriptDir, 'views')); app.set('view engine', 'ejs'); if (!utils.isElectron()) { @@ -35,11 +39,11 @@ app.use(express.json({ limit: '500mb' })); app.use(express.raw({ limit: '500mb' })); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); -app.use(express.static(path.join(__dirname, 'public/root'))); -app.use(`/manifest.webmanifest`, express.static(path.join(__dirname, 'public/manifest.webmanifest'))); -app.use(`/robots.txt`, express.static(path.join(__dirname, 'public/robots.txt'))); +app.use(express.static(path.join(scriptDir, 'public/root'))); +app.use(`/manifest.webmanifest`, express.static(path.join(scriptDir, 'public/manifest.webmanifest'))); +app.use(`/robots.txt`, express.static(path.join(scriptDir, 'public/robots.txt'))); app.use(sessionParser); -app.use(favicon(`${__dirname}/../images/app-icons/win/icon.ico`)); +app.use(favicon(`${scriptDir}/../images/app-icons/win/icon.ico`)); assets.register(app); routes.register(app); diff --git a/src/etapi/spec.ts b/src/etapi/spec.ts index a5cad4c2a8..9c183e9c13 100644 --- a/src/etapi/spec.ts +++ b/src/etapi/spec.ts @@ -3,7 +3,8 @@ import { Router } from "express"; import fs from "fs"; import path from "path"; -const specPath = path.join(__dirname, 'etapi.openapi.yaml'); +import { fileURLToPath } from "url"; +const specPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'etapi.openapi.yaml'); let spec: string | null = null; function register(router: Router) { diff --git a/src/routes/assets.ts b/src/routes/assets.ts index f715864b80..a22761e523 100644 --- a/src/routes/assets.ts +++ b/src/routes/assets.ts @@ -1,5 +1,6 @@ import assetPath from "../services/asset_path.js"; import path from "path"; +import { fileURLToPath } from "url"; import express from "express"; import env from "../services/env.js"; import serveStatic from "serve-static"; @@ -15,7 +16,7 @@ const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOp }; function register(app: express.Application) { - const srcRoot = path.join(__dirname, '..'); + const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); app.use(`/${assetPath}/app`, persistentCacheStatic(path.join(srcRoot, 'public/app'))); app.use(`/${assetPath}/app-dist`, persistentCacheStatic(path.join(srcRoot, 'public/app-dist'))); app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(srcRoot, 'public/fonts'))); diff --git a/src/services/resource_dir.ts b/src/services/resource_dir.ts index 14f286fd75..d93d1ea358 100644 --- a/src/services/resource_dir.ts +++ b/src/services/resource_dir.ts @@ -2,7 +2,8 @@ import log from "./log.js"; import path from "path"; import fs from "fs"; -const RESOURCE_DIR = path.resolve(__dirname, "../.."); +import { fileURLToPath } from "url"; +const RESOURCE_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); // where the "trilium" executable is const ELECTRON_APP_ROOT_DIR = path.resolve(RESOURCE_DIR, "../.."); diff --git a/src/services/tray.ts b/src/services/tray.ts index 0b6211ced9..a163a5a22a 100644 --- a/src/services/tray.ts +++ b/src/services/tray.ts @@ -2,12 +2,14 @@ import { Menu, Tray } from 'electron'; import path from "path"; import windowService from "./window.js"; import optionService from "./options.js"; +import { fileURLToPath } from "url"; let tray: Tray; // `mainWindow.isVisible` doesn't work with `mainWindow.show` and `mainWindow.hide` - it returns `false` when the window // is minimized let isVisible = true; + // Inspired by https://github.com/signalapp/Signal-Desktop/blob/dcb5bb672635c4b29a51adec8a5658e3834ec8fc/app/tray_icon.ts#L20 const getIconSize = () => { switch (process.platform) { @@ -23,7 +25,7 @@ const getIconPath = () => { const iconSize = getIconSize(); return path.join( - __dirname, + path.dirname(fileURLToPath(import.meta.url)), "../..", "images", "app-icons", diff --git a/src/services/window.ts b/src/services/window.ts index 23a3e7a984..236fca4417 100644 --- a/src/services/window.ts +++ b/src/services/window.ts @@ -10,6 +10,9 @@ import keyboardActionsService from "./keyboard_actions.js"; import remoteMain from "@electron/remote/main" import { App, BrowserWindow, WebContents, ipcMain } from 'electron'; +import { fileURLToPath } from "url"; +import { dirname } from "path"; + // Prevent the window being garbage collected let mainWindow: BrowserWindow | null; let setupWindow: BrowserWindow | null; @@ -128,7 +131,7 @@ function configureWebContents(webContents: WebContents, spellcheckEnabled: boole } function getIcon() { - return path.join(__dirname, '../../images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png'); + return path.join(dirname(fileURLToPath(import.meta.url)), '../../images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png'); } async function createSetupWindow() { diff --git a/tsconfig.json b/tsconfig.json index ee57f11303..1b0d06e530 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,37 @@ "lib": ["ES2022"], "downlevelIteration": true, "skipLibCheck": true, - "esModuleInterop": true + "esModuleInterop": true, + "types": [ + "@types/archiver", + "@types/better-sqlite3", + "@types/cls-hooked", + "@types/compression", + "@types/cookie-parser", + "@types/csurf", + "@types/debounce", + "@types/ejs", + "@types/escape-html", + "@types/express", + "@types/express-session", + "@types/html", + "@types/ini", + "@types/jasmine", + "@types/jsdom", + "@types/mime-types", + "@types/multer", + "@types/safe-compare", + "@types/sanitize-html", + "@types/sax", + "@types/semver", + "@types/serve-favicon", + "@types/session-file-store", + "@types/stream-throttle", + "@types/tmp", + "@types/turndown", + "@types/ws", + "@types/xml2js" + ] }, "include": [ "./src/**/*.js",