Skip to content

Commit

Permalink
Fix detection of installed package versions
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 3, 2024
1 parent b584929 commit 081f8d2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-var-requires": "off"
},
"env": {
"node": true
Expand Down
20 changes: 19 additions & 1 deletion src/common/packageVersions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { createRequire } from "module";

function getRequire() {
if (typeof require !== "undefined") {
return require;
} else {
return createRequire(import.meta.url);
}

Check warning on line 8 in src/common/packageVersions.ts

View check run for this annotation

Codecov / codecov/patch

src/common/packageVersions.ts#L7-L8

Added lines #L7 - L8 were not covered by tests
}

export function getPackageVersion(name: string): string | null {
try {
const _require = createRequire(import.meta.url);
const _require = getRequire();
return _require(`${name}/package.json`).version || null;
} catch (error) {
return null;
}
}

export function isPackageInstalled(name: string): boolean {
try {
const _require = getRequire();
_require.resolve(name);
return true;
} catch (error) {
return false;
}
}
17 changes: 12 additions & 5 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { performance } from "perf_hooks";

import { ApitallyClient } from "../common/client.js";
import { consumerFromStringOrObject } from "../common/consumerRegistry.js";
import { getPackageVersion } from "../common/packageVersions.js";
import {
getPackageVersion,
isPackageInstalled,
} from "../common/packageVersions.js";
import {
ApitallyConfig,
ApitallyConsumer,
Expand Down Expand Up @@ -32,10 +35,10 @@ export const useApitally = (
};

const getMiddleware = (app: Express | Router, client: ApitallyClient) => {
const validatorInstalled = getPackageVersion("express-validator") !== null;
const celebrateInstalled = getPackageVersion("celebrate") !== null;
const nestInstalled = getPackageVersion("@nestjs/core") !== null;
const classValidatorInstalled = getPackageVersion("class-validator") !== null;
const validatorInstalled = isPackageInstalled("express-validator");
const celebrateInstalled = isPackageInstalled("celebrate");
const nestInstalled = isPackageInstalled("@nestjs/core");
const classValidatorInstalled = isPackageInstalled("class-validator");
let errorHandlerConfigured = false;

return (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -256,10 +259,14 @@ const getAppInfo = (
["nodejs", process.version.replace(/^v/, "")],
];
const expressVersion = getPackageVersion("express");
const nestjsVersion = getPackageVersion("@nestjs/core");
const apitallyVersion = getPackageVersion("../..");
if (expressVersion) {
versions.push(["express", expressVersion]);
}
if (nestjsVersion) {
versions.push(["nestjs", nestjsVersion]);
}
if (apitallyVersion) {
versions.push(["apitally", apitallyVersion]);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/common/packageVersions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";

import {
getPackageVersion,
isPackageInstalled,
} from "../../src/common/packageVersions.js";

describe("Package versions", () => {
it("Check if package is installed", async () => {
expect(isPackageInstalled("vitest")).toBe(true);
expect(isPackageInstalled("nonexistent")).toBe(false);
});

it("Get package version", () => {
expect(getPackageVersion("vitest")).not.toBeNull();
expect(getPackageVersion("nonexistent")).toBeNull();
});
});
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from "tsup";

export default defineConfig({
format: ["cjs", "esm"],
platform: "node",
dts: true,
sourcemap: true,
splitting: false,
Expand Down

0 comments on commit 081f8d2

Please sign in to comment.