Files
jackwener__opencli/src/version.ts
AstroHan 20957cbc7b fix: resolve version 0.0.0 and user CLI load failures (#788) (#789)
Bug 1: version.ts used a single-level parent lookup for package.json,
which broke after #784 changed rootDir from "src" to "." (version.js
now lives in dist/src/ instead of dist/).  Walk up until package.json
is found — works in both dev (src/) and prod (dist/src/).

Bug 2: adapters copied to ~/.opencli/clis/ import ../../src/registry.js
etc., which resolves to ~/.opencli/src/.  Derive src/ compat shims from
the existing rootShims list so these imports resolve correctly.
2026-04-05 14:54:30 +08:00

26 lines
698 B
TypeScript

/**
* Single source of truth for package version.
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Dev: __dirname is src/ (one level to root).
// Prod: __dirname is dist/src/ (two levels to root).
let _pkgDir = path.resolve(__dirname, '..');
if (!fs.existsSync(path.join(_pkgDir, 'package.json'))) {
_pkgDir = path.resolve(_pkgDir, '..');
}
const pkgJsonPath = path.join(_pkgDir, 'package.json');
export const PKG_VERSION: string = (() => {
try {
return JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')).version;
} catch {
return '0.0.0';
}
})();