UNPKG

use-multiple-gits

Version:

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

84 lines 3.32 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPublicKey = exports.generateSSHKey = void 0; const child_process_1 = require("child_process"); const path = __importStar(require("path")); const util_1 = require("util"); const errors_1 = require("./errors"); const paths_1 = require("./paths"); const execAsync = (0, util_1.promisify)(child_process_1.exec); const generateSSHKey = async (options) => { const { email, keyName, keyType = 'ed25519', keySize = 4096, passphrase } = options; const keyPath = path.join(paths_1.SSH_DIR, keyName); const publicKeyPath = `${keyPath}.pub`; // Check if key already exists const fs = require('fs'); if (fs.existsSync(keyPath)) { throw new errors_1.SSHKeyNotFoundError(`SSH key already exists at ${keyPath}. Use a different name.`); } try { let command; if (keyType === 'ed25519') { command = `ssh-keygen -t ed25519 -C "${email}" -f "${keyPath}" -N "${passphrase || ''}"`; } else { command = `ssh-keygen -t rsa -b ${keySize} -C "${email}" -f "${keyPath}" -N "${passphrase || ''}"`; } await execAsync(command); // Verify key was created if (!fs.existsSync(keyPath) || !fs.existsSync(publicKeyPath)) { throw new Error('SSH key generation failed'); } return keyPath; } catch (error) { throw new Error(`Failed to generate SSH key: ${error.message}`); } }; exports.generateSSHKey = generateSSHKey; const getPublicKey = async (keyPath) => { const fs = require('fs').promises; const publicKeyPath = `${keyPath}.pub`; try { const content = await fs.readFile(publicKeyPath, 'utf-8'); return content.trim(); } catch (error) { throw new Error(`Failed to read public key: ${error.message}`); } }; exports.getPublicKey = getPublicKey; //# sourceMappingURL=sshKeyGenerator.js.map