import fs from "fs" ; import findUp from "find-up" ; import { readLodestarGitData } from "./gitData" ; import { GitData } from "./gitData/gitDataPath" ; type VersionJson = { /** "0.28.2" */ version : string ; } ; enum ReleaseTrack { git = "git" , npm = "npm" , nightly = "nightly" , alpha = "alpha" , beta = "beta" , rc = "release candidate" , stable = "stable" , lts = "long term support" } /** Defines default release track, i.e., the "stability" of tag releases */ const defaultReleaseTrack = ReleaseTrack . alpha ; returns export function getVersion ( ) { const gitData = readLodestarGitData ( ) ; const semver = gitData . semver ; const numCommits = gitData . numCommits ; const commitSlice = gitData . commit ?. slice ( 0 , 8 ) ; if ( ! semver || semver . includes ( "N/A" ) ) { return `v${ getLocalVersion ( ) } (${ ReleaseTrack . npm })` ; } if ( ! commitSlice || ! numCommits || numCommits === "" ) { return `${ semver } (${ defaultReleaseTrack })` ; } return `${ semver }/${ gitData . branch }/${ numCommits }/${ commitSlice } (${ ReleaseTrack . git })` ; } /** Exposes raw version data wherever needed for reporting (metrics, grafana). */ export function getVersionGitData ( ) { return readLodestarGitData ( ) ; } /** Returns local version from `lerna.json` or `package.json` as `"0.28.2"` */ function getLocalVersion ( ) { return readVersionFromLernaJson ( ) || readCliPackageJson ( ) ; } /** Read version information from lerna.json */ function readVersionFromLernaJson ( ) { const filePath = findUp . sync ( "lerna.json" , { cwd : __dirname } ) ; if ( ! filePath ) return undefined ; const lernaJson = JSON . parse ( fs . readFileSync ( filePath , "utf8" ) ) as VersionJson ; return lernaJson . version ; } /** Read version information from package.json */ function readCliPackageJson ( ) { const filePath = findUp . sync ( "package.json" , { cwd : __dirname } ) ; if ( ! filePath ) return undefined ; const packageJson = JSON . parse ( fs . readFileSync ( filePath , "utf8" ) ) as VersionJson ; return packageJson . version ; }