UNPKG

logggai

Version:

AI-powered CLI for transforming your development work into professional content

196 lines 8.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listCommand = listCommand; const chalk = require("chalk"); const ora_1 = require("ora"); const date_fns_1 = require("date-fns"); const { isLoggedIn, getCurrentContext, getCurrentOrganizationId, setConfig } = require('../lib/config'); const api_1 = require("../lib/api"); const auth_1 = require("../lib/auth"); // Helper function to synchronize CLI context with server async function synchronizeContextWithServer() { try { const currentContext = getCurrentContext() || 'personal'; const currentOrgId = getCurrentOrganizationId(); // Synchronize with server if (currentContext === 'organization' && currentOrgId) { await api_1.apiClient.switchContext(currentOrgId); } else { await api_1.apiClient.switchContext(); // No orgId = personal context } } catch (error) { if (error?.response?.status === 404 || error?.response?.status === 403) { setConfig('currentContext', 'personal'); setConfig('currentOrganizationId', undefined); console.log(chalk.default.yellow("Contexte d'organisation invalide ou inaccessible, retour au contexte personnel.")); } else { console.log(chalk.default.yellow('Warning: Could not sync context with server, using personal context')); } } } async function listCommand(options) { return (0, auth_1.withAuth)(async () => { await _listCommand(options); }); } async function _listCommand(options) { // Gérer le switch d'organisation si --org est spécifié if (options.org) { await handleOrganizationSwitch(options.org); } // Gestion contextuelle inspirée de la commande context if (getCurrentContext() === 'personal') { setConfig('currentContext', 'personal'); setConfig('currentOrganizationId', undefined); await api_1.apiClient.switchContext(undefined); } else { await synchronizeContextWithServer(); } // Afficher le contexte actuel avec plus de détails await displayCurrentContextStatus(); const limit = parseInt(options.limit || '10', 10); const spinner = (0, ora_1.default)('Fetching articles...').start(); try { const result = await api_1.apiClient.getPosts(limit); const posts = result.posts; spinner.stop(); if (posts.length === 0) { const currentContext = getCurrentContext() || 'personal'; if (currentContext === 'organization') { console.log(chalk.default.yellow('No articles found in this organization')); console.log(chalk.default.gray('Create your first organization article with: logggai post "My title"')); } else { console.log(chalk.default.yellow('No articles found')); console.log(chalk.default.gray('Create your first article with: logggai post "My title"')); } return; } console.log(chalk.default.green(`${posts.length} article(s) found`)); // Afficher info contexte si disponible dans la réponse if (result.context) { if (result.context.type === 'organization') { console.log(chalk.default.gray(`📁 Context: ${result.context.organizationName || 'Organization'}`)); } else { console.log(chalk.default.gray('👤 Context: Personal')); } } console.log(); posts.forEach((post, index) => { const isLast = index === posts.length - 1; const connector = isLast ? '└─' : '├─'; console.log(chalk.default.gray(connector) + chalk.default.bold(` ${post.title}`)); // Métadonnées const metadata = []; metadata.push((0, date_fns_1.formatDistanceToNow)(new Date(post.createdAt), { addSuffix: true })); if (post.aiProcessed) { metadata.push(chalk.default.blue('AI')); } if (post.tags && post.tags.length > 0) { metadata.push(chalk.default.cyan(`#${post.tags.join(' #')}`)); } if (post.prompt) { metadata.push(chalk.default.magenta(`[${post.prompt.name}]`)); } // Afficher si c'est un post d'organisation if (post.organizationId) { metadata.push(chalk.default.yellow('ORG')); } const metaLine = isLast ? ' ' : '│ '; console.log(chalk.default.gray(metaLine + metadata.join(' • '))); // Aperçu du contenu const preview = post.content.length > 100 ? post.content.substring(0, 100) + '...' : post.content; console.log(chalk.default.gray(metaLine + `"${preview}"`)); console.log(chalk.default.gray(metaLine + `ID: ${post.id}`)); if (!isLast) { console.log(chalk.default.gray('│')); } }); console.log(); console.log(chalk.default.gray(`Showing ${posts.length} article(s) of ${limit} requested`)); if (posts.length === limit) { console.log(chalk.default.yellow('Use --limit to see more results')); } } catch (error) { spinner.fail(chalk.default.red('Error fetching articles')); console.log(chalk.default.red(error.message)); } } // Fonction pour gérer le switch d'organisation (similaire à post.ts) async function handleOrganizationSwitch(orgIdentifier) { try { // Support spécial pour --org personal if (orgIdentifier.toLowerCase() === 'personal') { setConfig('currentContext', 'personal'); setConfig('currentOrganizationId', undefined); console.log(chalk.default.blue(`→ Listing from Personal Workspace`)); return; } const spinner = (0, ora_1.default)('Loading organizations...').start(); const userOrgs = await api_1.apiClient.getUserOrganizations(); spinner.stop(); if (!userOrgs.organizations || userOrgs.organizations.length === 0) { console.log(chalk.default.red('No organizations available')); console.log(chalk.default.gray('You need to be invited to an organization first')); console.log(chalk.default.gray('Use --org personal to list from personal workspace')); process.exit(1); } // Chercher l'organisation par nom, slug ou ID const org = userOrgs.organizations.find((o) => o.name.toLowerCase() === orgIdentifier.toLowerCase() || o.slug.toLowerCase() === orgIdentifier.toLowerCase() || o.id === orgIdentifier); if (!org) { console.log(chalk.default.red(`Organization "${orgIdentifier}" not found`)); console.log(chalk.default.gray('\nAvailable organizations:')); userOrgs.organizations.forEach((o) => { console.log(chalk.default.gray(` • ${o.name} (${o.slug})`)); }); console.log(chalk.default.gray(' • personal (Personal Workspace)')); process.exit(1); } // Switch vers l'organisation (temporaire pour cette commande) setConfig('currentContext', 'organization'); setConfig('currentOrganizationId', org.id); console.log(chalk.default.cyan(`→ Listing from ${org.name}`)); } catch (error) { console.log(chalk.default.red(`Failed to switch to organization "${orgIdentifier}"`)); console.log(chalk.default.red(error.message)); process.exit(1); } } // Fonction pour afficher le statut du contexte (similaire à post.ts) async function displayCurrentContextStatus() { const currentContext = getCurrentContext() || 'personal'; const currentOrgId = getCurrentOrganizationId(); try { if (currentContext === 'organization' && currentOrgId) { const contextData = await api_1.apiClient.getCurrentContext(); const orgName = contextData?.organization?.name || 'Unknown Organization'; console.log(chalk.default.cyan(`📁 Listing from: ${orgName} (Organization)`)); } else { console.log(chalk.default.blue('👤 Listing from: Personal Workspace')); } } catch (error) { // Fallback si erreur API if (currentContext === 'organization' && currentOrgId) { console.log(chalk.default.cyan(`📁 Listing from: Organization (${currentOrgId})`)); } else { console.log(chalk.default.blue('👤 Listing from: Personal Workspace')); } } } //# sourceMappingURL=list.js.map