@lenne.tech/cli
Version:
lenne.Tech CLI: lt
117 lines (116 loc) • 5.07 kB
JavaScript
;
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 });
/**
* Rename branch
*/
const NewCommand = {
alias: ['rn'],
description: 'Rename current branch',
hidden: false,
name: 'rename',
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: { error, 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.rename,
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;
}
// Get current branch
const branch = yield git.currentBranch();
// Get new name
const name = yield helper.getInput(parameters.first, {
name: 'new name',
showError: true,
});
if (!name) {
return;
}
// Check branch
if (branch === 'main' || branch === 'release' || branch === 'dev') {
error(`Rename branch ${branch} is not allowed!`);
return;
}
// Check name
if (yield git.getBranch(name, { exact: true })) {
error(`Branch with name ${name} already exists`);
return;
}
// Dry-run mode: show what would be affected
if (dryRun) {
warning('DRY-RUN MODE - No changes will be made');
info('');
// Check for remote branch
const remote = yield git.getBranch(branch, { exact: true, remote: true });
info(`Would rename branch "${branch}" to "${name}"`);
info('');
info('Actions that would be performed:');
info(` 1. Rename local branch (git branch -m ${name})`);
if (remote) {
info(` 2. Push new branch name to remote (git push origin ${name})`);
info(` 3. Delete old remote branch (git push origin :${branch})`);
}
else {
info(' (No remote branch to update)');
}
return `dry-run rename branch ${branch} to ${name}`;
}
// Ask to rename branch
if (!noConfirm && !(yield confirm(`Rename branch ${branch} into ${name}?`))) {
return;
}
// Start timer
let timer = startTimer();
// Get remote
const remote = yield git.getBranch(name, { exact: true, remote: true });
// Rename branch
const renameSpin = spin(`Rename ${branch} into ${name}`);
yield run(`git branch -m ${name}`);
// Ask to push branch
if (remote && (noConfirm || (yield confirm(`Push ${name} to remote?`)))) {
yield run(`git push origin ${name}`);
}
renameSpin.succeed();
// Save time
let time = timer();
// Ask to delete remote branch
if (remote &&
(parameters.options.deleteRemote || (!noConfirm && (yield confirm(`Delete remote branch ${branch}?`))))) {
timer = startTimer();
const deleteSpin = spin(`Delete remote branch ${branch}`);
yield run(`git push origin :${branch}`);
deleteSpin.succeed();
time += timer();
}
// Success
success(`Renamed ${branch} to ${name} in ${helper.msToMinutesAndSeconds(time)}m.`);
info('');
// Exit if not running from menu
if (!toolbox.parameters.options.fromGluegunMenu) {
process.exit();
}
// For tests
return `renamed ${branch} to ${name}`;
}),
};
exports.default = NewCommand;