Aethel Bot OSS repository!
aethel.xyz
bot
fun
ai
discord
discord-bot
aethel
1import { exec } from 'child_process';
2import { promisify } from 'util';
3import logger from './logger';
4
5const execAsync = promisify(exec);
6
7let cachedCommitHash: string | null = null;
8let isInitialized = false;
9
10function getGitCommitHashSync(): string {
11 if (process.env.SOURCE_COMMIT) {
12 return process.env.SOURCE_COMMIT.substring(0, 7);
13 }
14
15 return process.env.NODE_ENV === 'production' ? 'production' : 'development';
16}
17
18async function initializeGitCommitHash(): Promise<void> {
19 if (isInitialized) return;
20
21 try {
22 if (process.env.SOURCE_COMMIT) {
23 cachedCommitHash = process.env.SOURCE_COMMIT.substring(0, 7);
24 } else {
25 const { stdout } = await execAsync('git rev-parse --short HEAD');
26 cachedCommitHash = stdout.trim();
27 }
28
29 logger.debug('Git commit hash initialized', { hash: cachedCommitHash });
30 } catch (error) {
31 logger.warn('Failed to get git commit hash, using fallback', { error });
32 cachedCommitHash = process.env.NODE_ENV === 'production' ? 'production' : 'development';
33 } finally {
34 isInitialized = true;
35 }
36}
37
38function getGitCommitHash(): string {
39 return cachedCommitHash || getGitCommitHashSync();
40}
41
42initializeGitCommitHash().catch((error) => {
43 console.warn('Failed to initialize git commit hash:', error.message);
44});
45
46export default getGitCommitHash;