/ src / version.ts
version.ts
 1  /**
 2   * Single source of truth for package version.
 3   */
 4  
 5  import * as fs from 'node:fs';
 6  import * as path from 'node:path';
 7  import { fileURLToPath } from 'node:url';
 8  
 9  const __dirname = path.dirname(fileURLToPath(import.meta.url));
10  
11  // Dev: __dirname is src/ (one level to root).
12  // Prod: __dirname is dist/src/ (two levels to root).
13  let _pkgDir = path.resolve(__dirname, '..');
14  if (!fs.existsSync(path.join(_pkgDir, 'package.json'))) {
15    _pkgDir = path.resolve(_pkgDir, '..');
16  }
17  const pkgJsonPath = path.join(_pkgDir, 'package.json');
18  
19  export const PKG_VERSION: string = (() => {
20    try {
21      return JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')).version;
22    } catch {
23      return '0.0.0';
24    }
25  })();