Test


Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
/* eslint-disable no-console */
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createHash } from 'node:crypto';
// Cache file location: scripts/deploy/deploy-cache.json
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const CACHE_FILE = join(__dirname, 'deploy-cache.json');
/**
* Load deployment cache from disk.
* @returns {Object|null} Cache object or null if not found.
*/
export function loadCache() {
if (!existsSync(CACHE_FILE)) {
return null;
}
try {
const content = readFileSync(CACHE_FILE, 'utf-8');
return JSON.parse(content);
} catch (error) {
console.warn(`Warning: Could not read cache file: ${error.message}`);
return null;
}
}
/**
* Save deployment cache to disk.
* @param {Object} cache Cache object to save.
*/
export function saveCache(cache) {
try {
writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
} catch (error) {
console.warn(`Warning: Could not write cache file: ${error.message}`);
}
}
/**
* Calculate file hash (MD5 for speed).
* @param {string} filePath Path to file.
* @returns {string} Hex hash of file content.
*/
function calculateFileHash(filePath) {
try {
const fileBuffer = readFileSync(filePath);
const hashSum = createHash('md5');
hashSum.update(fileBuffer);
return hashSum.digest('hex');
} catch (error) {
// If file can't be read, log warning and return empty hash
console.warn(`Warning: Could not calculate hash for ${filePath}: ${error.message}`);
return '';
}
}
/**
* Get file metadata for cache.
* @param {string} filePath Path to file.
* @param {Object} stats File stats from fs.statSync.
* @returns {Object} File metadata with size and hash.
*/
export function getFileMetadata(filePath, stats) {
return {
size: stats.size,
hash: calculateFileHash(filePath)
};
}
/**
* Compare local and remote file metadata.
* @param {Object} localMeta Local file metadata (has size and hash).
* @param {Object} remoteMeta Remote file metadata (has size, may have hash).
* @returns {boolean} True if files differ.
*/
export function filesDiffer(localMeta, remoteMeta) {
if (!remoteMeta || !localMeta) {
return true; // One of them doesn't exist
}
// First check size (fastest check)
if (localMeta.size !== remoteMeta.size) {
return true;
}
// If sizes match, compare hash (if available)
// Local files always have hash, remote files might not (we don't hash remote files)
if (localMeta.hash && remoteMeta.hash) {
// Both have hash - compare hashes
return localMeta.hash !== remoteMeta.hash;
}
// If only one has hash, we can't be sure - assume they differ if sizes match but no hash comparison possible
// This is conservative: if we have hash info, use it; otherwise assume different
if (localMeta.hash && !remoteMeta.hash) {
// Local has hash but remote doesn't - can't compare, assume different to be safe
// But if size matches, it's likely the same file, so we could be optimistic
// For now, be conservative and assume different
return true;
}
// If sizes match and no hash info, assume same (size is good enough for most cases)
return false;
}

