accesscodes/scripts/update-last-modified.js
2025-12-21 08:51:14 +00:00

89 lines
2.7 KiB
JavaScript
Raw Permalink 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.

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Get the root directory of the git repository
function getGitRoot() {
try {
return execSync('git rev-parse --show-toplevel').toString().trim();
} catch (error) {
console.error('Error finding git root:', error.message);
return null;
}
}
function getLastCommitDate(filePath) {
try {
const gitRoot = getGitRoot();
if (!gitRoot) {
console.error('Could not find git repository root');
return null;
}
// Get the relative path from git root
const relativePath = path.relative(gitRoot, path.resolve(process.cwd(), filePath));
console.log(`Checking git history for: ${relativePath}`);
// Check if the file is tracked by git
const isTracked = execSync(`git ls-files --error-unmatch "${relativePath}" 2>/dev/null || echo ""`).toString().trim();
if (!isTracked) {
console.log('File is not tracked by git, using current date');
return new Date().toISOString();
}
// Get the last commit date for the file
const lastCommitDate = execSync(
`git log -1 --format="%ad" --date=iso -- "${relativePath}"`,
{ cwd: gitRoot }
).toString().trim();
return lastCommitDate || new Date().toISOString();
} catch (error) {
console.error('Error getting last commit date:', error.message);
return new Date().toISOString();
}
}
function updateLastModified() {
const privacyPagePath = path.join(process.cwd(), 'src/app/privacy/page.tsx');
try {
let content = fs.readFileSync(privacyPagePath, 'utf8');
const lastCommitDate = getLastCommitDate(privacyPagePath);
if (!lastCommitDate) {
console.log('Using current date as fallback');
return;
}
const formattedDate = new Date(lastCommitDate).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
const lastUpdatedText = `Last updated: ${formattedDate}`;
// Check if we need to add or update the last updated line
if (content.includes('LAST_UPDATED_PLACEHOLDER')) {
// Replace the placeholder with the actual date
const updatedContent = content.replace(
/\{\/\*\s*LAST_UPDATED_PLACEHOLDER\s*\*\/\}/,
`<p className="text-sm text-muted-foreground mb-4">
${lastUpdatedText}
</p>`
);
if (updatedContent !== content) {
fs.writeFileSync(privacyPagePath, updatedContent, 'utf8');
console.log('✅ Updated last modified date in privacy policy');
}
} else {
console.log('ℹ️ Last modified date is up to date');
}
} catch (error) {
console.error('Error updating last modified date:', error.message);
}
}
updateLastModified();