All files / cli/src/utils version.js

83.33% Statements 10/12
50% Branches 2/4
100% Functions 1/1
83.33% Lines 10/12

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 32 33 34 35 36 37 38 39                    5x               25x 20x     5x   5x 5x     5x 5x 5x 5x              
/**
 * CLI Version Utility
 * Provides consistent version detection across all generators
 */
 
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { env } from 'node:process';
 
let cachedVersion = null;
 
/**
 * Get the current CLI package version
 * @returns {string} The CLI version string
 */
export function getCLIVersion() {
  // Return cached version if already loaded
  if (cachedVersion) {
    return cachedVersion;
  }
 
  try {
    // Try to get the path of this module to determine CLI root
    const __filename = fileURLToPath(import.meta.url);
    const __dirname = dirname(__filename);
 
    // Look for package.json in CLI directory
    const packagePath = join(__dirname, '..', '..', 'package.json');
    const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
    cachedVersion = packageJson.version;
    return cachedVersion;
  } catch {
    // Fallback to environment variable or default
    cachedVersion = env.COHERENT_CLI_VERSION || '1.0.0-beta.5';
    return cachedVersion;
  }
}