generated from darkobits/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactor
getPackageInfo
and report saffron version correctly.
- Loading branch information
Showing
4 changed files
with
60 additions
and
45 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
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,42 +1,48 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { dirname } from '@darkobits/fd-name'; | ||
import { readPackageUpSync, NormalizedPackageJson } from 'read-pkg-up'; | ||
|
||
|
||
/** | ||
* Object returned by getPackageInfo. | ||
* Object returned by `getPackageInfo`. | ||
*/ | ||
export interface PackageData { | ||
pkgJson: NormalizedPackageJson | undefined; | ||
pkgRoot: string | undefined; | ||
export interface PackageInfo { | ||
json: NormalizedPackageJson | undefined; | ||
root: string | undefined; | ||
} | ||
|
||
|
||
/** | ||
* Module-local cached result from read-pkg-up. | ||
* @private | ||
* | ||
* Module-local cache of package info lookups. | ||
*/ | ||
const cachedPackageResult: PackageData = { | ||
pkgJson: undefined, | ||
pkgRoot: undefined | ||
}; | ||
const packageCache = new Map<'host' | 'local', PackageInfo>(); | ||
|
||
|
||
/** | ||
* Loads the package.json of the host application, if one exists, and caches the | ||
* result. | ||
* Loads the package.json of the host or local package and returns the | ||
* normalized result and the package's root directory. Results are cached. | ||
*/ | ||
export default function getPackageInfo(): PackageData { | ||
if (Object.keys(cachedPackageResult).length === 0) { | ||
const execPath = path.dirname(fs.realpathSync(process.argv[1])); | ||
|
||
const packageResult = readPackageUpSync({ cwd: execPath }); | ||
|
||
if (packageResult) { | ||
cachedPackageResult.pkgJson = packageResult.packageJson; | ||
cachedPackageResult.pkgRoot = path.dirname(packageResult.path); | ||
} | ||
export function getPackageInfo(type: 'host' | 'local'): PackageInfo { | ||
// Cache miss; populate cache. | ||
if (!packageCache.has(type)) { | ||
const cwd = type === 'host' | ||
? path.dirname(fs.realpathSync(process.argv[1])) | ||
: dirname(); | ||
if (!cwd) throw new Error(`[getOurPackageInfo] Unable to compute cwd for the ${type} package.`); | ||
|
||
const packageResult = readPackageUpSync({ cwd }); | ||
if (!packageResult) throw new Error(`[getPackageInfo] Unable to get metadata for the ${type} package.`); | ||
|
||
packageCache.set(type, { | ||
json: packageResult.packageJson, | ||
root: path.dirname(packageResult.path) | ||
}); | ||
} | ||
|
||
return cachedPackageResult; | ||
// Return from cache. | ||
return packageCache.get(type) as PackageInfo; | ||
} |