UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

222 lines (221 loc) 9.52 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 }); /** * Squash branch */ const NewCommand = { alias: ['s'], description: 'Squash commits', hidden: false, name: 'squash', 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: { ask, confirm }, system: { run, startTimer }, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); const configBase = (_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.squash) === null || _c === void 0 ? void 0 : _c.base; const configAuthor = (_f = (_e = (_d = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _d === void 0 ? void 0 : _d.git) === null || _e === void 0 ? void 0 : _e.squash) === null || _f === void 0 ? void 0 : _f.author; // Load global defaults const globalBaseBranch = config.getGlobalDefault(ltConfig, 'baseBranch'); const globalAuthor = config.getGlobalDefault(ltConfig, 'author'); // 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: (_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.squash, 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 (['dev', 'develop', 'main', 'master', 'release', 'test'].includes(branch)) { error(`Squash of branch ${branch} is not allowed!`); return; } // Determine base branch with priority: CLI > config > global > default const cliBase = parameters.first; let base; if (cliBase) { base = cliBase; } else if (configBase) { base = configBase; } else if (globalBaseBranch) { base = globalBaseBranch; } else { base = 'dev'; // Default for dry-run preview } // Dry-run mode: show what would be affected if (dryRun) { warning('DRY-RUN MODE - No changes will be made'); info(''); // Get merge base for preview const mergeBase = yield git.getMergeBase(base); if (!mergeBase) { error(`No merge base found with ${base}!`); return `dry-run squash branch ${branch} (no merge base)`; } // Show commits that would be squashed const commitsToSquash = yield run(`git log ${mergeBase}..HEAD --oneline`); if (commitsToSquash === null || commitsToSquash === void 0 ? void 0 : commitsToSquash.trim()) { const commitCount = commitsToSquash.trim().split('\n').length; info(`Would squash ${commitCount} commit(s) on branch "${branch}" into base "${base}":`); info(''); info('Commits to be squashed:'); commitsToSquash .trim() .split('\n') .forEach((line) => info(` ${line}`)); } else { info(`No commits to squash on branch "${branch}" (already up to date with "${base}")`); } // Show the first commit message that would be used const squashMessage = yield git.getFirstBranchCommit(branch, base); if (squashMessage) { info(''); info(`Default squash message: "${squashMessage}"`); } info(''); info('Actions that would be performed:'); info(` 1. Soft reset to merge base (${mergeBase})`); info(' 2. Commit all changes with squash message'); info(' 3. Force push to origin'); return `dry-run squash branch ${branch}`; } // Check for changes if (yield git.changes({ showError: true })) { return; } // Ask to squash the branch if (!noConfirm && !(yield confirm(`Squash branch ${branch}?`))) { return; } // Start timer const timer = startTimer(); // If base was not determined from CLI/config/global, ask interactively if (!cliBase && !configBase && !globalBaseBranch) { base = yield helper.getInput('', { initial: 'dev', name: 'Base branch', showError: false, }); } else if (configBase && !cliBase) { info(`Using base branch from lt.config commands.git.squash: ${configBase}`); } else if (globalBaseBranch && !cliBase && !configBase) { info(`Using base branch from lt.config defaults: ${globalBaseBranch}`); } // Merge base const mergeBaseSpin = spin(`Get merge ${base}`); const mergeBase = yield git.getMergeBase(base); if (!mergeBase) { error('No merge base found!'); return; } mergeBaseSpin.succeed(); // Get squash message (before reset) const squashMessage = yield git.getFirstBranchCommit(yield git.currentBranch(), base); // Soft reset const resetSpin = spin('Soft reset'); yield git.reset(mergeBase, true); resetSpin.succeed(); // Get status const status = yield git.status(); // Ask to go on info(`You are now on commit ${mergeBase}, with following changes:`); info(status); if (!noConfirm && !(yield confirm('Continue?'))) { return; } // Get git user const user = yield git.getUser(); // Determine author with priority: CLI > config > global > interactive const cliAuthor = parameters.options.author; let author; if (cliAuthor) { author = cliAuthor; } else if (configAuthor) { author = configAuthor; info(`Using author from lt.config commands.git.squash: ${configAuthor}`); } else if (globalAuthor) { author = globalAuthor; info(`Using author from lt.config defaults: ${globalAuthor}`); } else if (noConfirm) { // In noConfirm mode, use git user as default author = `${user.name} <${user.email}>`; } else { author = (yield ask({ initial: `${user.name} <${user.email}>`, message: 'Author: ', name: 'author', type: 'input', })).author; } // Ask for message let message = parameters.options.message; if (!message && !noConfirm) { message = (yield ask({ initial: squashMessage, message: 'Message: ', name: 'message', type: 'input', })).message; } else if (!message) { // In noConfirm mode without message, use the first commit message message = squashMessage; } // Confirm inputs info(author); info(message); if (!noConfirm && !(yield confirm('Commit?'))) { return; } // Start spinner const commitSpin = spin('Commit'); // Commit and push yield run(`git commit -am "${message}" --author="${author}"`); commitSpin.succeed(); if (!noConfirm && !(yield confirm('Push force?'))) { return; } // Start timer const pushForceSpin = spin('Push force'); // Push yield run('git push -f origin HEAD'); pushForceSpin.succeed(); // Success success(`Squashed ${branch} in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `squashed ${branch}`; }), }; exports.default = NewCommand;