#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const projectRoot = path.resolve(__dirname, '../..'); // Adjust if script is deeper 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'); const buildTimePlaceholder = '__BUILD_TIME__'; // Using a more robust regex to find the placeholder within the ref assignment const buildTimeRegex = /const buildTime = ref\s*\(\s*["']__BUILD_TIME__["']\s*\);/; if (infoViewContent.includes(buildTimePlaceholder)) { // Check if placeholder exists const now = new Date(); const timestamp = `${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')}`; // Replace the placeholder string within the ref() call infoViewContent = infoViewContent.replace( `ref("${buildTimePlaceholder}")`, `ref("${timestamp}")` ); // Or, if your placeholder is part of a more complex line: // infoViewContent = infoViewContent.replace( // buildTimeRegex, // `const buildTime = ref("${timestamp}");` // ); fs.writeFileSync(infoViewFile, infoViewContent, 'utf8'); console.log(`Updated build time in ${path.basename(infoViewFile)} to: ${timestamp}`); execSync(`git add "${infoViewFile}"`, { stdio: 'inherit' }); } else { console.log(`Placeholder ${buildTimePlaceholder} not found in ${path.basename(infoViewFile)}. Skipping build time update.`); } } catch (error) { console.error(`Error updating build time in ${infoViewFile}:`, error); process.exit(1); // Exit with error } // --- 2. Increment Service Worker Cache Version --- try { let swContent = fs.readFileSync(serviceWorkerFile, 'utf8'); const cacheVersionRegex = /const CACHE_VERSION = ['"](nexus-timer-cache-v)(\d+)['"];/; const match = swContent.match(cacheVersionRegex); if (match && match[1] && match[2]) { const prefix = match[1]; const currentVersion = parseInt(match[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); // Exit with error } console.log('Pre-commit hook finished successfully.'); process.exit(0); // Exit successfully