diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 120000 index 0000000..ade65c5 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1 @@ +../scripts/git-hooks/pre-commit.cjs \ No newline at end of file diff --git a/README.md b/README.md index 67b2dc2..d2d1797 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,30 @@ curl https://nexus-timer.virtonline.eu Or open it in your browser: [https://nexus-timer.virtonline.eu](https://nexus-timer.virtonline.eu) -### Update the app +## Developer Setup + +### Clone the repository +```bash +git clone https://gitea.virtonline.eu/2HoursProject/nexus-timer.git +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`) +### Modify the app Make code changes... Run the live update server locally @@ -188,11 +211,6 @@ npm run dev Open it in your browser: [http://localhost:8080/](http://localhost:8080/) -When done, do not forget to update the `CACHE_VERSION` in the `service-worker.js`. It is the indicator for the PWA that the new version is available. -```bash -ver=$(grep -oP "CACHE_VERSION = 'nexus-timer-cache-v\K[0-9]+" public/service-worker.js) -sed -i "s/nexus-timer-cache-v$ver/nexus-timer-cache-v$((ver+1))/" public/service-worker.js -``` Stage changes, commit and push ```bash git add . diff --git a/public/manifest.json b/public/manifest.json index c933fc7..50d98ea 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,7 +1,7 @@ { "name": "Nexus Timer - Multiplayer Turn Timer", "short_name": "NexusTimer", - "description": "Dynamic multiplayer timer for games, workshops, or sequential turns. Focuses on current & next player.", // Add a description + "description": "Dynamic multiplayer timer for games, workshops, or sequential turns. Focuses on current & next player.", "start_url": "/", "scope": "/", "display": "standalone", @@ -9,8 +9,6 @@ "background_color": "#1f2937", "theme_color": "#3b82f6", "orientation": "portrait", - "version": "0.0.1", - "version_name": "0.0.1", "lang": "en-US", "icons": [ { diff --git a/public/service-worker.js b/public/service-worker.js index cad4d60..d7885f0 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -1,4 +1,4 @@ -const CACHE_VERSION = 'nexus-timer-cache-v6'; +const CACHE_VERSION = 'nexus-timer-cache-v7'; const APP_SHELL_URLS = [ // '/', // Let NetworkFirst handle '/' '/manifest.json', diff --git a/scripts/git-hooks/pre-commit.cjs b/scripts/git-hooks/pre-commit.cjs new file mode 100755 index 0000000..1f0041e --- /dev/null +++ b/scripts/git-hooks/pre-commit.cjs @@ -0,0 +1,71 @@ +#!/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 \ No newline at end of file diff --git a/src/views/InfoView.vue b/src/views/InfoView.vue index 144740b..d2b8450 100644 --- a/src/views/InfoView.vue +++ b/src/views/InfoView.vue @@ -1,7 +1,11 @@ \ No newline at end of file + + + \ No newline at end of file