Files
whatsapp-web.js/tools/version-checker/update-version
2022-11-26 18:17:44 -04:00

56 lines
1.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fs = require('fs');
const fetch = require('node-fetch');
const getLatestVersion = async (currentVersion) => {
const res = await fetch(`https://web.whatsapp.com/check-update?version=${currentVersion}&platform=web`);
const data = await res.json();
return data.currentVersion;
};
const getCurrentVersion = () => {
try {
const versionFile = fs.readFileSync('./.version');
return versionFile ? versionFile.toString().trim() : null;
} catch(_) {
return null;
}
};
const updateInFile = (filePath, oldVersion, newVersion) => {
const originalFile = fs.readFileSync(filePath);
const newFile = originalFile.toString().replaceAll(oldVersion, newVersion);
fs.writeFileSync(filePath, newFile);
};
const updateVersion = async (oldVersion, newVersion) => {
const filesToUpdate = [
'../../src/util/Constants.js',
'../../README.md',
];
for (const file of filesToUpdate) {
updateInFile(file, oldVersion, newVersion);
}
fs.writeFileSync('./.version', newVersion);
};
(async () => {
const currentVersion = getCurrentVersion();
const version = await getLatestVersion(currentVersion);
console.log(`Current version: ${currentVersion}`);
console.log(`Latest version: ${version}`);
if(currentVersion !== version) {
console.log('Updating files...');
await updateVersion(currentVersion, version);
console.log('Updated!');
} else {
console.log('No changes.');
}
})();