apiveritas
Version:
Lightweight CLI tool for consumer-driven API contract testing via JSON schema and payload comparisons.
77 lines (76 loc) • 2.4 kB
JavaScript
;
/**
* @file PackageInfo.
* @author Mario Galea
* @description
* Singleton class for reading and exposing metadata from the root `package.json` file.
* This is useful for logging the tool name, version, or description in CLI output, reports, etc.
* The shape of the `package.json` is defined by the shared `IPackageJson` interface.
*
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PackageInfo = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Singleton class that provides access to metadata from the root `package.json` file.
*/
class PackageInfo {
/**
* Private constructor ensures only one instance is created.
* Loads and parses the root `package.json` at module load time.
*/
constructor() {
const packageJsonPath = path_1.default.resolve(__dirname, '../../../package.json');
// Read and parse package.json synchronously (expected at startup)
const rawData = fs_1.default.readFileSync(packageJsonPath, 'utf-8');
this.data = JSON.parse(rawData);
}
/**
* Returns the singleton instance of `PackageInfo`.
*
* @returns {PackageInfo} The single, shared instance.
*/
static getInstance() {
if (!PackageInfo.instance) {
PackageInfo.instance = new PackageInfo();
}
return PackageInfo.instance;
}
/**
* Gets the package name (from `name` field in package.json).
*
* @returns {string} The package name.
*/
getName() {
return this.data.name;
}
/**
* Gets the package version (from `version` field in package.json).
*
* @returns {string} The version string (e.g., "1.0.0").
*/
getVersion() {
return this.data.version;
}
/**
* Gets the optional package description.
*
* @returns {string | undefined} The description text if available.
*/
getDescription() {
return this.data.description;
}
/**
* Gets the raw parsed content of `package.json`.
*
* @returns {IPackageJson} The full package.json object.
*/
getRaw() {
return this.data;
}
}
exports.PackageInfo = PackageInfo;