feat(cli): add support for repeated directus tasks
This commit is contained in:
parent
8a12e83b13
commit
1f9e2d856f
11 changed files with 601 additions and 5 deletions
80
cli/lib/tasks/addEpisodeToShow.js
Normal file
80
cli/lib/tasks/addEpisodeToShow.js
Normal file
|
@ -0,0 +1,80 @@
|
|||
import inquirer from 'inquirer';
|
||||
import { loadConfig } from '../config.js';
|
||||
import { initDirectusClient, getDirectusClient, searchItems, createItem, updateItem } from '../directus/client.js';
|
||||
|
||||
export const addEpisodeToShow = async () => {
|
||||
const config = await loadConfig();
|
||||
|
||||
initDirectusClient(config);
|
||||
|
||||
const directus = getDirectusClient();
|
||||
const showResults = await inquirer.prompt({
|
||||
name: 'query',
|
||||
message: 'Search for a show:',
|
||||
});
|
||||
const matches = await searchItems('shows', showResults.query);
|
||||
|
||||
if (!matches.length) {
|
||||
console.warn('⚠️ No matching shows found.');
|
||||
return;
|
||||
}
|
||||
|
||||
const { showId } = await inquirer.prompt({
|
||||
type: 'list',
|
||||
name: 'showId',
|
||||
message: 'Select a show:',
|
||||
choices: matches.map(s => ({
|
||||
name: s.title || s.name || s.id,
|
||||
value: s.id,
|
||||
})),
|
||||
});
|
||||
const { season_number, episode_number, plays } = await inquirer.prompt([
|
||||
{
|
||||
name: 'season_number',
|
||||
message: 'Season number:',
|
||||
validate: val => !isNaN(val),
|
||||
},
|
||||
{
|
||||
name: 'episode_number',
|
||||
message: 'Episode number:',
|
||||
validate: val => !isNaN(val),
|
||||
},
|
||||
{
|
||||
name: 'plays',
|
||||
message: 'Play count:',
|
||||
default: 0,
|
||||
validate: val => !isNaN(val),
|
||||
},
|
||||
]);
|
||||
const existing = await searchItems('episodes', `${season_number} ${episode_number}`);
|
||||
const match = existing.find(e =>
|
||||
Number(e.season_number) === Number(season_number) &&
|
||||
Number(e.episode_number) === Number(episode_number) &&
|
||||
e.show === showId
|
||||
);
|
||||
|
||||
if (match) {
|
||||
const { update } = await inquirer.prompt({
|
||||
type: 'confirm',
|
||||
name: 'update',
|
||||
message: `Episode exists. Update play count from ${match.plays ?? 0} to ${plays}?`,
|
||||
default: true,
|
||||
});
|
||||
|
||||
if (update) {
|
||||
await updateItem('episodes', match.id, { plays });
|
||||
console.log(`✅ Updated episode: S${season_number}E${episode_number}`);
|
||||
} else {
|
||||
console.warn('⚠️ Skipped update.');
|
||||
}
|
||||
} else {
|
||||
await createItem('episodes', {
|
||||
season_number: Number(season_number),
|
||||
episode_number: Number(episode_number),
|
||||
plays: Number(plays),
|
||||
show: showId,
|
||||
});
|
||||
|
||||
console.log(`📺 Created episode S${season_number}E${episode_number}`);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue