import inquirer from 'inquirer'; import { loadConfig } from '../config.js'; import { initDirectusClient, searchItems, createItem } from '../directus/client.js'; export const addLinkToShare = async () => { const config = await loadConfig(); initDirectusClient(config); const { title, link, description, authorQuery } = await inquirer.prompt([{ name: 'title', message: '📝 Title for the link:', validate: input => !!input || 'Title is required' }, { name: 'link', message: '🔗 URL to share:', validate: input => input.startsWith('http') || 'Must be a valid URL' }, { name: 'description', message: '🗒 Description (optional):', default: '' }, { name: 'authorQuery', message: '👤 Search for an author:', }]); const authorMatches = await searchItems('authors', authorQuery); if (!authorMatches.length) { console.log('❌ No matching authors found.'); return; } const { author } = await inquirer.prompt({ type: 'list', name: 'author', message: 'Select an author:', choices: authorMatches.map(a => ({ name: a.name || a.id, value: a.id, })) }); let tagIds = []; while (true) { const { query } = await inquirer.prompt({ name: 'query', message: '🏷 Search for tags (or leave blank to finish):', }); const trimmedQuery = query.trim(); if (!trimmedQuery) break; const tags = await searchItems('tags', trimmedQuery); if (!tags.length) { console.warn(`⚠️ No tags found matching "${trimmedQuery}"`); continue; } const { selected } = await inquirer.prompt({ type: 'checkbox', name: 'selected', message: '✔ Select tags to add:', choices: tags.map(tag => ({ name: tag.name, value: tag.id })) }); tagIds.push(...selected); const { again } = await inquirer.prompt({ type: 'confirm', name: 'again', message: 'Search and select more tags?', default: false, }); if (!again) break; } await createItem('links', { title, link, description, author, link_tags: tagIds.map(tagId => ({ tags_id: tagId })), date: new Date().toISOString() }); console.log('✅ Link created successfully.'); };