const { execSync } = require('child_process'); function getGitVersion() { try { try { execSync('git fetch --tags', { stdio: 'ignore' }); } catch (e) {} const raw = execSync('git describe --tags --always', { stdio: ['pipe', 'pipe', 'ignore'] }) .toString() .trim(); // Check if it's just a commit hash (no dashes, just hex) if (/^[0-9a-f]{7,}$/.test(raw)) { return `v0.0.0-${raw}`; } // It might be tag-commits-hash, e.g., v0.1.1-5-g4a9f6b6 // Let's shorten it to v0.1.1-4a9f6b6 for a cleaner look, or just return as-is const match = raw.match(/^(.*)-\d+-g([0-9a-f]+)$/); if (match) { return `${match[1]}-${match[2]}`; } return raw; } catch (e) { return 'dev'; } } console.log(getGitVersion());