UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

90 lines (89 loc) 4.33 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 }); /** * Undo last commit (without losing files) */ const NewCommand = { alias: ['un'], description: 'Undo last commit', hidden: false, name: 'undo', 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.undo, 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; } // Last commit message const lastCommitMessage = yield git.lastCommitMessage(); // Dry-run mode: show what would be affected if (dryRun) { warning('DRY-RUN MODE - No changes will be made'); info(''); // Get current branch const branch = yield git.currentBranch(); // Show commit details const commitDetails = yield run('git log -1 --format="%h %s%n Author: %an <%ae>%n Date: %ad" --date=short'); info(`Would undo last commit on branch "${branch}":`); info(''); info('Commit to undo:'); commitDetails === null || commitDetails === void 0 ? void 0 : commitDetails.trim().split('\n').forEach((line) => info(` ${line}`)); // Show files that would become staged const changedFiles = yield run('git diff-tree --no-commit-id --name-status -r HEAD'); if (changedFiles === null || changedFiles === void 0 ? void 0 : changedFiles.trim()) { info(''); info('Files that would become staged:'); changedFiles .trim() .split('\n') .forEach((line) => info(` ${line}`)); } info(''); info('Note: This uses --soft reset, so changes are preserved as staged.'); return `dry-run undo last commit on branch ${branch}`; } // Ask to undo the commit if (!noConfirm && !(yield confirm(`Undo last commit "${lastCommitMessage}"?`))) { return; } // Start timer const timer = startTimer(); // Get current branch const branch = yield git.currentBranch(); // Reset soft const undoSpinner = spin(`Undo last commit of branch ${branch}`); yield run('git reset --soft HEAD~'); undoSpinner.succeed(); // Success success(`Undo last commit of ${branch} in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `undo last commit of branch ${branch}`; }), }; exports.default = NewCommand;