Files
DRB-Client/modules/selfUpdater.mjs
2024-06-02 20:10:55 -04:00

59 lines
2.0 KiB
JavaScript

import { DebugBuilder } from "./debugger.mjs";
const log = new DebugBuilder("client", "selfUpdater");
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) {
log.INFO('An update is available. Updating...');
// Check if there have been any changes to the code
const gitStatus = await git.status()
log.INFO(gitStatus);
if (gitStatus.modified.length > 0){
// There is locally modified code
log.INFO("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
log.INFO('Running post-update script...');
await launchProcess("bash", ['./post-update.sh'], true);
// Restart the application to apply the updates
log.INFO('Update completed successfully. Restarting the application...');
restartApplication();
return true
} else {
log.INFO('The application is up to date.');
return false
}
} catch (error) {
log.ERROR('Error checking for updates:', error);
}
}
// Function to restart the application
export const restartApplication = () => {
log.INFO('Restarting the application...');
restartService('discord-radio-bot');
}