import inquirer from "inquirer"; import { loadConfig } from "../config.js"; import { initDirectusClient, searchItems, createItem } from "../directus/client.js"; import { removeUrlProtocol } from "../sanitize.js"; import { promptForTags } from "../directus/tagHelpers.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); let author; 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) => { const cleanUrl = removeUrlProtocol(a.url); const display = cleanUrl ? `${a.name} (${cleanUrl})` : a.name; return { name: display, value: a.id }; }) }); author = response.author; } const tagIds = await promptForTags(); await createItem("links", { title, link, description, author, link_tags: tagIds.map((tagId) => ({ tags_id: tagId })), date: new Date().toISOString() }); console.log("โœ… Link created successfully."); };