Fix self-updater to use the current branch

This commit is contained in:
Logan Cusano
2024-09-14 23:12:35 -04:00
parent 821e4f6a64
commit bc0fc23fb0

View File

@@ -1,8 +1,8 @@
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'
import { restartService } from './serviceHandler.mjs';
import { launchProcess } from './subprocessHandler.mjs';
const git = simpleGit();
@@ -12,27 +12,31 @@ export const checkForUpdates = async () => {
// Fetch remote changes
await git.fetch();
// Get the latest commit hash
const latestCommitHash = await git.revparse(['@{u}']);
// Get the current branch
const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']);
log.INFO(`Current branch is ${currentBranch}`);
// Get the latest commit hash for the current branch
const latestCommitHash = await git.revparse([`${currentBranch}@{u}`]);
// Compare with the local commit hash
const localCommitHash = await git.revparse(['HEAD']);
if (latestCommitHash !== localCommitHash) {
log.INFO('An update is available. Updating...');
log.INFO(`An update is available on branch ${currentBranch}. Updating...`);
// Check if there have been any changes to the code
const gitStatus = await git.status()
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...");
log.INFO("There is locally modified code, stashing changes...");
await git.stash();
await git.reset('hard', ['origin/master']);
}
// Pull the latest changes from the remote repository
await git.pull();
// Ensure we are on the correct branch and pull the latest changes
await git.checkout(currentBranch);
await git.pull('origin', currentBranch);
// Run the post-update script
log.INFO('Running post-update script...');
@@ -42,10 +46,10 @@ export const checkForUpdates = async () => {
log.INFO('Update completed successfully. Restarting the application...');
restartApplication();
return true
return true;
} else {
log.INFO('The application is up to date.');
return false
return false;
}
} catch (error) {
log.ERROR('Error checking for updates:', error);
@@ -53,7 +57,7 @@ export const checkForUpdates = async () => {
}
// Function to restart the application
export const restartApplication = () => {
export const restartApplication = () => {
log.INFO('Restarting the application...');
restartService('discord-radio-bot');
}
};