Files
GreenHomeUI/scripts/generate-version.js
alireza 31b58b7151
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
fix bug and version check
2025-12-18 19:39:05 +03:30

39 lines
1.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const version = Date.now().toString();
const versionFile = path.join(__dirname, '../public/version.json');
// Ensure public directory exists
const publicDir = path.dirname(versionFile);
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true });
}
fs.writeFileSync(versionFile, JSON.stringify({ version }, null, 2));
// Also write to .env.local for Next.js to use
const envFile = path.join(__dirname, '../.env.local');
const envContent = `NEXT_PUBLIC_APP_VERSION=${version}\n`;
fs.writeFileSync(envFile, envContent);
// Update cache name in sw.js based on version
const swPath = path.join(__dirname, '../public/sw.js');
if (fs.existsSync(swPath)) {
let swContent = fs.readFileSync(swPath, 'utf8');
// Update cache names with version - using more specific regex
const cacheNameRegex = /const CACHE_NAME\s*=\s*['"][^'"]*['"]/;
const staticCacheNameRegex = /const STATIC_CACHE_NAME\s*=\s*['"][^'"]*['"]/;
if (cacheNameRegex.test(swContent)) {
swContent = swContent.replace(cacheNameRegex, `const CACHE_NAME = 'greenhome-${version}'`);
}
if (staticCacheNameRegex.test(swContent)) {
swContent = swContent.replace(staticCacheNameRegex, `const STATIC_CACHE_NAME = 'greenhome-static-${version}'`);
}
fs.writeFileSync(swPath, swContent);
}
console.log(`Version generated: ${version}`);