Skip to content

Commit

Permalink
server-esm: Fix use of __dirname
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Jul 18, 2024
1 parent 27c296f commit b6c5880
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 14 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion spec/etapi/import.spec.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
14 changes: 9 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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()) {
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/etapi/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/assets.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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')));
Expand Down
3 changes: 2 additions & 1 deletion src/services/resource_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "../..");
Expand Down
4 changes: 3 additions & 1 deletion src/services/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -23,7 +25,7 @@ const getIconPath = () => {
const iconSize = getIconSize();

return path.join(
__dirname,
path.dirname(fileURLToPath(import.meta.url)),
"../..",
"images",
"app-icons",
Expand Down
5 changes: 4 additions & 1 deletion src/services/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
32 changes: 31 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b6c5880

Please sign in to comment.