import { Command } from 'commander'; import { checkbox } from '@inquirer/prompts'; import { getAllPlaylistMetas, getPlaylistSongs } from './lib/services/jellyfin.ts'; import type { Playlist } from './lib/services/jellyfin.ts'; import { downloadsFromPlaylists, downloadSongs } from './lib/fileDownloader.ts'; import { writeM3UFile } from './lib/m3uWriter.ts'; import 'dotenv/config'; const program = new Command(); const { JF_SERVER, JF_USERNAME, JF_PASSWORD, DEST_FOLDER, REMOTE_ROOT_PATH } = process.env; program .name('') .description('CLI to download Jellyfin Playlists to mp3 player m3u format.') .version('0.0.1'); program .command('wizard') .description('Start the CLI wizzard to select playlists and start the downloads') .action(async (str, options) => { console.log('Fetching playlist information...'); if (!JF_SERVER) { console.error('Missing required env var JF_SERVER. Exiting...'); process.exit(); } if (!JF_USERNAME) { console.error('Missing required env var JF_USERNAME. Exiting...'); process.exit(); } if (!JF_PASSWORD) { console.error('Missing required env var JF_PASSWORD. Exiting...'); process.exit(); } if (!DEST_FOLDER) { console.error('Missing required env var DEST_FOLDER. Exiting...'); process.exit(); } //TODO: Get this from the library path?? if (!REMOTE_ROOT_PATH) { console.error('Missing required env var REMOTE_ROOT_PATH. Exiting...'); process.exit(); } const creds = { server: JF_SERVER, user: JF_USERNAME, password: JF_PASSWORD, }; const playlistMetas = await getAllPlaylistMetas(creds); const answer = await checkbox({ message: 'Which playlists would you like to download?', choices: playlistMetas.map((pl: Playlist) => ({ name: pl.Name, value: pl.Id })), }); const playlists = await getPlaylistSongs(creds, answer, playlistMetas); console.log('Downloading songs to ', DEST_FOLDER); const songsToDownload = downloadsFromPlaylists(playlists); await downloadSongs(creds, songsToDownload, DEST_FOLDER); await Promise.all(playlists.map(pl => writeM3UFile(DEST_FOLDER, pl))); }); await program.parseAsync(process.argv);