All files / cli/src/utils version.js

93.75% Statements 15/16
84.61% Branches 11/13
100% Functions 2/2
93.75% Lines 15/16

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                    6x             31x 25x     6x 6x 6x       6x 18x 18x 18x 6x 6x         12x           31x                             30x    
/**
 * 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() {
  if (cachedVersion) {
    return cachedVersion;
  }
 
  try {
    const __filename = fileURLToPath(import.meta.url);
    let dir = dirname(__filename);
    // Walk up looking for the @coherent.js/cli package.json. Works from both
    // src/utils (depth 2) and dist/index.js (depth 1), and survives future
    // bundling moves.
    while (dir && dir !== dirname(dir)) {
      try {
        const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf-8'));
        if (pkg.name === '@coherent.js/cli' && typeof pkg.version === 'string') {
          cachedVersion = pkg.version;
          return cachedVersion;
        }
      } catch {
        // not this dir; keep walking
      }
      dir = dirname(dir);
    }
  } catch {
    // fall through to env/default
  }
  cachedVersion = env.COHERENT_CLI_VERSION || '1.0.0-rc.6';
  return cachedVersion;
}
 
/**
 * Build the dependency range scaffolded projects should depend on.
 *
 * A caret on a prerelease (^1.0.0-rc.6) also satisfies the eventual stable
 * 1.0.0 and every later 1.x, so a project scaffolded against an RC would
 * silently jump off the line it was generated for. Pin prereleases exactly
 * and keep the caret for stable releases.
 *
 * @param {string} [version] - Version to build a range for
 * @returns {string} A semver range
 */
export function getDependencyRange(version = getCLIVersion()) {
  return version.includes('-') ? version : `^${version}`;
}