import { ExecOptionsWithStringEncoding, execSync } from 'child_process'; import { Log } from './types'; const options: ExecOptionsWithStringEncoding = { encoding: 'utf8', }; const exec = (cmd: string): string => execSync(cmd, options).trim(); /** * Returns latest branch name. */ const getCurrentBranch = (log: Log) => { try { return exec('git rev-parse --abbrev-ref HEAD'); } catch (e) { log.warn('Error getting branch.', e); return 'uknown branch'; } }; /** * Returns latest commit revision ID. */ const getCurrentRevision = (log: Log) => { try { return exec('git rev-parse HEAD'); } catch (e) { log.warn('Error getting revision ID.', e); return 'uknown revision ID'; } }; /** * Returns latest tags. */ const getCurrentTags = (log: Log) => { try { return exec('git describe --tags --abbrev=0'); } catch (e) { log.warn('No tags found.'); return ''; } }; export { getCurrentBranch, getCurrentRevision, getCurrentTags, };