UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

146 lines (145 loc) 7.29 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 }); /** * Rebase branch */ const NewCommand = { alias: ['rb'], description: 'Rebase onto branch', hidden: false, name: 'rebase', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j; // Retrieve the tools we need const { config, git, helper, parameters, print: { error, info, spin, success, warning }, prompt: { confirm }, system: { run, startTimer }, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); const configBaseBranch = (_d = (_c = (_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.rebase) === null || _c === void 0 ? void 0 : _c.base) !== null && _d !== void 0 ? _d : (_f = (_e = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _e === void 0 ? void 0 : _e.git) === null || _f === void 0 ? void 0 : _f.baseBranch; // Load global defaults const globalBaseBranch = config.getGlobalDefault(ltConfig, 'baseBranch'); // Parse CLI arguments const cliBaseBranch = parameters.options.base; 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: (_h = (_g = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _g === void 0 ? void 0 : _g.git) === null || _h === void 0 ? void 0 : _h.rebase, config: ltConfig, parentConfig: (_j = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _j === void 0 ? void 0 : _j.git, }); // Check git if (!(yield git.gitInstalled())) { return; } // Get current branch const branch = yield git.currentBranch(); // Check branch if (branch === 'main' || branch === 'release' || branch === 'dev') { error(`Rebase of branch ${branch} is not allowed!`); return; } // Determine base branch early for dry-run const baseBranchPreview = cliBaseBranch || parameters.first || configBaseBranch || globalBaseBranch || 'dev'; // Dry-run mode: show what would be affected if (dryRun) { warning('DRY-RUN MODE - No changes will be made'); info(''); // Show commits that would be rebased const commitsToRebase = yield run(`git log ${baseBranchPreview}..HEAD --oneline 2>/dev/null || echo ""`); if (commitsToRebase === null || commitsToRebase === void 0 ? void 0 : commitsToRebase.trim()) { const commitCount = commitsToRebase.trim().split('\n').length; info(`Would rebase ${commitCount} commit(s) from branch "${branch}" onto "${baseBranchPreview}":`); info(''); info('Commits to be rebased:'); commitsToRebase .trim() .split('\n') .forEach((line) => info(` ${line}`)); } else { info(`No commits to rebase on branch "${branch}" (already up to date with "${baseBranchPreview}")`); } info(''); info('Actions that would be performed:'); info(` 1. Fetch latest changes`); info(` 2. Checkout and pull ${baseBranchPreview}`); info(` 3. Checkout ${branch}`); info(` 4. Rebase onto ${baseBranchPreview}`); return `dry-run rebase branch ${branch}`; } // Ask to Rebase the branch if (!noConfirm && !(yield confirm(`Rebase branch ${branch}?`))) { return; } // Determine base branch with priority: CLI --base > CLI first param > config > global > interactive let baseBranch; // Helper function to validate and use a branch const tryUseBranch = (branchName, source) => __awaiter(void 0, void 0, void 0, function* () { if (yield git.getBranch(branchName)) { baseBranch = branchName; info(`Using base branch from ${source}: ${branchName}`); return true; } info(`Configured base branch "${branchName}" not found (${source}), trying next source...`); return false; }); if (cliBaseBranch) { // CLI --base parameter if (yield git.getBranch(cliBaseBranch)) { baseBranch = cliBaseBranch; } else { error(`Base branch ${cliBaseBranch} does not exist!`); return; } } else if (parameters.first) { // CLI first parameter (positional) if (yield git.getBranch(parameters.first)) { baseBranch = parameters.first; } } if (!baseBranch && configBaseBranch) { // Command-specific or git-level config if (!(yield tryUseBranch(configBaseBranch, 'lt.config commands.git'))) { // Fall through to global if (globalBaseBranch && globalBaseBranch !== configBaseBranch) { yield tryUseBranch(globalBaseBranch, 'lt.config defaults'); } } } else if (!baseBranch && globalBaseBranch) { // Global default yield tryUseBranch(globalBaseBranch, 'lt.config defaults'); } // If still no baseBranch, go interactive if (!baseBranch) { baseBranch = yield git.selectBranch({ text: 'Select base branch' }); } // Start timer const timer = startTimer(); // Rebase const rebaseSpin = spin(`Set ${baseBranch} as base of ${branch}`); yield run(`git fetch && git checkout ${baseBranch} && git pull && git checkout ${branch} && git rebase ${baseBranch}`); rebaseSpin.succeed(); // Success success(`Rebased ${branch} in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `rebased ${branch}`; }), }; exports.default = NewCommand;