@overwolf/ow-electron
Version:
Build cross platform desktop apps with JavaScript, HTML, and CSS
72 lines (63 loc) • 2.73 kB
JavaScript
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
function calculateFileHash(filePath) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const stream = fs.createReadStream(filePath);
stream.on('error', err => reject(err));
stream.on('data', chunk => hash.update(chunk));
stream.on('end', () => resolve(hash.digest('hex')));
});
}
async function validateChecksums(folderPath) {
try {
// Read checksums.json from current directory
const checksumPath = path.join(process.cwd(), 'checksums.json');
const checksums = JSON.parse(fs.readFileSync(checksumPath, 'utf8'));
let hasUpdates = false;
// Check each file
for (const [fileName, expectedHash] of Object.entries(checksums)) {
const filePath = path.join(folderPath, fileName);
if (fs.existsSync(filePath)) {
const actualHash = await calculateFileHash(filePath);
const matches = actualHash === expectedHash;
if (!matches) {
console.log(`Actual hash: ${actualHash}`);
console.log(`File: ${fileName}`);
console.log(`Expected hash: ${expectedHash}`);
console.log(`Match: ${matches}`);
// Update checksums.json if hashes don't match
console.log('Updating checksums.json with new hash...');
checksums[fileName] = actualHash;
hasUpdates = true;
} else {
console.log(`File: ${fileName} match`);
}
} else {
console.log(`File: ${fileName} not found in specified directory`);
}
}
// Write updated checksums back to file if there were any changes
if (hasUpdates) {
fs.writeFileSync(
checksumPath,
JSON.stringify(checksums, null, 2), // Pretty print with 2 spaces
'utf8'
);
console.log('\nChecksums.json has been updated with new hashes.');
} else {
console.log('\nNo updates were necessary.');
}
} catch (error) {
console.error('Error:', error.message);
}
}
// Check if folder path is provided as command line argument
const folderPath = process.argv[2];
if (!folderPath) {
console.error('Please provide a folder path as an argument');
console.error('Usage: node checksum-validator.js <folder-path>');
process.exit(1);
}
validateChecksums(folderPath);