diff --git a/cli/bin/index.js b/cli/bin/index.js index ccc087c..21ea4d9 100755 --- a/cli/bin/index.js +++ b/cli/bin/index.js @@ -4,7 +4,7 @@ import { program } from "commander"; import chalk from "chalk"; import figlet from "figlet"; import { loadConfig } from "../lib/config.js"; -import { handleExitError } from "../lib/handlers.js"; +import { handleExitError } from "../lib/utils.js"; import { runRootScript } from "../lib/runScript.js"; import { runJobsMenu } from "../lib/jobs.js"; import { runTasksMenu } from "../lib/tasks/index.js"; diff --git a/cli/lib/directus/tagHelpers.js b/cli/lib/directus/tagHelpers.js new file mode 100644 index 0000000..c2cc123 --- /dev/null +++ b/cli/lib/directus/tagHelpers.js @@ -0,0 +1,65 @@ +import inquirer from "inquirer"; +import { searchItems, createItem } from "./client.js"; + +export const promptForTags = async () => { + const 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}"`); + + const { shouldCreateTag } = await inquirer.prompt({ + type: "confirm", + name: "shouldCreateTag", + message: `Do you want to create a new tag named "${trimmedQuery}"?`, + default: true + }); + + if (shouldCreateTag) { + const createdTag = await createItem("tags", { name: trimmedQuery }); + const newTagId = createdTag.data?.id || createdTag.id; + tagIds.push(newTagId); + } + + const { again } = await inquirer.prompt({ + type: "confirm", + name: "again", + message: "Search and select more tags?", + default: false + }); + + if (!again) break; + 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; + } + + return [...new Set(tagIds)]; +}; diff --git a/cli/lib/tasks/addLinkToShare.js b/cli/lib/tasks/addLinkToShare.js index 41852b1..cdee2f0 100644 --- a/cli/lib/tasks/addLinkToShare.js +++ b/cli/lib/tasks/addLinkToShare.js @@ -2,6 +2,7 @@ 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(); @@ -87,66 +88,7 @@ export const addLinkToShare = async () => { author = response.author; } - 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}"`); - - const { shouldCreateTag } = await inquirer.prompt({ - type: "confirm", - name: "shouldCreateTag", - message: `Do you want to create a new tag named "${trimmedQuery}"?`, - default: true - }); - - if (shouldCreateTag) { - const createdTag = await createItem("tags", { name: trimmedQuery }); - const newTagId = createdTag.data?.id || createdTag.id; - - tagIds.push(newTagId); - } - - const { again } = await inquirer.prompt({ - type: "confirm", - name: "again", - message: "Search and select more tags?", - default: false - }); - - if (!again) break; - - 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; - } + const tagIds = await promptForTags(); await createItem("links", { title, diff --git a/cli/lib/tasks/addPost.js b/cli/lib/tasks/addPost.js index 3156d33..7075954 100644 --- a/cli/lib/tasks/addPost.js +++ b/cli/lib/tasks/addPost.js @@ -2,6 +2,7 @@ import inquirer from "inquirer"; import { loadConfig } from "../config.js"; import { initDirectusClient, createItem, searchItems } from "../directus/client.js"; import { promptForMultipleRelations } from "../directus/relationHelpers.js"; +import { promptForTags } from "../directus/tagHelpers.js"; const ASSOCIATED_MEDIA_TYPES = ["artists", "books", "movies", "shows", "genres"]; const BLOCK_COLLECTIONS = [ @@ -42,44 +43,7 @@ export const addPost = async () => { } ]); - 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; - } - + const tagIds = await promptForTags(); const selectedBlocks = []; const { includeBlocks } = await inquirer.prompt({ type: "confirm", diff --git a/cli/lib/handlers.js b/cli/lib/utils.js similarity index 100% rename from cli/lib/handlers.js rename to cli/lib/utils.js diff --git a/cli/package-lock.json b/cli/package-lock.json index e5d857d..bd502c9 100644 --- a/cli/package-lock.json +++ b/cli/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd", - "version": "3.3.0", + "version": "3.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd", - "version": "3.3.0", + "version": "3.4.0", "dependencies": { "@directus/sdk": "^19.1.0", "chalk": "^5.4.1", diff --git a/cli/package.json b/cli/package.json index dd031ac..c62e6c0 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "coryd", - "version": "3.3.0", + "version": "3.4.0", "description": "The CLI for my site to handle tasks, run commands or jobs and download things.", "type": "module", "bin": { diff --git a/package-lock.json b/package-lock.json index f735ae8..82b3e91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "10.6.7", + "version": "10.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "10.6.7", + "version": "10.7.0", "license": "MIT", "dependencies": { "minisearch": "^7.1.2",