UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

159 lines (158 loc) 7.08 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"); /** * Checkout git branch */ const NewCommand = { alias: ['g'], description: 'Checkout branch', hidden: false, name: 'get', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; // Retrieve the tools we need const { config, filesystem, git, helper, npm, parameters, print: { error, info, spin, success }, prompt, system, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); const configMode = (_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.get) === null || _c === void 0 ? void 0 : _c.mode; // Parse CLI arguments const cliMode = parameters.options.mode; // Determine noConfirm with priority: CLI > config > global > default (false) const noConfirm = config.getNoConfirm({ cliValue: parameters.options.noConfirm, commandConfig: (_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.get, config: ltConfig, parentConfig: (_f = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _f === void 0 ? void 0 : _f.git, }); // Start timer const timer = system.startTimer(); // Check git if (!(yield git.gitInstalled())) { return; } // Get (part of) branch name const branchName = yield helper.getInput(parameters.first, { name: 'branch name', showError: true, }); if (!branchName) { return; } // Check changes in current branch (reset necessary) if (!(yield git.askForReset({ showError: true }))) { return; } // Search for branch, which includes branch name const branch = yield git.getBranch(branchName, { error: true, exact: false, remote: false, spin: true, }); if (!branch) { return; } // Get remote branch const remoteBranch = yield git.getBranch(branch, { remote: true }); // Ask for checkout branch if (branchName !== branch) { if (!noConfirm && !(yield prompt.confirm(`Checkout ${remoteBranch ? 'remote' : 'local'} branch ${branch}`))) { return; } } else { success(`Branch ${branchName} found`); } // Checkout branch yield system.run(`git checkout ${(yield git.getDefaultBranch()) || 'main'}`); let checkoutSpin; // Handling for remote if (remoteBranch) { // Delete local let removed = false; const checkSpin = spin('Check status'); if (branch !== 'main' && branch && (yield git.diffFiles(branch, { noDiffResult: '' })).length) { checkSpin.succeed(); // Determine mode with priority: CLI > config > interactive let mode = cliMode || configMode; if (!cliMode && configMode) { info(`Using mode from lt.config commands.git.get: ${configMode}`); } if (!mode && !noConfirm) { if (yield prompt.confirm(`Remove local commits of ${branch}`)) { mode = 'hard'; } } else if (!mode && noConfirm) { // In noConfirm mode without explicit mode, default to not removing mode = undefined; } if (mode === 'hard') { const prepareSpin = spin(`Refresh ${branch}`); yield system.run(`git branch -D ${branch}`); removed = true; prepareSpin.succeed(); } } else { checkSpin.succeed(); } // Start spin checkoutSpin = spin(`Checkout ${branch}`); // Checkout remote if local branch not exists if (removed || !(yield git.getBranch(branch, { local: true }))) { yield system.run(`git fetch && git checkout --track origin/${branch} && git reset --hard && git clean -fd && git pull`); // Checkout local branch } else { yield system.run(`git fetch && git checkout ${branch} && git reset --hard && git clean -fd && git pull`); } // Handling for local only } else if (branch) { checkoutSpin = spin(`Checkout ${branch}`); yield system.run(`git fetch && git checkout ${branch} && git reset --hard && git clean -fd`); // No branch found } else { error(`Branch ${branch} not found!`); return; } // Checkout done checkoutSpin.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(); } // Init lerna projects if (filesystem.isFile('./lerna.json')) { const initProjectsSpin = spin('Init projects'); yield system.run(`${toolbox.pm.run('init')} --if-present`); initProjectsSpin.succeed(); } // Success info success(`${remoteBranch ? 'Remote' : 'Local'} branch ${branch} checked out in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `checked out branch ${branch}`; }), }; exports.default = NewCommand;