import { execSync } from "child_process" ; /**
 * This file is created in the build step and is distributed through NPM
 * MUST be in sync with `-/gitDataPath.ts` and `package.json` files.
 */ import { readGitDataFile , GitData } from "./gitDataPath" ; /** Silent shell that won't pollute stdout, or stderr */ function shell ( cmd ) { return execSync ( cmd , { stdio : [ "ignore" , "pipe" , "ignore" ] } ) . toString ( ) . trim ( ) ; } /** Tries to get branch from git CLI. */ function getBranch ( ) { try { return shell ( "git rev-parse --abbrev-ref HEAD" ) ; } catch ( e ) { return undefined ; } } /** Tries to get commit from git from git CLI. */ function getCommit ( ) { try { return shell ( "git rev-parse --verify HEAD" ) ; } catch ( e ) { return undefined ; } } /** Tries to get the latest tag from git CLI. */ function getLatestTag ( ) { try { return shell ( "git describe --abbrev=0" ) ; } catch ( e ) { return undefined ; } } /** Gets number of commits since latest tag/release. */ function getCommitsSinceRelease ( ) { let numCommits = 0 ; const latestTag = getLatestTag ( ) ; try { numCommits = + shell ( `git rev-list ${ latestTag }..HEAD --count` ) ; } catch ( e ) { return undefined ; } return numCommits ; } /** Reads git data from a persisted file or local git data at build time. */ export function readLodestarGitData ( ) { try { const currentGitData = getGitData ( ) ; const persistedGitData = getPersistedGitData ( ) ; let gitData = { ... persistedGitData , ... currentGitData } ; if ( ! gitData . semver || ! gitData . branch || ! gitData . commit ) { gitData = persistedGitData ; } return { semver : gitData ?. semver || "N/A" , branch : gitData ?. branch || "N/A" , commit : gitData ?. commit || "N/A" , numCommits : gitData ?. numCommits || "" , } ; } catch ( e ) { return { semver : "" , branch : "" , commit : "" , numCommits : "" } ; } } /** Wrapper for updating git data. ONLY to be used with build scripts! */ export function forceUpdateGitData ( ) { return getGitData ( ) ; } /** Gets git data containing current branch and commit info from CLI. */ function getGitData ( ) { const numCommits = getCommitsSinceRelease ( ) ; let strCommits = "" ; if ( numCommits !== undefined && numCommits > 0 ) { strCommits = `+${ numCommits }` ; } return { branch : getBranch ( ) , commit : getCommit ( ) , semver : getLatestTag ( ) , numCommits : strCommits , } ; } /** Gets git data containing current branch and commit info from persistent file. */ function getPersistedGitData ( ) { try { return readGitDataFile ( ) ; } catch ( e ) { return { } ; } }