All files / scripts/shared version.ts

92.3% Statements 12/13
75% Branches 3/4
100% Functions 1/1
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                19x 19x 19x   19x 19x     19x 19x 19x 19x 19x 19x               19x  
/**
 * @module shared/version
 * @description Centralised package version loader with safe fallback.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
 
function loadPkgVersion(): string {
  try {
    const pkgJsonPath = join(__dirname, '..', '..', 'package.json');
    const pkgRaw = readFileSync(pkgJsonPath, 'utf-8');
    const pkg = JSON.parse(pkgRaw) as { version?: string };
    Eif (typeof pkg.version === 'string' && pkg.version.trim() !== '') {
      return pkg.version;
    }
  } catch {
    // Fallback to a safe default if package.json cannot be read or parsed.
  }
  return '0.0.0';
}
 
export const PKG_VERSION: string = loadPkgVersion();