Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 3, 2024
1 parent 081f8d2 commit 0a7e828
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/common/packageVersions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { createRequire } from "module";

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

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

export function isPackageInstalled(name: string): boolean {
try {
const _require = getRequire();
_require.resolve(name);
require.resolve(name);
return true;
} catch (error) {
return false;
const _require = createRequire(import.meta.url);
try {
_require.resolve(name);
return true;
} catch (error) {
return false;
}
}
}

0 comments on commit 0a7e828

Please sign in to comment.