fix(cli): clean up eager loading bug when initializing cli
This commit is contained in:
parent
a443868f8b
commit
3d20361355
5 changed files with 99 additions and 96 deletions
|
@ -3,8 +3,7 @@
|
||||||
import { program } from 'commander';
|
import { program } from 'commander';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import figlet from 'figlet';
|
import figlet from 'figlet';
|
||||||
import { initConfig, loadConfig } from '../lib/config.js';
|
import { loadConfig } from '../lib/config.js';
|
||||||
import { downloadAsset } from '../lib/download.js';
|
|
||||||
import { runRootScript } from '../lib/runScript.js';
|
import { runRootScript } from '../lib/runScript.js';
|
||||||
import { handleExitError } from '../lib/handlers.js';
|
import { handleExitError } from '../lib/handlers.js';
|
||||||
import { runJobsMenu } from '../lib/jobs.js';
|
import { runJobsMenu } from '../lib/jobs.js';
|
||||||
|
@ -14,11 +13,17 @@ process.on('unhandledRejection', (err) => handleExitError(err, 'Unhandled reject
|
||||||
process.on('uncaughtException', (err) => handleExitError(err, 'Uncaught exception'));
|
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.2.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('init').description('Initialize CLI and populate required config.').action(async () => {
|
||||||
|
const { initConfig } = await import('../lib/config.js');
|
||||||
|
await 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);
|
||||||
program.command('jobs').description('Trigger jobs and scripts.').action(runJobsMenu);
|
program.command('jobs').description('Trigger jobs and scripts.').action(runJobsMenu);
|
||||||
program.command('download').description('Download, name and store image assets.').action(downloadAsset);
|
program.command('download').description('Download, name and store image assets.').action(async () => {
|
||||||
|
const { downloadAsset } = await import('../lib/download.js');
|
||||||
|
await downloadAsset();
|
||||||
|
});
|
||||||
|
|
||||||
if (process.argv.length <= 2) {
|
if (process.argv.length <= 2) {
|
||||||
const ascii = figlet.textSync('coryd.dev', { horizontalLayout: 'default' });
|
const ascii = figlet.textSync('coryd.dev', { horizontalLayout: 'default' });
|
||||||
|
|
|
@ -5,7 +5,8 @@ import { fileURLToPath } from 'url';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const CACHE_DIR = path.resolve(__dirname, '..', '.cache');
|
const rootDir = path.resolve(__dirname, '..');
|
||||||
|
const CACHE_DIR = path.join(rootDir, '.cache');
|
||||||
const CONFIG_PATH = path.join(CACHE_DIR, 'config.json');
|
const CONFIG_PATH = path.join(CACHE_DIR, 'config.json');
|
||||||
const MEDIA_TYPES = ['movie', 'show'];
|
const MEDIA_TYPES = ['movie', 'show'];
|
||||||
const ASSET_TYPES = ['poster', 'backdrop'];
|
const ASSET_TYPES = ['poster', 'backdrop'];
|
||||||
|
@ -13,10 +14,7 @@ const ASSET_TYPES = ['poster', 'backdrop'];
|
||||||
dotenv.config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
dotenv.config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
||||||
|
|
||||||
export const initConfig = async () => {
|
export const initConfig = async () => {
|
||||||
const existingConfig = await fs.pathExists(CONFIG_PATH)
|
const existingConfig = await fs.pathExists(CONFIG_PATH) ? await fs.readJson(CONFIG_PATH) : {};
|
||||||
? await fs.readJson(CONFIG_PATH)
|
|
||||||
: {};
|
|
||||||
|
|
||||||
const config = { ...existingConfig };
|
const config = { ...existingConfig };
|
||||||
|
|
||||||
if (config.storageDir) {
|
if (config.storageDir) {
|
||||||
|
@ -153,7 +151,7 @@ export const loadConfig = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
return await fs.readJson(CONFIG_PATH);
|
return await fs.readJson(CONFIG_PATH);
|
||||||
}
|
};
|
||||||
|
|
||||||
const fetchGlobals = async () => {
|
const fetchGlobals = async () => {
|
||||||
const POSTGREST_URL = process.env.POSTGREST_URL;
|
const POSTGREST_URL = process.env.POSTGREST_URL;
|
||||||
|
|
166
cli/lib/jobs.js
166
cli/lib/jobs.js
|
@ -4,96 +4,96 @@ import { fileURLToPath } from 'url';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { loadConfig } from './config.js';
|
import { loadConfig } from './config.js';
|
||||||
|
|
||||||
const config = await loadConfig();
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
dotenv.config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
dotenv.config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
||||||
|
|
||||||
const JOBS = [
|
export const runJobsMenu = async () => {
|
||||||
{
|
const config = await loadConfig();
|
||||||
name: '🛠 Rebuild site',
|
const JOBS = [
|
||||||
type: 'curl',
|
{
|
||||||
urlEnvVar: 'SITE_REBUILD_WEBHOOK',
|
name: '🛠 Rebuild site',
|
||||||
tokenEnvVar: 'DIRECTUS_API_TOKEN',
|
type: 'curl',
|
||||||
method: 'GET'
|
urlEnvVar: 'SITE_REBUILD_WEBHOOK',
|
||||||
},
|
tokenEnvVar: 'DIRECTUS_API_TOKEN',
|
||||||
{
|
method: 'GET'
|
||||||
name: '💿 Scrobble listens from Navidrome',
|
|
||||||
type: 'curl',
|
|
||||||
apiUrl: `${config.globals.url}/api/scrobble.php`,
|
|
||||||
tokenEnvVar: 'NAVIDROME_SCROBBLE_TOKEN',
|
|
||||||
method: 'POST'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '🎧 Update total plays',
|
|
||||||
type: 'curl',
|
|
||||||
urlEnvVar: 'TOTAL_PLAYS_WEBHOOK',
|
|
||||||
tokenEnvVar: 'DIRECTUS_API_TOKEN',
|
|
||||||
method: 'GET'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '🐘 Send posts to Mastodon',
|
|
||||||
type: 'curl',
|
|
||||||
apiUrl: `${config.globals.url}/api/mastodon.php`,
|
|
||||||
tokenEnvVar: 'MASTODON_SYNDICATION_TOKEN',
|
|
||||||
method: 'POST'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '🎤 Import artist from Navidrome',
|
|
||||||
type: 'curl',
|
|
||||||
apiUrl: `${config.globals.url}/api/artist-import.php`,
|
|
||||||
tokenEnvVar: 'ARTIST_IMPORT_TOKEN',
|
|
||||||
method: 'POST',
|
|
||||||
paramsPrompt: [{
|
|
||||||
type: 'input',
|
|
||||||
name: 'artistId',
|
|
||||||
message: 'Enter the Navidrome artist ID:',
|
|
||||||
validate: input => input ? true : 'Artist ID is required'
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '📖 Import book',
|
|
||||||
type: 'curl',
|
|
||||||
apiUrl: `${config.globals.url}/api/book-import.php`,
|
|
||||||
tokenEnvVar: 'BOOK_IMPORT_TOKEN',
|
|
||||||
method: 'POST',
|
|
||||||
paramsPrompt: [{
|
|
||||||
type: 'input',
|
|
||||||
name: 'isbn',
|
|
||||||
message: 'Enter the book\'s ISBN:',
|
|
||||||
validate: input => input ? true : 'ISBN is required'
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '📽 Import movie or show',
|
|
||||||
type: 'curl',
|
|
||||||
apiUrl: `${config.globals.url}/api/watching-import.php`,
|
|
||||||
tokenEnvVar: 'WATCHING_IMPORT_TOKEN',
|
|
||||||
method: 'POST',
|
|
||||||
tokenIncludeInParams: true,
|
|
||||||
paramsPrompt: [{
|
|
||||||
type: 'input',
|
|
||||||
name: 'tmdb_id',
|
|
||||||
message: 'Enter the TMDB ID:',
|
|
||||||
validate: (input) =>
|
|
||||||
/^\d+$/.test(input) ? true : 'Please enter a valid TMDB ID'
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'list',
|
name: '💿 Scrobble listens from Navidrome',
|
||||||
name: 'media_type',
|
type: 'curl',
|
||||||
message: 'Is this a movie or a show?',
|
apiUrl: `${config.globals.url}/api/scrobble.php`,
|
||||||
choices: ['movie', 'show']
|
tokenEnvVar: 'NAVIDROME_SCROBBLE_TOKEN',
|
||||||
}]
|
method: 'POST'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '📺 Import upcoming TV seasons',
|
name: '🎧 Update total plays',
|
||||||
type: 'curl',
|
type: 'curl',
|
||||||
apiUrl: `${config.globals.url}/api/seasons-import.php`,
|
urlEnvVar: 'TOTAL_PLAYS_WEBHOOK',
|
||||||
tokenEnvVar: 'SEASONS_IMPORT_TOKEN',
|
tokenEnvVar: 'DIRECTUS_API_TOKEN',
|
||||||
method: 'POST'
|
method: 'GET'
|
||||||
}];
|
},
|
||||||
|
{
|
||||||
|
name: '🐘 Send posts to Mastodon',
|
||||||
|
type: 'curl',
|
||||||
|
apiUrl: `${config.globals.url}/api/mastodon.php`,
|
||||||
|
tokenEnvVar: 'MASTODON_SYNDICATION_TOKEN',
|
||||||
|
method: 'POST'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '🎤 Import artist from Navidrome',
|
||||||
|
type: 'curl',
|
||||||
|
apiUrl: `${config.globals.url}/api/artist-import.php`,
|
||||||
|
tokenEnvVar: 'ARTIST_IMPORT_TOKEN',
|
||||||
|
method: 'POST',
|
||||||
|
paramsPrompt: [{
|
||||||
|
type: 'input',
|
||||||
|
name: 'artistId',
|
||||||
|
message: 'Enter the Navidrome artist ID:',
|
||||||
|
validate: input => input ? true : 'Artist ID is required'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '📖 Import book',
|
||||||
|
type: 'curl',
|
||||||
|
apiUrl: `${config.globals.url}/api/book-import.php`,
|
||||||
|
tokenEnvVar: 'BOOK_IMPORT_TOKEN',
|
||||||
|
method: 'POST',
|
||||||
|
paramsPrompt: [{
|
||||||
|
type: 'input',
|
||||||
|
name: 'isbn',
|
||||||
|
message: 'Enter the book\'s ISBN:',
|
||||||
|
validate: input => input ? true : 'ISBN is required'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '📽 Import movie or show',
|
||||||
|
type: 'curl',
|
||||||
|
apiUrl: `${config.globals.url}/api/watching-import.php`,
|
||||||
|
tokenEnvVar: 'WATCHING_IMPORT_TOKEN',
|
||||||
|
method: 'POST',
|
||||||
|
tokenIncludeInParams: true,
|
||||||
|
paramsPrompt: [{
|
||||||
|
type: 'input',
|
||||||
|
name: 'tmdb_id',
|
||||||
|
message: 'Enter the TMDB ID:',
|
||||||
|
validate: (input) =>
|
||||||
|
/^\d+$/.test(input) ? true : 'Please enter a valid TMDB ID'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'list',
|
||||||
|
name: 'media_type',
|
||||||
|
message: 'Is this a movie or a show?',
|
||||||
|
choices: ['movie', 'show']
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '📺 Import upcoming TV seasons',
|
||||||
|
type: 'curl',
|
||||||
|
apiUrl: `${config.globals.url}/api/seasons-import.php`,
|
||||||
|
tokenEnvVar: 'SEASONS_IMPORT_TOKEN',
|
||||||
|
method: 'POST'
|
||||||
|
}];
|
||||||
|
|
||||||
export const runJobsMenu = async () => {
|
|
||||||
const { selectedJob } = await inquirer.prompt([{
|
const { selectedJob } = await inquirer.prompt([{
|
||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'selectedJob',
|
name: 'selectedJob',
|
||||||
|
|
4
cli/package-lock.json
generated
4
cli/package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd",
|
"name": "coryd",
|
||||||
"version": "3.2.2",
|
"version": "3.2.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd",
|
"name": "coryd",
|
||||||
"version": "3.2.2",
|
"version": "3.2.3",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@directus/sdk": "^19.1.0",
|
"@directus/sdk": "^19.1.0",
|
||||||
"chalk": "^5.4.1",
|
"chalk": "^5.4.1",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd",
|
"name": "coryd",
|
||||||
"version": "3.2.2",
|
"version": "3.2.3",
|
||||||
"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": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue