-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
16 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |