This commit is contained in:
cpu
2025-05-09 17:10:59 +02:00
parent e6f7c87ea4
commit 0e409c403d
3 changed files with 34 additions and 41 deletions

View File

@@ -4,7 +4,7 @@ const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const projectRoot = path.resolve(__dirname, '../..'); // Adjust if script is deeper
const projectRoot = path.resolve(__dirname, '../..');
const infoViewFile = path.join(projectRoot, 'src/views/InfoView.vue');
const serviceWorkerFile = path.join(projectRoot, 'public/service-worker.js');
@@ -13,45 +13,45 @@ 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')}`;
// 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}");`;
// 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}");`
// );
infoViewContent = infoViewContent.replace(oldLine, newLine);
fs.writeFileSync(infoViewFile, infoViewContent, 'utf8');
console.log(`Updated build time in ${path.basename(infoViewFile)} to: ${timestamp}`);
console.log(`Updated build time in ${path.basename(infoViewFile)} to: ${newTimestamp}`);
execSync(`git add "${infoViewFile}"`, { stdio: 'inherit' });
} else {
console.log(`Placeholder ${buildTimePlaceholder} not found in ${path.basename(infoViewFile)}. Skipping build time update.`);
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); // Exit with 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 match = swContent.match(cacheVersionRegex);
const matchCache = swContent.match(cacheVersionRegex);
if (match && match[1] && match[2]) {
const prefix = match[1];
const currentVersion = parseInt(match[2], 10);
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}';`;
@@ -64,8 +64,8 @@ try {
}
} catch (error) {
console.error(`Error updating Service Worker cache version in ${serviceWorkerFile}:`, error);
process.exit(1); // Exit with error
process.exit(1);
}
console.log('Pre-commit hook finished successfully.');
process.exit(0); // Exit successfully
process.exit(0);