|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { execSync } = require('child_process'); |
| 6 | + |
| 7 | +function readEnv(name) { |
| 8 | + const value = process.env[name]; |
| 9 | + return value && value.trim() ? value.trim() : undefined; |
| 10 | +} |
| 11 | + |
| 12 | +function tryGitCommand(args, options) { |
| 13 | + try { |
| 14 | + return execSync(`git ${args}`, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], ...options }).trim(); |
| 15 | + } catch (error) { |
| 16 | + return undefined; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function getGitInfoForPath(targetPath, repoRoot) { |
| 21 | + const options = repoRoot ? { cwd: repoRoot } : undefined; |
| 22 | + const commit = tryGitCommand(`log -1 --format=%H -- ${targetPath}`, options); |
| 23 | + const timestamp = tryGitCommand(`log -1 --format=%cI -- ${targetPath}`, options); |
| 24 | + return { commit, timestamp }; |
| 25 | +} |
| 26 | + |
| 27 | +function selectValue(primary, fallback, defaultValue) { |
| 28 | + return primary ?? fallback ?? defaultValue; |
| 29 | +} |
| 30 | + |
| 31 | +function buildInfo() { |
| 32 | + const scriptDir = __dirname; |
| 33 | + const frontendRoot = path.resolve(scriptDir, '..'); |
| 34 | + const repoRoot = tryDetermineRepoRoot(frontendRoot); |
| 35 | + |
| 36 | + const frontendGit = repoRoot ? getGitInfoForPath('Frontend', repoRoot) : { commit: undefined, timestamp: undefined }; |
| 37 | + const backendGit = repoRoot ? getGitInfoForPath('Backend', repoRoot) : { commit: undefined, timestamp: undefined }; |
| 38 | + |
| 39 | + const frontendTimestamp = selectValue( |
| 40 | + readEnv('FRONTEND_BUILD_TIME'), |
| 41 | + frontendGit.timestamp, |
| 42 | + new Date().toISOString() |
| 43 | + ); |
| 44 | + |
| 45 | + const backendTimestamp = selectValue( |
| 46 | + readEnv('BACKEND_BUILD_TIME'), |
| 47 | + backendGit.timestamp, |
| 48 | + undefined |
| 49 | + ); |
| 50 | + |
| 51 | + const info = { |
| 52 | + generatedAt: new Date().toISOString(), |
| 53 | + frontend: { |
| 54 | + buildTimestamp: frontendTimestamp, |
| 55 | + commit: selectValue(readEnv('FRONTEND_COMMIT'), frontendGit.commit, undefined) |
| 56 | + }, |
| 57 | + backend: { |
| 58 | + buildTimestamp: backendTimestamp, |
| 59 | + commit: selectValue(readEnv('BACKEND_COMMIT'), backendGit.commit, undefined) |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + const outputPath = path.resolve(frontendRoot, 'src', 'app', 'build-info.generated.ts'); |
| 64 | + const fileContent = `// This file is auto-generated by scripts/generate-build-info.js\n` + |
| 65 | + `// Do not edit manually.\n` + |
| 66 | + `export interface BuildInfoMeta {\n` + |
| 67 | + ` generatedAt: string;\n` + |
| 68 | + ` frontend: BuildInfoEntry;\n` + |
| 69 | + ` backend: BuildInfoEntry;\n` + |
| 70 | + `}\n` + |
| 71 | + `export interface BuildInfoEntry {\n` + |
| 72 | + ` buildTimestamp?: string;\n` + |
| 73 | + ` commit?: string;\n` + |
| 74 | + `}\n` + |
| 75 | + `export const BUILD_INFO: BuildInfoMeta = ${JSON.stringify(info, null, 2)};\n`; |
| 76 | + |
| 77 | + fs.writeFileSync(outputPath, fileContent); |
| 78 | +} |
| 79 | + |
| 80 | +function tryDetermineRepoRoot(frontendRoot) { |
| 81 | + let current = frontendRoot; |
| 82 | + for (let i = 0; i < 5; i++) { |
| 83 | + const potentialGit = path.join(current, '.git'); |
| 84 | + if (fs.existsSync(potentialGit)) { |
| 85 | + return current; |
| 86 | + } |
| 87 | + const parent = path.dirname(current); |
| 88 | + if (parent === current) { |
| 89 | + break; |
| 90 | + } |
| 91 | + current = parent; |
| 92 | + } |
| 93 | + return undefined; |
| 94 | +} |
| 95 | + |
| 96 | +buildInfo(); |
0 commit comments