Files
DRB-Client/modules/selfUpdater.mjs

57 lines
1.9 KiB
JavaScript

import simpleGit from 'simple-git';
import { restartService } from './serviceHandler.mjs'
import { launchProcess } from './subprocessHandler.mjs'
const git = simpleGit();
// Function to check for updates
export const checkForUpdates = async () => {
try {
// Fetch remote changes
await git.fetch();
// Get the latest commit hash
const latestCommitHash = await git.revparse(['@{u}']);
// Compare with the local commit hash
const localCommitHash = await git.revparse(['HEAD']);
if (latestCommitHash !== localCommitHash) {
console.log('An update is available. Updating...');
// Check if there have been any changes to the code
const gitStatus = await git.status()
console.log(gitStatus);
if (gitStatus.modified.length > 0){
// There is locally modified code
console.log("There is locally modified code, resetting...");
await git.stash();
await git.reset('hard', ['origin/master']);
}
// Pull the latest changes from the remote repository
await git.pull();
// Run the post-update script
console.log('Running post-update script...');
await launchProcess("bash", ['./post-update.sh'], true);
// Restart the application to apply the updates
console.log('Update completed successfully. Restarting the application...');
restartApplication();
return true
} else {
console.log('The application is up to date.');
return false
}
} catch (error) {
console.error('Error checking for updates:', error);
}
}
// Function to restart the application
export const restartApplication = () => {
console.log('Restarting the application...');
restartService('discord-radio-bot');
}