1616
1717import { execSync } from 'node:child_process'
1818import type { AstroIntegration } from 'astro'
19- import { BuildError } from '../../lib/errors/BuildError'
19+
20+ const PRIVACY_POLICY_PATH = 'src/pages/privacy/index.astro'
21+
22+ export const toIsoDateString = ( date : Date ) : string => date . toISOString ( ) . slice ( 0 , 10 )
2023
2124/**
2225 * Get privacy policy version from git commit date
2326 * @param filePath - Path to privacy policy file (relative to project root)
2427 * @returns ISO date string (YYYY-MM-DD) of last commit
25- * @throws {BuildError } If git command fails or returns empty result
2628 */
27- function getPrivacyPolicyVersionFromGit ( filePath : string ) : string {
29+ function getPrivacyPolicyVersionFromGit ( filePath : string ) : string | null {
2830 try {
2931 // Get last commit date for privacy policy file in YYYY-MM-DD format
3032 const lastCommitDate = execSync (
@@ -36,21 +38,37 @@ function getPrivacyPolicyVersionFromGit(filePath: string): string {
3638 return lastCommitDate
3739 }
3840
39- // If no commits found (new file), throw error
40- throw new BuildError (
41- `No git commits found for privacy policy file: ${ filePath } ` ,
42- { phase : 'config-setup' , filePath } ,
41+ console . warn (
42+ `[privacy-policy-version] No git commits found for privacy policy file: ${ filePath } . Falling back to current date.` ,
4343 )
44+ return null
4445 } catch ( error ) {
45- // Git not available or command failed
46- if ( error instanceof BuildError ) {
47- throw error
48- }
49- throw new BuildError (
50- `Could not get privacy policy version from git: ${ error instanceof Error ? error . message : String ( error ) } ` ,
51- { phase : 'config-setup' , tool : 'git' , cause : error } ,
46+ console . warn (
47+ `[privacy-policy-version] Could not get privacy policy version from git: ${ error instanceof Error ? error . message : String ( error ) } ` ,
5248 )
49+ return null
50+ }
51+ }
52+
53+ /**
54+ * Resolve privacy policy version using env, git metadata, or current date fallback.
55+ */
56+ export function resolvePrivacyPolicyVersion ( ) : string {
57+ const envVersion = process . env . PRIVACY_POLICY_VERSION ?. trim ( )
58+ if ( envVersion ) {
59+ console . log ( `✅ Privacy policy version sourced from env: ${ envVersion } ` )
60+ return envVersion
61+ }
62+
63+ const gitVersion = getPrivacyPolicyVersionFromGit ( PRIVACY_POLICY_PATH )
64+ if ( gitVersion ) {
65+ console . log ( `✅ Privacy policy version set from git: ${ gitVersion } ` )
66+ return gitVersion
5367 }
68+
69+ const fallback = toIsoDateString ( new Date ( ) )
70+ console . log ( `⚠️ Privacy policy version fallback applied: ${ fallback } ` )
71+ return fallback
5472}
5573
5674/**
@@ -61,11 +79,7 @@ export function privacyPolicyVersion(): AstroIntegration {
6179 name : 'privacy-policy-version' ,
6280 hooks : {
6381 'astro:config:setup' : async ( { updateConfig } ) => {
64- // Get version from git commit date
65- const privacyPolicyPath = 'src/pages/privacy/index.astro'
66- const version = getPrivacyPolicyVersionFromGit ( privacyPolicyPath )
67-
68- console . log ( `✅ Privacy policy version set to: ${ version } ` )
82+ const version = resolvePrivacyPolicyVersion ( )
6983
7084 // Inject as Vite define so it's available as import.meta.env.PRIVACY_POLICY_VERSION
7185 updateConfig ( {
0 commit comments