@lenne.tech/cli
Version:
lenne.Tech CLI: lt
165 lines (164 loc) • 7.59 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 });
const path_1 = require("path");
/**
* Create a new branch
*/
const NewCommand = {
alias: ['c'],
description: 'Create new branch',
hidden: false,
name: 'create',
run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
// Retrieve the tools we need
const { config, git, helper, npm, parameters, print: { error, info, spin, success, warning }, system, } = toolbox;
// Parse dry-run flag early
const dryRun = parameters.options.dryRun || parameters.options['dry-run'];
// Check git
if (!(yield git.gitInstalled())) {
return;
}
// Load configuration
const ltConfig = config.loadConfig();
const commandBaseBranch = (_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.create) === null || _c === void 0 ? void 0 : _c.base;
const categoryBaseBranch = (_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.baseBranch;
// Load global defaults
const globalBaseBranch = config.getGlobalDefault(ltConfig, 'baseBranch');
// Parse CLI arguments
const { base: cliBaseBranch } = parameters.options;
// Check changes in current branch (reset optional) - skip in dry-run mode
if (!dryRun) {
yield git.askForReset();
}
// Get branch name
const branch = yield helper.getInput(parameters.first, {
name: 'branch name',
showError: true,
});
if (!branch) {
return;
}
// Check if branch already exists
if (yield git.getBranch(branch)) {
error(`Branch ${branch} already exists!`);
return;
}
// Determine base branch with priority: CLI > config > global > interactive
let baseBranch;
// Helper function to validate and use a branch
const tryUseBranch = (branch, source) => __awaiter(void 0, void 0, void 0, function* () {
if (yield git.getBranch(branch)) {
baseBranch = branch;
info(`Using base branch from ${source}: ${branch}`);
return true;
}
info(`Configured base branch "${branch}" not found (${source}), trying next source...`);
return false;
});
if (cliBaseBranch) {
// CLI parameter provided
if (yield git.getBranch(cliBaseBranch)) {
baseBranch = cliBaseBranch;
}
else {
error(`Base branch ${cliBaseBranch} does not exist!`);
return;
}
}
else if (commandBaseBranch) {
// Command-specific config value provided (git.create.base)
if (!(yield tryUseBranch(commandBaseBranch, 'lt.config commands.git.create'))) {
// Fall through to category-level or global
if (categoryBaseBranch && categoryBaseBranch !== commandBaseBranch) {
if (!(yield tryUseBranch(categoryBaseBranch, 'lt.config commands.git'))) {
if (globalBaseBranch && globalBaseBranch !== categoryBaseBranch) {
yield tryUseBranch(globalBaseBranch, 'lt.config defaults');
}
}
}
else if (globalBaseBranch && globalBaseBranch !== commandBaseBranch) {
yield tryUseBranch(globalBaseBranch, 'lt.config defaults');
}
}
}
else if (categoryBaseBranch) {
// Category-level config value provided (git.baseBranch)
if (!(yield tryUseBranch(categoryBaseBranch, 'lt.config commands.git'))) {
// Fall through to global or interactive
if (globalBaseBranch && globalBaseBranch !== categoryBaseBranch) {
yield tryUseBranch(globalBaseBranch, 'lt.config defaults');
}
}
}
else if (globalBaseBranch) {
// Global default value provided
yield tryUseBranch(globalBaseBranch, 'lt.config defaults');
}
// If no baseBranch yet, check parameters or go interactive
if (!baseBranch) {
const paramBaseBranch = parameters.second;
if (paramBaseBranch && (yield git.getBranch(paramBaseBranch))) {
baseBranch = paramBaseBranch;
}
else {
// Interactive mode
baseBranch = yield git.selectBranch({ text: 'Select base branch' });
}
}
if (!baseBranch) {
error('No base branch selected!');
return;
}
// Dry-run mode: show what would happen
if (dryRun) {
warning('DRY-RUN MODE - No changes will be made');
info('');
info('Would create branch:');
info(` - New branch: ${branch}`);
info(` - Base branch: ${baseBranch}`);
info('');
info('Steps that would be executed:');
info(' 1. git fetch');
info(` 2. git checkout ${baseBranch}`);
info(' 3. git pull');
info(` 4. git checkout -b ${branch}`);
info(` 5. ${toolbox.pm.install()}`);
return `dry-run create branch ${branch} from ${baseBranch}`;
}
// Start timer
const timer = system.startTimer();
// Checkout
const createSpin = spin(`Create ${branch}`);
yield system.run('git fetch &&' + `git checkout ${baseBranch} &&` + 'git pull && ' + `git checkout -b ${branch}`);
createSpin.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 created from ${baseBranch} in ${helper.msToMinutesAndSeconds(timer())}m.`);
info('');
// Exit if not running from menu
if (!toolbox.parameters.options.fromGluegunMenu) {
process.exit();
}
// For tests
return `created branch ${branch}`;
}),
};
exports.default = NewCommand;