git-branch-clean/main.js

36 lines
1.1 KiB
JavaScript

const yml = require('require-yml');
const path = require('path');
const fs = require('fs-extra');
const simpleGit = require('simple-git');
const { promisify } = require('util');
let conf = yml(path.join(__dirname, 'config.yml'));
const localConfFile = path.join(__dirname, 'config.local.yml');
if (fs.existsSync(localConfFile))
conf = Object.assign({}, conf, yml(localConfFile));
const git = simpleGit(conf.path);
const gitBranch = promisify(git.branch.bind(git));
const gitPush = promisify(git.push.bind(git));
async function main() {
try {
await git.fetch(['--prune']);
let branches = (await gitBranch(['-r', '--merged', 'HEAD'])).all;
const excludedBranches = ['test', 'master', 'dev'];
branches = branches.map((branch) => {return branch.substring(7, branch.length)});
branches = branches.filter((element) => !excludedBranches.includes(element));
for (let branch of branches) {
await gitPush(['origin', '--delete', branch]);
console.log(`Branch deleted: ${branch}`);
}
console.log('Merged branches deleted successfully!');
} catch (err) {
console.error('[Error]', err);
}
}
main();