@notreallyr8/mp3
Version:
CLI music player with playlists, shuffle, multi-format support
88 lines (76 loc) • 2.69 kB
JavaScript
import fs from 'fs';
import path from 'path';
import readlineSync from 'readline-sync';
import { PlaylistPlayer } from '../lib/player.js';
const PLAYLISTS_FILE = path.resolve('./playlists.json');
let playlists = {};
if (fs.existsSync(PLAYLISTS_FILE)) {
playlists = JSON.parse(fs.readFileSync(PLAYLISTS_FILE));
} else {
playlists = {};
}
function savePlaylists() {
fs.writeFileSync(PLAYLISTS_FILE, JSON.stringify(playlists, null, 2));
}
async function main() {
console.log('=== @notreallyr8/mp3 CLI Music Player ===');
while (true) {
console.log('\nPlaylists:');
const keys = Object.keys(playlists);
if (keys.length === 0) {
console.log('No playlists defined. Add one:');
const name = readlineSync.question('Playlist name: ');
const folder = readlineSync.question('Folder path: ');
playlists[name] = folder;
savePlaylists();
continue;
}
keys.forEach((p, i) => {
console.log(`${i + 1}: ${p} -> ${playlists[p]}`);
});
const choice = readlineSync.questionInt(`Choose playlist (1-${keys.length}) or 0 to add new: `);
if (choice === 0) {
const name = readlineSync.question('Playlist name: ');
const folder = readlineSync.question('Folder path: ');
playlists[name] = folder;
savePlaylists();
continue;
}
const playlistName = keys[choice - 1];
const folder = playlists[playlistName];
const player = new PlaylistPlayer(playlistName, folder);
if (!player.loadSongs()) continue;
while (true) {
console.log(`\n[${playlistName}] Commands: play, next, prev, shuffle, list, add, exit`);
const cmd = readlineSync.question('Command: ').toLowerCase();
if (cmd === 'play') {
await player.play();
} else if (cmd === 'next') {
await player.next();
} else if (cmd === 'prev') {
await player.previous();
} else if (cmd === 'shuffle') {
player.toggleShuffle();
} else if (cmd === 'list') {
console.log('Songs in playlist:');
player.songs.forEach((s, i) => {
const currentMark = i === player.currentIndex ? '*' : ' ';
console.log(`${currentMark} ${i + 1}: ${s}`);
});
} else if (cmd === 'add') {
const name = readlineSync.question('New playlist name: ');
const folder = readlineSync.question('Folder path: ');
playlists[name] = folder;
savePlaylists();
console.log(`Added playlist ${name} -> ${folder}`);
} else if (cmd === 'exit') {
player.stop();
break;
} else {
console.log('Unknown command');
}
}
}
}
main().catch(console.error);