UNPKG

use-multiple-gits

Version:

CLI tool to manage multiple git configurations (user.name, user.email, SSH keys) with easy switching between identities

82 lines 3.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setLocalCommand = void 0; const chalk_1 = __importDefault(require("chalk")); const child_process_1 = require("child_process"); const util_1 = require("util"); const configStorage_1 = require("../utils/configStorage"); const errors_1 = require("../utils/errors"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const execAsync = (0, util_1.promisify)(child_process_1.exec); const setLocalCommand = async (name) => { try { const config = await (0, configStorage_1.getConfig)(name); if (!config) { throw new errors_1.ConfigNotFoundError(name); } // Check if we're in a git repository const gitDir = path.join(process.cwd(), '.git'); if (!fs.existsSync(gitDir)) { console.error(chalk_1.default.red('\n❌ Not a git repository. Run this command from a git repository root.\n')); process.exit(1); } // Set local git config await execAsync(`git config user.name "${config.userName}"`); await execAsync(`git config user.email "${config.userEmail}"`); // Set SSH key for this repo const sshKeyPath = path.join(require('os').homedir(), '.ssh', config.sshKeyName); await execAsync(`git config core.sshCommand "ssh -i ${sshKeyPath} -F /dev/null"`); console.log(chalk_1.default.green(`\n✅ Set local git config for this repository:\n`)); console.log(chalk_1.default.cyan(` Name: ${config.userName}`)); console.log(chalk_1.default.cyan(` Email: ${config.userEmail}`)); console.log(chalk_1.default.cyan(` SSH Key: ${config.sshKeyName}\n`)); } catch (error) { if (error instanceof errors_1.ConfigNotFoundError) { console.error(chalk_1.default.red(`\n❌ ${error.message}\n`)); } else { console.error(chalk_1.default.red(`\n❌ Error: ${error.message}\n`)); } process.exit(1); } }; exports.setLocalCommand = setLocalCommand; //# sourceMappingURL=setLocal.js.map