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

@@ -186,20 +186,13 @@ cd nexus-timer
```
### Git Pre-Commit Hook
This project uses a Git pre-commit hook to automatically updates the build timestamp placeholder in `src/views/InfoView.vue` and increments the `CACHE_VERSION` in `public/service-worker.js` (it is the indicator for the installed PWA that the new version is available). This ensures that each commit intended for a build/deployment reflects the correct information.
**Setup Steps:**
1. **Ensure Node.js is installed.** The hook script is written in Node.js.
2. **Configure Git to use the local hooks directory:**
After cloning the repository, run the following command in your terminal from the project root to tell Git to use the hooks located in the `.githooks` directory:
```bash
git config core.hooksPath .githooks
```
This step needs to be done once per local clone of the repository.
3. **Ensure the hook script is executable:**
The script `scripts/git-hooks/pre-commit.js` is the source, and it's copied/symlinked to `.githooks/pre-commit`. If you manually copy or if permissions are lost, ensure the hook in `.githooks` is executable:
```bash
chmod +x .githooks/pre-commit
```
(The main script in `scripts/git-hooks/` should also be executable if you plan to run it manually for testing: `chmod +x scripts/git-hooks/pre-commit.js`)
#### Configure Git to use the local hooks directory
After cloning the repository, run the following command in your terminal from the project root to tell Git to use the hooks located in the `.githooks` directory:
```bash
git config core.hooksPath .githooks
```
This step needs to be done once per local clone of the repository. The script `scripts/git-hooks/pre-commit.cjs` will be executed before every commit.
### Modify the app
Make code changes...

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);

View File

@@ -60,7 +60,7 @@ const store = useStore();
// and replace it with the actual build timestamp.
// For display, we'll use a ref.
const buildTime = ref("2025-05-09 16:57:06");
// If your hook replaces the entire line `const buildTime = ref("2025-05-09 17:03:37");`
// If your hook replaces the entire line `const buildTime = ref("2025-05-09 17:10:59");`
// with `const buildTime = ref("YYYY-MM-DD HH:MM:SS");`, that's also fine.
// Or, if it replaces only the string content: `const buildTime = ref("Actual Build Time");`