From fe7158600ae2908284f4533602cc9c64b48b8859 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Thu, 28 Aug 2025 16:58:35 -0700 Subject: [PATCH] fix: implement automatic version detection from package.json - Replace hardcoded version string with dynamic reading from package.json - Add version caching for performance - Export getBrainyVersion function from main index - Ensures version stays automatically synchronized with releases --- src/index.ts | 3 +++ src/utils/version.ts | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 799999f1..501792ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,6 +50,9 @@ export { getStatistics } +// Export version utilities +export { getBrainyVersion } from './utils/version.js' + // Export embedding functionality import { UniversalSentenceEncoder, diff --git a/src/utils/version.ts b/src/utils/version.ts index 03ede225..25a16529 100644 --- a/src/utils/version.ts +++ b/src/utils/version.ts @@ -2,15 +2,32 @@ * Version utilities for Brainy */ -// Package version - this should be updated during the build process -const BRAINY_VERSION = '0.41.0' +import { readFileSync } from 'fs' +import { join, dirname } from 'path' +import { fileURLToPath } from 'url' + +// Get package.json path relative to this file +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) +const packageJsonPath = join(__dirname, '../../package.json') + +let cachedVersion: string | null = null /** * Get the current Brainy package version * @returns The current version string */ export function getBrainyVersion(): string { - return BRAINY_VERSION + if (!cachedVersion) { + try { + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) + cachedVersion = packageJson.version + } catch { + // Fallback version if package.json can't be read + cachedVersion = '2.7.1' + } + } + return cachedVersion! } /**