Skip to main content

PackageInfo

Upgather SDK


Upgather SDK / PackageInfo

Class: PackageInfo

Defined in: lib/utils/PackageInfo.ts:19

Utility for accessing package information with graceful fallbacks.

Remarks

This utility provides safe access to package.json information with robust error handling. Version information is cached for performance and falls back to a default value if package.json cannot be accessed.

Example

import { PackageInfo } from '@upgather/upgather-sdk';

const version = PackageInfo.getVersion();
console.log(version); // "0.2.2" or "0.0.0-unknown" if package.json is not accessible

Constructors

new PackageInfo()

new PackageInfo(): PackageInfo

Returns

PackageInfo

Methods

getVersion()

static getVersion(): string

Defined in: lib/utils/PackageInfo.ts:46

Gets the package version with graceful fallback handling.

Returns

string

The package version string, or "0.0.0-unknown" as a fallback

Remarks

This method caches the version after the first successful read for performance. If package.json cannot be loaded or doesn't contain a version field, it returns "0.0.0-unknown" as a safe fallback.

The version detection follows this priority:

  1. Cached version (if previously loaded)
  2. Version from package.json via packageLoader
  3. Fallback to "0.0.0-unknown"

Example

const version = PackageInfo.getVersion();
// Returns something like "0.2.2" or "0.0.0-unknown"

resetCache()

static resetCache(): void

Defined in: lib/utils/PackageInfo.ts:78

Resets the cached version information.

Returns

void

Remarks

This method is primarily useful for testing scenarios where you need to clear the cache and force a fresh version lookup.

Example

PackageInfo.resetCache();
const freshVersion = PackageInfo.getVersion(); // Will re-read from package.json

restoreDefaultLoader()

static restoreDefaultLoader(): void

Defined in: lib/utils/PackageInfo.ts:123

Restores the default package loader that reads from ../../package.json.

Returns

void

Remarks

This method resets the package loader to its default behavior and clears the cache. Primarily useful in testing scenarios to restore normal behavior after using a custom loader.

Example

// After testing with custom loader, restore default behavior
PackageInfo.restoreDefaultLoader();
const version = PackageInfo.getVersion(); // Reads from actual package.json

setPackageLoader()

static setPackageLoader(loader): void

Defined in: lib/utils/PackageInfo.ts:101

Sets a custom package loader function.

Parameters

loader

() => any

Function that returns package.json data or throws an error

Returns

void

Remarks

This method is primarily intended for testing scenarios where you need to mock the package.json loading behavior. When called, it also resets the cache to ensure the new loader is used on the next version request.

Example

// For testing - mock a package.json with specific version
PackageInfo.setPackageLoader(() => ({ version: '1.0.0-test' }));
const version = PackageInfo.getVersion(); // Returns "1.0.0-test"