@gohcltech/bitbucket-mcp
Version:
Bitbucket integration for Claude via Model Context Protocol
117 lines • 4.16 kB
JavaScript
/**
* Dynamic version management utilities for the Bitbucket MCP server.
*
* Provides functions to dynamically load version information from package.json
* at runtime, ensuring a single source of truth for version management.
*
* @module version
*/
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
// Get current directory in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Cached version to avoid repeated file reads.
* Once loaded, the version is cached for the lifetime of the process.
*/
let cachedVersion = null;
/**
* Dynamically loads version from package.json.
*
* This function reads the package.json file from the project root and extracts
* the version field. It includes comprehensive error handling and caches the
* result to avoid repeated file system operations.
*
* @returns The version string from package.json or 'unknown' on error
*
* @example
* ```typescript
* const version = getVersion();
* console.log(`Server version: ${version}`); // "Server version: 1.0.0"
* ```
*
* @throws Does not throw - returns 'unknown' on any error
*/
export function getVersion() {
// Return cached version if available
if (cachedVersion !== null) {
return cachedVersion;
}
try {
// Construct path to package.json (go up one level from src/)
const packageJsonPath = join(__dirname, '..', 'package.json');
// Read and parse package.json
const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);
// Extract and validate version
const version = packageJson.version;
if (!version || typeof version !== 'string') {
console.warn('Version field missing or invalid in package.json');
cachedVersion = 'unknown';
return cachedVersion;
}
// Basic semver validation (major.minor.patch format)
const semverPattern = /^\d+\.\d+\.\d+(?:-[\w\-.]+)?(?:\+[\w\-.]+)?$/;
if (!semverPattern.test(version)) {
console.warn(`Invalid semver format in package.json: ${version}`);
cachedVersion = 'unknown';
return cachedVersion;
}
// Cache and return valid version
cachedVersion = version;
return cachedVersion;
}
catch (error) {
console.warn('Could not read version from package.json:', error instanceof Error ? error.message : error);
cachedVersion = 'unknown';
return cachedVersion;
}
}
/**
* Gets full package information including name, version, and description.
*
* This function provides access to additional package.json fields beyond
* just the version, useful for detailed server information reporting.
*
* @returns Object containing package name, version, and description
*
* @example
* ```typescript
* const info = getPackageInfo();
* console.log(`${info.name} v${info.version} - ${info.description}`);
* ```
*/
export function getPackageInfo() {
try {
const packageJsonPath = join(__dirname, '..', 'package.json');
const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);
return {
name: packageJson.name || 'unknown',
version: packageJson.version || 'unknown',
description: packageJson.description || 'No description available',
};
}
catch (error) {
console.warn('Could not read package info from package.json:', error instanceof Error ? error.message : error);
return {
name: 'unknown',
version: 'unknown',
description: 'No description available',
};
}
}
/**
* Clears the cached version, forcing a fresh read on next getVersion() call.
*
* This function is primarily useful for testing scenarios where the
* package.json might be modified during runtime.
*
* @internal
*/
export function clearVersionCache() {
cachedVersion = null;
}
//# sourceMappingURL=version.js.map