From 981fe3a93963f70f8debafc5be3a97d452fa20d3 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 12 May 2024 10:11:51 +0300 Subject: [PATCH] #12: Move electron-specific route to electron project --- electron/package-lock.json | 89 +++++++++++++++++++++ electron/package.json | 1 + electron/src/electron.ts | 22 ++++- {server => electron}/src/routes/electron.ts | 0 server/src/www.ts | 22 +---- 5 files changed, 110 insertions(+), 24 deletions(-) rename {server => electron}/src/routes/electron.ts (100%) diff --git a/electron/package-lock.json b/electron/package-lock.json index 2f26b019f..b72247a1f 100644 --- a/electron/package-lock.json +++ b/electron/package-lock.json @@ -17,6 +17,7 @@ "electron-window-state": "5.0.3" }, "devDependencies": { + "@types/express": "^4.17.21", "cross-env": "7.0.3", "electron": "25.9.8", "electron-builder": "24.13.3", @@ -634,6 +635,16 @@ "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", "dev": true }, + "node_modules/@types/body-parser": { + "version": "1.19.5", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", + "integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, "node_modules/@types/cacheable-request": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.3.tgz", @@ -645,6 +656,15 @@ "@types/responselike": "^1.0.0" } }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/debug": { "version": "4.1.12", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz", @@ -654,6 +674,30 @@ "@types/ms": "*" } }, + "node_modules/@types/express": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", + "integrity": "sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.33", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.19.0", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.19.0.tgz", + "integrity": "sha512-bGyep3JqPCRry1wq+O5n7oiBgGWmeIJXPjXXCo8EK0u8duZGSYar7cGqd3ML2JUsLGeB7fmc06KYo9fLGWqPvQ==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, "node_modules/@types/fs-extra": { "version": "9.0.13", "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", @@ -678,6 +722,12 @@ "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz", "integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==" }, + "node_modules/@types/http-errors": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", + "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", + "dev": true + }, "node_modules/@types/keyv": { "version": "3.1.4", "resolved": "https://registry.npmjs.org/@types/keyv/-/keyv-3.1.4.tgz", @@ -686,6 +736,12 @@ "@types/node": "*" } }, + "node_modules/@types/mime": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", + "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", + "dev": true + }, "node_modules/@types/minimatch": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", @@ -717,6 +773,18 @@ "xmlbuilder": ">=11.0.1" } }, + "node_modules/@types/qs": { + "version": "6.9.15", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.15.tgz", + "integrity": "sha512-uXHQKES6DQKKCLh441Xv/dwxOq1TVS3JPUMlEqoEglvlhR6Mxnlew/Xq/LRVHpLyk7iK3zODe1qYHIMltO7XGg==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true + }, "node_modules/@types/responselike": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/@types/responselike/-/responselike-1.0.3.tgz", @@ -725,6 +793,27 @@ "@types/node": "*" } }, + "node_modules/@types/send": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz", + "integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "1.15.7", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.7.tgz", + "integrity": "sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==", + "dev": true, + "dependencies": { + "@types/http-errors": "*", + "@types/node": "*", + "@types/send": "*" + } + }, "node_modules/@types/verror": { "version": "1.10.10", "resolved": "https://registry.npmjs.org/@types/verror/-/verror-1.10.10.tgz", diff --git a/electron/package.json b/electron/package.json index 673d7553b..f75a3257e 100644 --- a/electron/package.json +++ b/electron/package.json @@ -20,6 +20,7 @@ "electron-window-state": "5.0.3" }, "devDependencies": { + "@types/express": "^4.17.21", "electron": "25.9.8", "electron-builder": "24.13.3", "electron-packager": "17.1.2", diff --git a/electron/src/electron.ts b/electron/src/electron.ts index f6592ab98..fcd17aca0 100644 --- a/electron/src/electron.ts +++ b/electron/src/electron.ts @@ -5,8 +5,8 @@ import sqlInit = require("../../server/src/services/sql_init"); import appIconService = require("./services/app_icon"); import windowService = require("./services/window"); import tray = require("./services/tray"); - -import startTrilium = require("../../server/src/www.js"); +import startTrilium = require("../../server/src/www"); +import electronRouting = require('./routes/electron'); // Adds debug features like hotkeys for triggering dev tools and reload require("electron-debug")(); @@ -62,4 +62,20 @@ electron.app.on("will-quit", () => { // this is to disable electron warning spam in the dev console (local development only) process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true"; -startTrilium(); \ No newline at end of file +/** + * The intended behavior is to detect when a second instance is running, in that case open the old instance + * instead of the new one. This is complicated by the fact that it is possible to run multiple instances of Trilium + * if port and data dir are configured separately. This complication is the source of the following weird usage. + * + * The line below makes sure that the "second-instance" (process in window.ts) is fired. Normally it returns a boolean + * indicating whether another instance is running or not, but we ignore that and kill the app only based on the port conflict. + * + * A bit weird is that "second-instance" is triggered also on the valid usecases (different port/data dir) and + * focuses the existing window. But the new process is start as well and will steal the focus too, it will win, because + * its startup is slower than focusing the existing process/window. So in the end, it works out without having + * to do a complex evaluation. + */ +electron.app.requestSingleInstanceLock(); + +const app = startTrilium(); +electronRouting(app); \ No newline at end of file diff --git a/server/src/routes/electron.ts b/electron/src/routes/electron.ts similarity index 100% rename from server/src/routes/electron.ts rename to electron/src/routes/electron.ts diff --git a/server/src/www.ts b/server/src/www.ts index 8ab3c6321..c9dd4f257 100644 --- a/server/src/www.ts +++ b/server/src/www.ts @@ -38,23 +38,6 @@ function startTrilium() { process.exit(1); } - /** - * The intended behavior is to detect when a second instance is running, in that case open the old instance - * instead of the new one. This is complicated by the fact that it is possible to run multiple instances of Trilium - * if port and data dir are configured separately. This complication is the source of the following weird usage. - * - * The line below makes sure that the "second-instance" (process in window.ts) is fired. Normally it returns a boolean - * indicating whether another instance is running or not, but we ignore that and kill the app only based on the port conflict. - * - * A bit weird is that "second-instance" is triggered also on the valid usecases (different port/data dir) and - * focuses the existing window. But the new process is start as well and will steal the focus too, it will win, because - * its startup is slower than focusing the existing process/window. So in the end, it works out without having - * to do a complex evaluation. - */ - if (utils.isElectron()) { - require("electron").app.requestSingleInstanceLock(); - } - log.info(JSON.stringify(appInfo, null, 2)); const cpuInfos = require('os').cpus(); @@ -66,10 +49,7 @@ function startTrilium() { ws.init(httpServer, sessionParser as any); // TODO: Not sure why session parser is incompatible. - if (utils.isElectron()) { - const electronRouting = require('./routes/electron'); - electronRouting(app); - } + return app; } function startHttpServer() {