From 0e409c403dc5d74362fe086377ae89c651efc435 Mon Sep 17 00:00:00 2001 From: cpu Date: Fri, 9 May 2025 17:10:59 +0200 Subject: [PATCH] clean up --- README.md | 21 +++++-------- scripts/git-hooks/pre-commit.cjs | 52 ++++++++++++++++---------------- src/views/InfoView.vue | 2 +- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 9bdb018..10dc8cf 100644 --- a/README.md +++ b/README.md @@ -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... diff --git a/scripts/git-hooks/pre-commit.cjs b/scripts/git-hooks/pre-commit.cjs index 1f0041e..c3d4e30 100755 --- a/scripts/git-hooks/pre-commit.cjs +++ b/scripts/git-hooks/pre-commit.cjs @@ -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 \ No newline at end of file +process.exit(0); \ No newline at end of file diff --git a/src/views/InfoView.vue b/src/views/InfoView.vue index 9830a9a..7156141 100644 --- a/src/views/InfoView.vue +++ b/src/views/InfoView.vue @@ -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");`