import { resolve } from "node:path";

import { validateVersion } from "../../scripts/bump-version-logic";

const PACKAGE_JSON_PATH = resolve(import.meta.dir, "..", "..", "package.json");

export async function readAppVersion(): Promise<string> {
  const packageJson = (await Bun.file(PACKAGE_JSON_PATH).json()) as { version: string };
  const version = packageJson.version;

  if (typeof version !== "string") {
    throw new Error("package.json does not have a valid version field");
  }

  validateVersion(version);
  return version;
}
