-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exactCurrentVersion format option
- Loading branch information
1 parent
eae4692
commit 2196dee
Showing
9 changed files
with
81 additions
and
26 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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import { PackageFile } from '../types/PackageFile' | ||
import exists from './exists' | ||
|
||
/** Gets the package.json contents of an installed package. */ | ||
async function getPackageJson( | ||
packageName: string, | ||
{ | ||
pkgFile, | ||
}: { | ||
/** Specify the package file location to add to the node_modules search paths. Needed in workspaces/deep mode. */ | ||
pkgFile?: string | ||
} = {}, | ||
): Promise<PackageFile | null> { | ||
const requirePaths = require.resolve.paths(packageName) || [] | ||
const pkgFileNodeModules = pkgFile ? [path.join(path.dirname(pkgFile), 'node_modules')] : [] | ||
const localNodeModules = [path.join(process.cwd(), 'node_modules')] | ||
const nodeModulePaths = [...pkgFileNodeModules, ...localNodeModules, ...requirePaths] | ||
|
||
for (const basePath of nodeModulePaths) { | ||
const packageJsonPath = path.join(basePath, packageName, 'package.json') | ||
if (await exists(packageJsonPath)) { | ||
try { | ||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')) | ||
return packageJson | ||
} catch (e) {} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
export default getPackageJson |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PackageFile } from '../types/PackageFile' | ||
import getPackageJson from './getPackageJson' | ||
|
||
/** | ||
* @param packageName A package name as listed in package.json's dependencies list | ||
* @param packageJson Optional param to specify a object representation of a package.json file instead of loading from node_modules | ||
* @returns The package version or null if a version could not be determined | ||
*/ | ||
async function getPackageVersion( | ||
packageName: string, | ||
packageJson?: PackageFile, | ||
{ | ||
pkgFile, | ||
}: { | ||
/** Specify the package file location to add to the node_modules search paths. Needed in workspaces/deep mode. */ | ||
pkgFile?: string | ||
} = {}, | ||
) { | ||
if (packageJson) { | ||
return packageJson.version | ||
} | ||
|
||
const loadedPackageJson = await getPackageJson(packageName, { pkgFile }) | ||
return loadedPackageJson?.version ?? null | ||
} | ||
|
||
export default getPackageVersion |
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
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
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
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
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