[AsarUpdate] Source cleanup

This commit is contained in:
Ducko 2022-04-12 13:38:09 +01:00
parent a8dd97e3cc
commit 031d634ac9

View file

@ -18,55 +18,42 @@ module.exports = async () => { // (Try) update asar
const originalHash = getAsarHash(); const originalHash = getAsarHash();
const downloadSuccess = await new Promise((res) => { await new Promise((res) => {
const file = fs.createWriteStream(downloadPath); const file = fs.createWriteStream(downloadPath);
let writeError;
file.on('error', e => { file.on('error', e => {
log('AsarUpdate', 'Failed to write', e); log('AsarUpdate', e);
file.close(); file.close();
writeError = true; res();
res(false);
});
const req = request.get(asarUrl);
req.on('response', (res) => {
if (writeError) return;
res.pipe(file);
}); });
file.on('finish', () => { file.on('finish', () => {
file.close(); file.close();
res(true); res();
});
}); });
if (!downloadSuccess || fs.readFileSync(downloadPath, 'utf8').startsWith('<Error>')) return log('AsarUpdate', 'Download error'); const req = request.get(asarUrl);
const copySuccess = await new Promise((res) => { req.on('response', (res) => res.pipe(file));
});
if (fs.readFileSync(downloadPath, 'utf8').startsWith('<Error>')) return log('AsarUpdate', 'Download error');
if (await new Promise((res) => {
try { try {
fs.copyFileSync(downloadPath, asarPath); // Overwrite actual app.asar fs.copyFileSync(downloadPath, asarPath); // Overwrite actual app.asar
fs.unlinkSync(downloadPath); // Delete downloaded temp file fs.unlinkSync(downloadPath); // Delete downloaded temp file
res(true); res();
} catch (err) { } catch (err) {
log('AsarUpdate', 'Copy error', err); log('AsarUpdate', err);
res(false); res(true);
} }
}); })) return;
if (!copySuccess) return;
const newHash = getAsarHash(); const newHash = getAsarHash();
const changed = originalHash !== newHash;
log('AsarUpdate', `Hash Comparison: if (oaConfig.updatePrompt === true && originalHash !== newHash) {
Original: ${originalHash}
New: ${newHash}`);
if (changed && oaConfig.updatePrompt === true) {
const { response } = await dialog.showMessageBox(null, { const { response } = await dialog.showMessageBox(null, {
message: 'Updated OpenAsar', message: 'Updated OpenAsar',
detail: `Restart required to use new version.`, detail: `Restart required to use new version.`,