feat(cli): support blocking bots

This commit is contained in:
Cory Dransfeldt 2025-06-08 17:55:22 -07:00
parent 1f9e2d856f
commit 91621b120a
No known key found for this signature in database
5 changed files with 48 additions and 4 deletions

View file

@ -13,7 +13,7 @@ import { runTasksMenu } from '../lib/tasks/index.js';
process.on('unhandledRejection', (err) => handleExitError(err, 'Unhandled rejection')); process.on('unhandledRejection', (err) => handleExitError(err, 'Unhandled rejection'));
process.on('uncaughtException', (err) => handleExitError(err, 'Uncaught exception')); process.on('uncaughtException', (err) => handleExitError(err, 'Uncaught exception'));
program.name('coryd').description('🪄 Run commands, 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.0.0');
program.command('init').description('Initialize CLI and populate required config.').action(initConfig); 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('run [script]').description('Run site scripts and commands.').action(runRootScript);
program.command('tasks').description('Handle repeated tasks.').action(runTasksMenu); program.command('tasks').description('Handle repeated tasks.').action(runTasksMenu);

View file

@ -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 "/"`);
};

View file

@ -3,12 +3,14 @@ import { addPost } from './addPost.js';
import { addLinkToShare } from './addLinkToShare.js'; import { addLinkToShare } from './addLinkToShare.js';
import { addEpisodeToShow } from './addEpisodeToShow.js'; import { addEpisodeToShow } from './addEpisodeToShow.js';
import { updateReadingProgress } from './updateReadingProgress.js'; import { updateReadingProgress } from './updateReadingProgress.js';
import { addBlockedRobot } from './addBlockedRobot.js';
const TASKS = [ const TASKS = [
{ name: '📄 Add post', handler: addPost }, { name: '📄 Add post', handler: addPost },
{ name: '🔗 Add link to share', handler: addLinkToShare }, { name: '🔗 Add link to share', handler: addLinkToShare },
{ name: ' Add episode to show', handler: addEpisodeToShow }, { name: ' Add episode to show', handler: addEpisodeToShow },
{ name: '📚 Update reading progress', handler: updateReadingProgress }, { name: '📚 Update reading progress', handler: updateReadingProgress },
{ name: '🤖 Block robot', handler: addBlockedRobot },
]; ];
export const runTasksMenu = async () => { export const runTasksMenu = async () => {

4
cli/package-lock.json generated
View file

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

View file

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