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 233e609155
No known key found for this signature in database
4 changed files with 55 additions and 21 deletions

View file

@ -13,7 +13,7 @@ import { runTasksMenu } from '../lib/tasks/index.js';
process.on('unhandledRejection', (err) => handleExitError(err, 'Unhandled rejection'));
process.on('uncaughtException', (err) => handleExitError(err, 'Uncaught exception'));
program.name('coryd').description('🪄 Handle tasks, run commands or jobs, download things and have fun.').version('3.0.0');
program.name('coryd').description('🪄 Handle tasks, run commands or jobs, download things and have fun.').version('3.2.0');
program.command('init').description('Initialize CLI and populate required config.').action(initConfig);
program.command('run [script]').description('Run site scripts and commands.').action(runRootScript);
program.command('tasks').description('Handle repeated tasks.').action(runTasksMenu);

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,15 +25,46 @@ 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({
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:',
@ -43,6 +74,9 @@ export const addLinkToShare = async () => {
}))
});
author = response.author;
}
let tagIds = [];
while (true) {
@ -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);

4
cli/package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd",
"version": "3.1.0",
"version": "3.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd",
"version": "3.1.0",
"version": "3.2.0",
"dependencies": {
"@directus/sdk": "^19.1.0",
"chalk": "^5.4.1",

View file

@ -1,6 +1,6 @@
{
"name": "coryd",
"version": "3.1.0",
"version": "3.2.0",
"description": "The CLI for my site to run scripts, manage and download assets.",
"type": "module",
"bin": {