File size: 1,370 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Version utility - Reads version from package.json
 */

import { readFileSync, existsSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { createLogger } from '@automaker/utils';

const logger = createLogger('Version');

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

let cachedVersion: string | null = null;

/**
 * Get the version from package.json
 * Caches the result for performance
 */
export function getVersion(): string {
  if (cachedVersion) {
    return cachedVersion;
  }

  try {
    const candidatePaths = [
      // Development via tsx: src/lib -> project root
      join(__dirname, '..', '..', 'package.json'),
      // Packaged/build output: lib -> server bundle root
      join(__dirname, '..', 'package.json'),
    ];

    const packageJsonPath = candidatePaths.find((candidate) => existsSync(candidate));
    if (!packageJsonPath) {
      throw new Error(
        `package.json not found in any expected location: ${candidatePaths.join(', ')}`
      );
    }

    const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
    const version = packageJson.version || '0.0.0';
    cachedVersion = version;
    return version;
  } catch (error) {
    logger.warn('Failed to read version from package.json:', error);
    return '0.0.0';
  }
}