From 1589c4c6fdc67f7b9172d4c8d79e796d71420429 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sun, 8 Jun 2025 17:55:22 -0700 Subject: [PATCH] feat(cli): support blocking bots --- cli/lib/tasks/addBlockedRobot.js | 42 ++++++++++++++++++++++++++++++++ cli/lib/tasks/index.js | 2 ++ cli/package-lock.json | 4 +-- cli/package.json | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 cli/lib/tasks/addBlockedRobot.js diff --git a/cli/lib/tasks/addBlockedRobot.js b/cli/lib/tasks/addBlockedRobot.js new file mode 100644 index 0000000..2fa430e --- /dev/null +++ b/cli/lib/tasks/addBlockedRobot.js @@ -0,0 +1,42 @@ +import inquirer from 'inquirer'; +import { loadConfig } from '../config.js'; +import { initDirectusClient, searchItems, createItem } from '../directus/client.js'; + +export const addBlockedRobot = async () => { + const config = await loadConfig(); + + initDirectusClient(config); + + const robots = await searchItems('robots', '/'); + + let rootRobot = robots.find(r => r.path === '/'); + + if (!rootRobot) { + console.log('â„šī¸ No robots entry for `/` found. Creating one...'); + + const newRobot = await createItem('robots', { path: '/' }); + + rootRobot = newRobot.data || newRobot; + + console.log('✅ Created robots rule for `/`'); + } + + const { userAgent } = await inquirer.prompt({ + name: 'userAgent', + message: '🤖 Enter the user-agent string to block:', + validate: input => !!input || 'User-agent cannot be empty' + }); + + const createdAgent = await createItem('user_agents', { + user_agent: userAgent + }); + + const agentId = createdAgent.data?.id || createdAgent.id; + + await createItem('robots_user_agents', { + robots_id: rootRobot.id, + user_agents_id: agentId + }); + + console.log(`✅ Blocked user-agent "${userAgent}" under path "/"`); +}; diff --git a/cli/lib/tasks/index.js b/cli/lib/tasks/index.js index 2d0a180..7703d40 100644 --- a/cli/lib/tasks/index.js +++ b/cli/lib/tasks/index.js @@ -3,12 +3,14 @@ import { addPost } from './addPost.js'; import { addLinkToShare } from './addLinkToShare.js'; import { addEpisodeToShow } from './addEpisodeToShow.js'; import { updateReadingProgress } from './updateReadingProgress.js'; +import { addBlockedRobot } from './addBlockedRobot.js'; const TASKS = [ { name: '📄 Add post', handler: addPost }, { name: '🔗 Add link to share', handler: addLinkToShare }, { name: '➕ Add episode to show', handler: addEpisodeToShow }, { name: '📚 Update reading progress', handler: updateReadingProgress }, + { name: '🤖 Block robot', handler: addBlockedRobot }, ]; export const runTasksMenu = async () => { diff --git a/cli/package-lock.json b/cli/package-lock.json index 1e780ff..f7c6fdd 100644 --- a/cli/package-lock.json +++ b/cli/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd", - "version": "3.0.0", + "version": "3.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd", - "version": "3.0.0", + "version": "3.1.0", "dependencies": { "@directus/sdk": "^19.1.0", "chalk": "^5.4.1", diff --git a/cli/package.json b/cli/package.json index 66a654e..635af8b 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "coryd", - "version": "3.0.0", + "version": "3.1.0", "description": "The CLI for my site to run scripts, manage and download assets.", "type": "module", "bin": {