UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

133 lines (132 loc) 5.88 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const gluegun_1 = require("gluegun"); /** * Removed local merged branches */ const NewCommand = { alias: ['rm'], description: 'Remove merged branches', hidden: false, name: 'clean', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; // Retrieve the tools we need const { config, git, helper, parameters, print: { info, spin, success, warning }, prompt: { confirm }, system: { run, startTimer }, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); // Parse CLI arguments const dryRun = parameters.options.dryRun || parameters.options['dry-run']; // Determine noConfirm with priority: CLI > config > global > default (false) const noConfirm = config.getNoConfirm({ cliValue: parameters.options.noConfirm, commandConfig: (_b = (_a = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _a === void 0 ? void 0 : _a.git) === null || _b === void 0 ? void 0 : _b.clean, config: ltConfig, parentConfig: (_c = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _c === void 0 ? void 0 : _c.git, }); // Check git if (!(yield git.gitInstalled())) { return; } const excludedBranches = ['main', 'test', 'dev', 'develop']; // Dry-run mode: show what would be deleted (without side effects) if (dryRun) { warning('DRY-RUN MODE - No changes will be made'); info(''); // Get current merged branches without fetching const result = yield gluegun_1.system.run('git branch --merged'); const branches = result .split('\n') .map((b) => b.trim().replace(/^\* /, '')) .filter((b) => b && !excludedBranches.includes(b)); if (branches.length === 0) { info('No merged branches to delete.'); info(''); info('Note: Run without --dry-run to fetch latest from remote first.'); return 'dry-run clean: no branches'; } info(`Would delete ${branches.length} merged branch(es):`); branches.forEach((b) => info(` - ${b}`)); info(''); info(`Excluded branches: ${excludedBranches.join(', ')}`); info(''); info('Actions that would be performed:'); info(' 1. Checkout dev/main branch'); info(' 2. Fetch and prune remote'); info(' 3. Pull latest changes'); info(' 4. Delete merged branches'); return `dry-run clean: ${branches.length} branches`; } // Get current branch const currentBranch = yield git.currentBranch(); // Ask for confirmation before cleaning if (!noConfirm && !(yield confirm('Remove all merged branches?'))) { return; } let branch; if (currentBranch !== 'dev' && currentBranch !== 'main') { // Search for branch, which includes branch name branch = yield git.getBranch('dev', { error: false, exact: false, remote: false, spin: true, }); if (branch !== 'dev') { branch = yield git.getBranch('main', { error: true, exact: false, remote: false, spin: true, }); } yield run(`git checkout ${branch}`); info(`Changed Branch to ${branch}`); } // Start timer const timer = startTimer(); // Reset soft const undoSpinner = spin('Start cleaning\n'); const resultFetch = yield run('git fetch -p'); info(resultFetch); const resultpull = yield run('git pull'); info(resultpull); const result = yield gluegun_1.system.run('git branch --merged'); // Local Branches into Array const branches = result .split('\n') .map((b) => b.trim().replace(/^\* /, '')) // Remove '* ' .filter((b) => b && !excludedBranches.includes(b)); if (branches.length === 0) { undoSpinner.succeed(); info('No branches to delete.'); return; } info(`Deleting branches: ${branches.join(', ')}`); // Delete branches for (const branchToDelete of branches) { yield gluegun_1.system.run(`git branch -d ${branchToDelete}`); success(`Deleted branch: ${branchToDelete}`); } undoSpinner.succeed(); // Success success(`Successfully cleaned in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return 'cleaned branches'; }), }; exports.default = NewCommand;