Created
February 7, 2024 18:48
-
-
Save seiyria/7c4090080a0fdf47a998b3da2209bc7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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