forked from CredenceOrg/Credence-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.ts
More file actions
51 lines (44 loc) · 1.32 KB
/
Copy pathversion.ts
File metadata and controls
51 lines (44 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { execSync } from 'node:child_process'
import { statSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
export interface VersionMetadata {
gitSha: string
buildTimestamp: string
nodeVersion: string
}
let cachedVersion: VersionMetadata | null = null
export function getVersionMetadata(): VersionMetadata {
if (cachedVersion) {
return cachedVersion
}
// 1. Resolve Git SHA
let gitSha = process.env.GIT_SHA || process.env.COMMIT_SHA
if (!gitSha && process.env.NODE_ENV !== 'production') {
try {
gitSha = execSync('git rev-parse HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim()
} catch {
gitSha = 'unknown'
}
}
gitSha = gitSha || 'unknown'
// 2. Resolve Build Timestamp
let buildTimestamp = process.env.BUILD_TIMESTAMP
if (!buildTimestamp) {
try {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const pkgPath = join(__dirname, '../../package.json')
const stat = statSync(pkgPath)
buildTimestamp = stat.mtime.toISOString()
} catch {
buildTimestamp = new Date(Date.now() - process.uptime() * 1000).toISOString()
}
}
cachedVersion = {
gitSha,
buildTimestamp,
nodeVersion: process.version,
}
return cachedVersion
}