UNPKG

gh-legacy

Version:

A powerful GitHub CLI tool for automatic repository ownership transfer to trusted beneficiaries after periods of inactivity

99 lines (86 loc) • 3.83 kB
// commands/list.js const fs = require('fs'); const path = require('path'); const CONFIG_PATH = path.join(__dirname, '../db.json'); function formatTimeRemaining(milliseconds) { const seconds = Math.floor(milliseconds / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); const months = Math.floor(days / 30); const years = Math.floor(months / 12); if (years > 0) return `${years} year${years > 1 ? 's' : ''}`; if (months > 0) return `${months} month${months > 1 ? 's' : ''}`; if (days > 0) return `${days} day${days > 1 ? 's' : ''}`; if (hours > 0) return `${hours} hour${hours > 1 ? 's' : ''}`; if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''}`; return `${seconds} second${seconds > 1 ? 's' : ''}`; } exports.listBeneficiaries = () => { if (!fs.existsSync(CONFIG_PATH)) { console.error('āŒ No configuration found. Run "gh-legacy init" first.'); return; } const config = JSON.parse(fs.readFileSync(CONFIG_PATH)); const now = Date.now(); const lastHeartbeat = new Date(config.lastHeartbeat).getTime(); const timeSinceHeartbeat = now - lastHeartbeat; console.log('\nšŸ“Š GH-Legacy Configuration'); console.log('='.repeat(50)); console.log(`ā° Last Heartbeat: ${new Date(config.lastHeartbeat).toLocaleString()}`); console.log(`ā±ļø Time Since Last Heartbeat: ${formatTimeRemaining(timeSinceHeartbeat)}`); console.log(`šŸ‘„ Total Beneficiaries: ${config.beneficiaries.length}`); console.log(''); if (config.beneficiaries.length === 0) { console.log('šŸ“ No beneficiaries configured yet.'); console.log('šŸ’” Use "gh-legacy add-beneficiary" to add beneficiaries.'); return; } config.beneficiaries.forEach((b, index) => { console.log(`\n${index + 1}. šŸ“‹ Beneficiary Details`); console.log('─'.repeat(40)); console.log(`šŸ—ļø Repository: ${b.repo}`); console.log(`šŸ‘¤ GitHub Username: ${b.githubUsername}`); console.log(`šŸ“§ Email: ${b.email}`); console.log(`ā³ Access After: ${b.accessAfter}`); console.log(`šŸ“… Added: ${new Date(b.addedAt).toLocaleDateString()}`); if (b.granted) { console.log(`āœ… Status: ACCESS GRANTED`); console.log(`šŸŽÆ Granted: ${new Date(b.grantedAt).toLocaleString()}`); } else { // Calculate if access should be granted const accessDelay = parseDuration(b.accessAfter); const shouldGrant = timeSinceHeartbeat >= accessDelay; if (shouldGrant) { console.log(`🚨 Status: READY FOR ACCESS (conditions met!)`); console.log(`šŸ’” Run "gh-legacy trigger-access" to grant access`); } else { const remaining = accessDelay - timeSinceHeartbeat; console.log(`ā³ Status: WAITING (${formatTimeRemaining(remaining)} remaining)`); } } }); console.log('\nšŸ’” Commands:'); console.log(' gh-legacy heartbeat - Send a heartbeat'); console.log(' gh-legacy trigger-access - Check and grant access'); console.log(' gh-legacy add-beneficiary - Add new beneficiary'); }; function parseDuration(durationStr) { const [value, unit] = durationStr.toLowerCase().split(' '); const num = parseInt(value); switch (unit) { case 'minutes': case 'minute': return num * 60 * 1000; case 'hours': case 'hour': return num * 60 * 60 * 1000; case 'days': case 'day': return num * 24 * 60 * 60 * 1000; case 'weeks': case 'week': return num * 7 * 24 * 60 * 60 * 1000; case 'months': case 'month': return num * 30 * 24 * 60 * 60 * 1000; case 'years': case 'year': return num * 365 * 24 * 60 * 60 * 1000; default: return 0; } }