UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

127 lines (126 loc) 5.92 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 path_1 = require("path"); /** * Reset current branch */ const NewCommand = { alias: ['rs'], description: 'Reset to remote state', hidden: false, name: 'reset', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; // Retrieve the tools we need const { config, git, helper, npm, parameters, print: { error, info, spin, success, warning }, prompt, system, } = 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.reset, config: ltConfig, parentConfig: (_c = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _c === void 0 ? void 0 : _c.git, }); // Start timer const timer = system.startTimer(); // Check git if (!(yield git.gitInstalled())) { return; } // Current branch const branch = yield git.currentBranch(); if (!branch) { error('No current branch!'); return; } // Check remote (use short SSH timeout so ls-remote doesn't hang in offline environments) const remoteBranch = yield system.run(`GIT_TERMINAL_PROMPT=0 GIT_SSH_COMMAND="ssh -o ConnectTimeout=5 -o BatchMode=yes" git ls-remote --heads origin ${branch} 2>/dev/null || true`); if (!remoteBranch) { error(`No remote branch ${branch} found!`); return; } // Dry-run mode: show what would be affected if (dryRun) { warning('DRY-RUN MODE - No changes will be made'); info(''); // Show local changes that would be lost const status = yield system.run('git status --porcelain'); if (status === null || status === void 0 ? void 0 : status.trim()) { const lines = status.trim().split('\n'); info(`Would discard on branch "${branch}":`); info(` - ${lines.length} file(s) with local changes`); info(''); info('Files:'); lines.forEach((line) => info(` ${line}`)); } else { info('No local changes to discard.'); } // Show commits that would be lost const localCommits = yield system.run(`git log origin/${branch}..HEAD --oneline 2>/dev/null || echo ""`); if (localCommits === null || localCommits === void 0 ? void 0 : localCommits.trim()) { info(''); info('Local commits that would be lost:'); localCommits .trim() .split('\n') .forEach((line) => info(` ${line}`)); } info(''); info('Actions that would be performed:'); info(' 1. Clean untracked files (git clean -fd)'); info(' 2. Reset HEAD (git reset HEAD --hard)'); info(' 3. Checkout main and pull'); info(` 4. Delete local branch "${branch}"`); info(` 5. Checkout "${branch}" from remote`); info(` 6. Run ${toolbox.pm.install()}`); return `dry-run reset branch ${branch}`; } // Ask for reset if (!noConfirm && !(yield prompt.confirm(`Reset branch ${branch} to the remote state`))) { return; } // Reset const resetSpin = spin(`Reset ${branch}`); yield system.run('git clean -fd && ' + 'git reset HEAD --hard && ' + 'git checkout main && ' + 'git fetch && ' + 'git pull && ' + `git branch -D ${branch} && ` + `git checkout ${branch} && ` + 'git pull'); resetSpin.succeed(); // Install packages with correctly detected package manager (supports monorepo lockfiles) const { path: pkgPath } = yield npm.getPackageJson(); if (pkgPath) { const projectDir = (0, path_1.dirname)(pkgPath); const detectedPm = toolbox.pm.detect(projectDir); const installSpin = spin(`Install packages using ${detectedPm}`); yield system.run(`cd ${projectDir} && ${toolbox.pm.install(detectedPm)}`); installSpin.succeed(); } // Success info success(`Branch ${branch} was reset in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `reset branch ${branch}`; }), }; exports.default = NewCommand;