tractatus/scripts/update-cache-version.js
TheFlow 2af47035ac refactor: remove website code and fix critical startup crashes (Phase 8)
CRITICAL FIX: Server would CRASH ON STARTUP (multiple import errors)

REMOVED (2 scripts):
1. scripts/framework-watchdog.js
   - Monitored .claude/session-state.json (OUR Claude Code setup)
   - Monitored .claude/token-checkpoints.json (OUR file structure)
   - Implementers won't have our .claude/ directory

2. scripts/init-db.js
   - Created website collections: blog_posts, media_inquiries, case_submissions
   - Created website collections: resources, moderation_queue, users, citations
   - Created website collections: translations, koha_donations
   - Next steps referenced deleted scripts (npm run seed:admin)

REWRITTEN (2 files):

src/models/index.js (29 lines → 27 lines)
- REMOVED imports: Document, BlogPost, MediaInquiry, CaseSubmission, Resource
- REMOVED imports: ModerationQueue, User (all deleted in Phase 2)
- KEPT imports: AuditLog, DeliberationSession, GovernanceLog, GovernanceRule
- KEPT imports: Precedent, Project, SessionState, VariableValue, VerificationLog
- Result: Only framework models exported

src/server.js (284 lines → 163 lines, 43% reduction)
- REMOVED: Imports to deleted middleware (csrf-protection, response-sanitization)
- REMOVED: Stripe webhook handling (/api/koha/webhook)
- REMOVED: Static file caching (for deleted public/ directory)
- REMOVED: Static file serving (public/ deleted in Phase 6)
- REMOVED: CSRF token endpoint
- REMOVED: Website homepage with "auth, documents, blog, admin" references
- REMOVED: Instruction sync (scripts/sync-instructions-to-db.js reference)
- REMOVED: Hardcoded log path (${process.env.HOME}/var/log/tractatus/...)
- REMOVED: Website-specific security middleware
- KEPT: Security headers, rate limiting, CORS, body parsers
- KEPT: API routes, governance services, MongoDB connections
- RESULT: Clean framework-only server

RESULT: Repository can now start without crashes, all imports resolve

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 22:17:02 +13:00

123 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Update Cache Version - Unified Cache Busting
*
* Updates all HTML files with a consistent cache-busting version string.
* Format: v={package.version}.{timestamp}
* Example: v=0.1.0.1760201234
*
* This ensures all assets (CSS, JS) are loaded with the same version,
* solving the inconsistent cache busting problem.
*/
const fs = require('fs');
const path = require('path');
const packageJson = require('../package.json');
// Generate cache version: package version + timestamp
const CACHE_VERSION = `${packageJson.version}.${Date.now()}`;
// HTML files to update (relative to project root)
const HTML_FILES = [
'public/index.html',
'public/docs.html',
'public/faq.html',
'public/researcher.html',
'public/implementer.html',
'public/leader.html',
'public/about.html',
'public/privacy.html',
'public/blog.html',
'public/blog-post.html',
'public/docs-viewer.html',
'public/api-reference.html',
'public/media-inquiry.html',
'public/case-submission.html',
'public/koha.html',
'public/check-version.html'
];
/**
* Update cache version in a file
* Replaces all instances of ?v=X with ?v={CACHE_VERSION}
*/
function updateCacheVersion(filePath) {
try {
const fullPath = path.join(__dirname, '..', filePath);
if (!fs.existsSync(fullPath)) {
console.warn(`⚠️ File not found: ${filePath}`);
return false;
}
let content = fs.readFileSync(fullPath, 'utf8');
const originalContent = content;
// Pattern: ?v=ANYTHING → ?v={CACHE_VERSION}
// Matches: ?v=1.0.4, ?v=1759833751, ?v=1.0.5.1760123456
content = content.replace(/\?v=[0-9a-zA-Z._-]+/g, `?v=${CACHE_VERSION}`);
// Only write if changed
if (content !== originalContent) {
fs.writeFileSync(fullPath, content, 'utf8');
// Count replacements
const matches = originalContent.match(/\?v=[0-9a-zA-Z._-]+/g) || [];
console.log(`${filePath}: Updated ${matches.length} cache version(s)`);
return true;
} else {
console.log(` ${filePath}: No changes needed`);
return false;
}
} catch (error) {
console.error(`❌ Error updating ${filePath}:`, error.message);
return false;
}
}
/**
* Main execution
*/
function main() {
console.log('');
console.log('═'.repeat(70));
console.log(' Tractatus - Cache Version Update');
console.log('═'.repeat(70));
console.log('');
console.log(`📦 Package version: ${packageJson.version}`);
console.log(`🔄 New cache version: ${CACHE_VERSION}`);
console.log('');
let updatedCount = 0;
let totalFiles = 0;
HTML_FILES.forEach(file => {
totalFiles++;
if (updateCacheVersion(file)) {
updatedCount++;
}
});
console.log('');
console.log('═'.repeat(70));
console.log(` Summary: ${updatedCount}/${totalFiles} files updated`);
console.log('═'.repeat(70));
console.log('');
if (updatedCount > 0) {
console.log('✅ Cache version updated successfully!');
console.log(` All assets will now use: ?v=${CACHE_VERSION}`);
} else {
console.log(' No files needed updating');
}
console.log('');
}
// Run if called directly
if (require.main === module) {
main();
}
module.exports = { updateCacheVersion, CACHE_VERSION };