UNPKG

pssst

Version:

A CLI tool for sharing and discovering developer thoughts from around the world

200 lines (166 loc) 5.36 kB
// lib/github.js const axios = require('axios'); const { execSync } = require('child_process'); function getMessageUrl(lang) { const baseUrl = 'https://raw.githubusercontent.com/yybmion/pssst/main/messages'; switch(lang) { case 'ko': return `${baseUrl}/ko.json`; case 'en': return `${baseUrl}/en.json`; case 'ch': return `${baseUrl}/ch.json`; case 'jp': return `${baseUrl}/jp.json`; case 'all': default: return `${baseUrl}/all.json`; } } // lib/github.js에 시간 함수 추가 function getLocalISOString() { const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); const seconds = String(now.getSeconds()).padStart(2, '0'); const milliseconds = String(now.getMilliseconds()).padStart(3, '0'); return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}Z`; } async function getRandomMessage(lang = 'all') { try { const url = getMessageUrl(lang); const response = await axios.get(url); const data = response.data; if (data.messages && data.messages.length > 0) { const randomIndex = Math.floor(Math.random() * data.messages.length); const message = data.messages[randomIndex]; return { text: message.text, author: message.author, timestamp: message.timestamp, lang: message.lang }; } return `No ${lang} messages available`; } catch (error) { console.error('Error details:', error.message); return `Failed to fetch ${lang} messages`; } } async function getRecentMessages(lang = 'all', count = 10) { try { const url = getMessageUrl(lang); const response = await axios.get(url); const data = response.data; if (data.messages && data.messages.length > 0) { const sortedMessages = data.messages .map(message => ({ text: message.text, author: message.author, timestamp: message.timestamp, lang: message.lang })) .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); return sortedMessages.slice(0, count); } return `No ${lang} messages available`; } catch (error) { console.error('Error details:', error.message); return `Failed to fetch ${lang} messages`; } } function checkGitHubCLI() { try { execSync('gh --version', { encoding: 'utf8', stdio: 'pipe' }); const authStatus = execSync('gh auth status', { encoding: 'utf8', stdio: 'pipe' }); return true; } catch (error) { return false; } } async function getUserInfo() { try { const userInfo = execSync('gh api user', { encoding: 'utf8' }); const user = JSON.parse(userInfo); return user.login; } catch (error) { throw new Error('GitHub CLI authentication failed'); } } async function contributeMessage(messageText, isAnonymous = false) { try { if (messageText.length > 200) { return { success: false, error: `Message too long (${messageText.length}/200 characters). Please keep it under 200 characters.` }; } let displayAuthor = 'anonymous'; if (!isAnonymous) { console.log('Checking GitHub CLI authentication...'); if (!checkGitHubCLI()) { return { success: false, error: ` ❌ GitHub CLI authentication required for non-anonymous messages! Please install and authenticate GitHub CLI: 1. Install GitHub CLI: - Windows: winget install GitHub.cli - macOS: brew install gh - Linux: sudo apt install gh 2. Authenticate: gh auth login 3. Try again or use anonymous mode: pssst send "your message" --anonymous ` }; } try { displayAuthor = await getUserInfo(); console.log(`✅ Authenticated as: ${displayAuthor}`); } catch (error) { return { success: false, error: ` ❌ GitHub CLI authentication failed! Please authenticate with GitHub CLI: gh auth login Or use anonymous mode: pssst send "your message" --anonymous ` }; } } else { console.log('🕶️ Using anonymous mode (no authentication required)'); } const apiUrl = 'https://pssst-bqwlkbrxu-yybmions-projects.vercel.app/api/contribute'; console.log('Calling API:', apiUrl); const response = await axios.post(apiUrl, { message: messageText, isAnonymous: isAnonymous, author: displayAuthor, timestamp: getLocalISOString() }); return { success: true, prUrl: response.data.prUrl, message: response.data.message, language: response.data.language, author: displayAuthor }; } catch (error) { console.error('API Error:', error.response?.data || error.message); return { success: false, error: error.response?.data?.error || `API Error: ${error.message}` }; } } module.exports = { getRandomMessage, getRecentMessages, contributeMessage };