UNPKG

git-veil

Version:

A CLI tool for synchronizing development activities to a personal GitHub repository discreetly and confidentially.

170 lines (169 loc) • 7.04 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); exports.handleConfigCommand = exports.setConfig = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const child_process_1 = require("child_process"); const readline = __importStar(require("readline")); // Always use the gitveil.config.json from the module (project root) const CONFIG_PATH = path.resolve(__dirname, '../../gitveil.config.json'); const VALID_KEYS = ['email', 'name', 'targetRepoPath']; function getGitConfig(field) { try { return (0, child_process_1.execSync)(`git config ${field}`).toString().trim(); } catch (_a) { try { return (0, child_process_1.execSync)(`git config --global ${field}`).toString().trim(); } catch (_b) { return null; } } } function promptUser(question, defaultValue) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); return new Promise((resolve) => { const prompt = defaultValue ? `${question} [${defaultValue}]: ` : `${question}: `; rl.question(prompt, (answer) => { rl.close(); resolve(answer.trim() || defaultValue || ''); }); }); } function setConfig(newConfig) { const currentConfig = loadCurrentConfig(); const updatedConfig = Object.assign(Object.assign({}, currentConfig), newConfig); saveConfig(updatedConfig); } exports.setConfig = setConfig; function handleConfigCommand(key, value, options = {}) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { let configUpdate = {}; if (fs.existsSync(CONFIG_PATH)) { configUpdate = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')); } if (options.init) { // Initialize all values from git and current directory with interactive prompts console.log('šŸ”§ GitVeil Configuration Setup'); console.log('Press Enter to use the default value shown in brackets.\n'); // Detect default values const detectedName = (_a = getGitConfig('user.name')) !== null && _a !== void 0 ? _a : 'unknown'; const detectedEmail = (_b = getGitConfig('user.email')) !== null && _b !== void 0 ? _b : 'unknown'; const detectedPath = process.cwd(); // Interactive prompts with detected defaults const name = yield promptUser('Your name', detectedName || undefined); const email = yield promptUser('Your email', detectedEmail || undefined); const targetRepoPath = yield promptUser('Target repository path', detectedPath); // Update config with user responses if (name) configUpdate['name'] = name; if (email) configUpdate['email'] = email; if (targetRepoPath) configUpdate['targetRepoPath'] = targetRepoPath; console.log('\nāœ… Configuration initialized successfully!'); } else if (options.init && key) { if (key === 'targetRepoPath') { configUpdate['targetRepoPath'] = process.cwd(); } else if (key === 'name') { const name = getGitConfig('user.name'); if (name) configUpdate['name'] = name; else console.log('Could not get git user.name'); } else if (key === 'email') { const email = getGitConfig('user.email'); if (email) configUpdate['email'] = email; else console.log('Could not get git user.email'); } else { console.log(`Unknown config key: ${key}`); return; } } else if (key && value) { if (VALID_KEYS.includes(key)) { configUpdate[key] = value; } else { console.log(`Unknown config key: ${key}`); return; } } if (!options.init && !(key && value)) { // Display all content if no arguments if (fs.existsSync(CONFIG_PATH)) { const raw = fs.readFileSync(CONFIG_PATH, 'utf-8'); console.log(raw); } else { console.log('gitveil.config.json not found'); } return; } setConfig(configUpdate); console.log('Config updated:', configUpdate); }); } exports.handleConfigCommand = handleConfigCommand; function loadCurrentConfig() { try { if (fs.existsSync(CONFIG_PATH)) { const raw = fs.readFileSync(CONFIG_PATH, 'utf-8'); return JSON.parse(raw); } } catch (e) { // ignore and use defaults } return { email: '', name: '', targetRepoPath: './records-folder', }; } function saveConfig(config) { fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), 'utf-8'); }