gitsu-cli
Version:
Interactive command line util for quickly & easily switching git users
62 lines (61 loc) • 3.09 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const log_symbols_1 = __importDefault(require("log-symbols"));
const bootstrap_config_1 = __importDefault(require("../functions/bootstrap-config"));
const get_current_choice_1 = __importDefault(require("../functions/get-current-choice"));
const save_git_config_1 = __importDefault(require("../functions/save-git-config"));
const choice_1 = require("../utils/choice");
const get_config_1 = __importDefault(require("../utils/get-config"));
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
// Ensure ~/.gitsurc config exists
yield (0, bootstrap_config_1.default)();
// Fetch configured gitsu users from config
const configResult = yield (0, get_config_1.default)();
const users = configResult.config;
// Prompt for new user defaulting to the next choice in the list (maybe in the future we can sort by most recently used)
let answers;
try {
const currentChoice = yield (0, get_current_choice_1.default)();
answers = yield inquirer_1.default.prompt([
{
type: 'list',
name: 'user',
message: 'Which user would you like to use?',
choices: () => users.map((user) => (0, choice_1.formatChoice)(user)),
default: (0, choice_1.defaultChoice)(users, currentChoice || users[0]),
},
]);
}
catch (error) {
console.log(`\n${log_symbols_1.default.error} ${chalk_1.default.red('Switch aborted by user')}`);
}
// Save new user to git config
try {
if (!answers) {
return;
}
const user = (0, choice_1.parseChoice)(answers.user);
const saved = yield (0, save_git_config_1.default)(user);
if (!saved) {
throw new Error('Failed to save git config');
}
console.log(`${log_symbols_1.default.success} Switched to user ${chalk_1.default.cyan((0, choice_1.formatChoice)(user))}`);
}
catch (error) {
console.log(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to switch user')}`, error);
}
});