feat(deps): remove dependency on semver

This commit is contained in:
Elian Doran
2025-02-04 21:15:47 +02:00
parent d908c9044b
commit ce45309818
4 changed files with 48 additions and 15 deletions

View File

@@ -324,6 +324,48 @@ export function getResourceDir() {
}
}
// TODO: Deduplicate with src/public/app/services/utils.ts
/**
* Compares two semantic version strings.
* Returns:
* 1 if v1 is greater than v2
* 0 if v1 is equal to v2
* -1 if v1 is less than v2
*
* @param v1 First version string
* @param v2 Second version string
* @returns
*/
function compareVersions(v1: string, v2: string): number {
// Remove 'v' prefix and everything after dash if present
v1 = v1.replace(/^v/, "").split("-")[0];
v2 = v2.replace(/^v/, "").split("-")[0];
const v1parts = v1.split(".").map(Number);
const v2parts = v2.split(".").map(Number);
// Pad shorter version with zeros
while (v1parts.length < 3) v1parts.push(0);
while (v2parts.length < 3) v2parts.push(0);
// Compare major version
if (v1parts[0] !== v2parts[0]) {
return v1parts[0] > v2parts[0] ? 1 : -1;
}
// Compare minor version
if (v1parts[1] !== v2parts[1]) {
return v1parts[1] > v2parts[1] ? 1 : -1;
}
// Compare patch version
if (v1parts[2] !== v2parts[2]) {
return v1parts[2] > v2parts[2] ? 1 : -1;
}
return 0;
}
export default {
randomSecureToken,
randomString,
@@ -360,5 +402,6 @@ export default {
getResourceDir,
isMac,
isWindows,
envToBoolean
envToBoolean,
compareVersions
};

View File

@@ -12,7 +12,8 @@ import ws from "./services/ws.js";
import utils from "./services/utils.js";
import port from "./services/port.js";
import host from "./services/host.js";
import semver from "semver";
const MINIMUM_NODE_VERSION = "22.0.0";
// setup basic error handling even before requiring dependencies, since those can produce errors as well
@@ -32,8 +33,8 @@ function exit() {
process.on("SIGINT", exit);
process.on("SIGTERM", exit);
if (!semver.satisfies(process.version, ">=10.5.0")) {
console.error("Trilium only supports node.js 10.5 and later");
if (utils.compareVersions(process.version, MINIMUM_NODE_VERSION) < 0) {
console.error(`\nTrilium requires Node.js ${MINIMUM_NODE_VERSION} and later.\n`);
process.exit(1);
}