git-branch-clean/main.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-07-31 09:44:56 +00:00
const yml = require('require-yml');
const path = require('path');
const fs = require('fs-extra');
2023-07-31 08:51:27 +00:00
const simpleGit = require('simple-git');
2023-07-31 08:28:03 +00:00
const { promisify } = require('util');
2023-07-31 09:44:56 +00:00
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);
2023-07-31 08:28:03 +00:00
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;
2023-07-31 08:51:27 +00:00
const excludedBranches = ['test', 'master', 'dev'];
2023-07-31 08:28:03 +00:00
branches = branches.map((branch) => {return branch.substring(7, branch.length)});
2023-07-31 08:51:27 +00:00
branches = branches.filter((element) => !excludedBranches.includes(element));
2023-07-31 08:28:03 +00:00
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();