feat(cli): prompt to create author if none found when adding link

This commit is contained in:
Cory Dransfeldt 2025-06-08 19:59:30 -07:00
parent 91621b120a
commit 328464670d
No known key found for this signature in database
5 changed files with 56 additions and 22 deletions

View file

@ -4,10 +4,10 @@ import { initDirectusClient, searchItems, createItem } from '../directus/client.
export const addLinkToShare = async () => {
const config = await loadConfig();
initDirectusClient(config);
const { title, link, description, authorQuery } = await inquirer.prompt([{
const { title, link, description, authorQuery } = await inquirer.prompt([
{
name: 'title',
message: '📝 Title for the link:',
validate: input => !!input || 'Title is required'
@ -25,23 +25,57 @@ export const addLinkToShare = async () => {
{
name: 'authorQuery',
message: '👤 Search for an author:',
}]);
}
]);
const authorMatches = await searchItems('authors', authorQuery);
if (!authorMatches.length) {
console.log('❌ No matching authors found.');
return;
}
let author;
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,
}))
});
if (!authorMatches.length) {
const { shouldCreate } = await inquirer.prompt({
type: 'confirm',
name: 'shouldCreate',
message: '❌ No authors found. Do you want to create a new one?',
default: true,
});
if (!shouldCreate) return;
const { name, url, mastodon, rss, json, newsletter, blogroll } = await inquirer.prompt([
{ name: 'name', message: '👤 Author name:', validate: input => !!input || 'Name is required' },
{ name: 'url', message: '🔗 URL (optional):', default: '' },
{ name: 'mastodon', message: '🐘 Mastodon handle (optional):', default: '' },
{ name: 'rss', message: '📡 RSS feed (optional):', default: '' },
{ name: 'json', message: '🧾 JSON feed (optional):', default: '' },
{ name: 'newsletter', message: '📰 Newsletter URL (optional):', default: '' },
{ type: 'confirm', name: 'blogroll', message: '📌 Add to blogroll?', default: false }
]);
const created = await createItem('authors', {
name,
url,
mastodon,
rss,
json,
newsletter,
blogroll
});
author = created.data?.id || created.id;
} else {
const response = await inquirer.prompt({
type: 'list',
name: 'author',
message: 'Select an author:',
choices: authorMatches.map(a => ({
name: a.name || a.id,
value: a.id,
}))
});
author = response.author;
}
let tagIds = [];
@ -50,8 +84,8 @@ export const addLinkToShare = async () => {
name: 'query',
message: '🏷 Search for tags (or leave blank to finish):',
});
const trimmedQuery = query.trim();
const trimmedQuery = query.trim();
if (!trimmedQuery) break;
const tags = await searchItems('tags', trimmedQuery);

View file

@ -18,7 +18,7 @@ export const runTasksMenu = async () => {
{
type: 'list',
name: 'task',
message: 'Select a task to run:',
message: 'Select a task to perform:',
choices: TASKS.map(t => ({ name: t.name, value: t.handler }))
}
]);