version.ts 635 B

123456789101112131415161718192021222324
  1. import fs from "fs"
  2. import path from "path"
  3. import { fileURLToPath } from "url"
  4. // Walk up from the current file to find the nearest package.json.
  5. // This works whether running from source (tsx src/lib/utils/) or bundle (dist/).
  6. function findVersion(): string {
  7. let dir = path.dirname(fileURLToPath(import.meta.url))
  8. while (dir !== path.dirname(dir)) {
  9. const candidate = path.join(dir, "package.json")
  10. if (fs.existsSync(candidate)) {
  11. const packageJson = JSON.parse(fs.readFileSync(candidate, "utf-8"))
  12. return packageJson.version
  13. }
  14. dir = path.dirname(dir)
  15. }
  16. return "0.0.0"
  17. }
  18. export const VERSION = findVersion()