#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const projectRoot = path.resolve(__dirname, '../..'); const infoViewFile = path.join(projectRoot, 'src/views/InfoView.vue'); const serviceWorkerFile = path.join(projectRoot, 'public/service-worker.js'); console.log('Running pre-commit hook...'); // --- 1. Update Build Time in InfoView.vue --- try { let infoViewContent = fs.readFileSync(infoViewFile, 'utf8'); // Regex to find the line assigning to buildTime.value or ref("...") // It looks for ref("...") containing either __BUILD_TIME__ or a date-like string. // This regex captures the part inside ref("..."). const buildTimeAssignmentRegex = /const buildTime = ref\s*\(\s*["']([^"']*)["']\s*\);/; const matchBuildTime = infoViewContent.match(buildTimeAssignmentRegex); const now = new Date(); const newTimestamp = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`; if (matchBuildTime && matchBuildTime[0]) { // If the line `const buildTime = ref(...)` is found // Replace the entire matched line with the new timestamp const oldLine = matchBuildTime[0]; const newLine = `const buildTime = ref("${newTimestamp}");`; infoViewContent = infoViewContent.replace(oldLine, newLine); fs.writeFileSync(infoViewFile, infoViewContent, 'utf8'); console.log(`Updated build time in ${path.basename(infoViewFile)} to: ${newTimestamp}`); execSync(`git add "${infoViewFile}"`, { stdio: 'inherit' }); } else { console.warn(`Could not find the buildTime ref assignment line in ${path.basename(infoViewFile)}. Skipping build time update.`); // You might want to make this an error if the line should always exist after the first run. // For now, just a warning. } } catch (error) { console.error(`Error updating build time in ${infoViewFile}:`, error); process.exit(1); } // --- 2. Increment Service Worker Cache Version --- try { let swContent = fs.readFileSync(serviceWorkerFile, 'utf8'); const cacheVersionRegex = /const CACHE_VERSION = ['"](nexus-timer-cache-v)(\d+)['"];/; const matchCache = swContent.match(cacheVersionRegex); if (matchCache && matchCache[1] && matchCache[2]) { const prefix = matchCache[1]; const currentVersion = parseInt(matchCache[2], 10); const newVersion = currentVersion + 1; const newCacheVersionLine = `const CACHE_VERSION = '${prefix}${newVersion}';`; swContent = swContent.replace(cacheVersionRegex, newCacheVersionLine); fs.writeFileSync(serviceWorkerFile, swContent, 'utf8'); console.log(`Updated Service Worker cache version in ${path.basename(serviceWorkerFile)} to: v${newVersion}`); execSync(`git add "${serviceWorkerFile}"`, { stdio: 'inherit' }); } else { console.warn(`Could not find or parse CACHE_VERSION in ${path.basename(serviceWorkerFile)}. Skipping version increment.`); } } catch (error) { console.error(`Error updating Service Worker cache version in ${serviceWorkerFile}:`, error); process.exit(1); } console.log('Pre-commit hook finished successfully.'); process.exit(0);