Skip to content

Instantly share code, notes, and snippets.

@seiyria
Created February 7, 2024 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seiyria/7c4090080a0fdf47a998b3da2209bc7b to your computer and use it in GitHub Desktop.
Save seiyria/7c4090080a0fdf47a998b3da2209bc7b to your computer and use it in GitHub Desktop.
const { exec } = require('child_process');
const { MultiSelect } = require('enquirer');
runCommand('git branch', (result) => {
const branches = parseBranches(result);
const prompt = new MultiSelect({
name: 'Branches',
message: 'Select branches to remove',
choices: branches.map((b) => {
return {
name: b
};
})
});
prompt.run()
.then(deleteBranches)
.catch(console.error);
});
function parseBranches(stdout) {
if (!stdout) {
return [];
}
const lines = stdout.trim().split('\n');
return lines.map((line) => {
return line.trim().replace(/^\*\s*/, '');
}).filter((line) => line !== '' && line !== 'master' && line !== 'main');
}
function deleteBranches(branchList) {
const deleteCommand = `git branch -D ${branchList.join(' ')}`;
console.log(deleteCommand);
runCommand(deleteCommand, (result) => {
console.log(result);
});
}
function runCommand(command, callback) {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
callback(stdout);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment