From a067d49f496d5205b02ba1358b124db5d10b3352 Mon Sep 17 00:00:00 2001 From: liam4 Date: Sun, 4 Jun 2017 16:15:28 -0300 Subject: Make things work even without a playlist.json --- src/play.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'src/play.js') diff --git a/src/play.js b/src/play.js index bc9324d..9751100 100755 --- a/src/play.js +++ b/src/play.js @@ -17,8 +17,14 @@ const { const readFile = promisify(fs.readFile) -readFile('./playlist.json', 'utf-8') - .then(plText => JSON.parse(plText)) +function setupDefaultPlaylist(file) { + return readFile(file, 'utf-8').then( + text => JSON.parse(text), + err => null + ) +} + +setupDefaultPlaylist('./playlist.json') .then(async playlist => { let sourcePlaylist = playlist let curPlaylist = playlist @@ -33,6 +39,14 @@ readFile('./playlist.json', 'utf-8') let shouldPlay = true let willPlay = null + function requiresOpenPlaylist() { + if (curPlaylist === null) { + throw new Error( + "This action requires an open playlist - try --open (file)" + ) + } + } + await processArgv(process.argv, { '-open': async function(util) { // --open (alias: -o) @@ -52,6 +66,8 @@ readFile('./playlist.json', 'utf-8') // Clears the active playlist. This does not affect the source // playlist. + requiresOpenPlaylist() + curPlaylist = [] }, @@ -64,6 +80,8 @@ readFile('./playlist.json', 'utf-8') // active playlist; it can also be used to keep a subgroup when // you've removed an entire parent group, e.g. `-r foo -k foo/baz`. + requiresOpenPlaylist() + const pathString = util.nextArg() const group = filterPlaylistByPathString(sourcePlaylist, pathString) curPlaylist.push(group) @@ -75,6 +93,8 @@ readFile('./playlist.json', 'utf-8') // --remove (alias: -r, -x) // Filters the playlist so that the given path is removed. + requiresOpenPlaylist() + const pathString = util.nextArg() console.log("Ignoring path: " + pathString) removeGroupByPathString(curPlaylist, pathString) @@ -87,6 +107,8 @@ readFile('./playlist.json', 'utf-8') // --list-groups (alias: -l, --list) // Lists all groups in the playlist. + requiresOpenPlaylist() + console.log(getPlaylistTreeString(curPlaylist)) // If this is the last item in the argument list, the user probably @@ -104,6 +126,8 @@ readFile('./playlist.json', 'utf-8') // --list-all (alias: --list-tracks, -L) // Lists all groups and tracks in the playlist. + requiresOpenPlaylist() + console.log(getPlaylistTreeString(curPlaylist, true)) // As with -l, if this is the last item in the argument list, we @@ -161,10 +185,18 @@ readFile('./playlist.json', 'utf-8') // --debug-list // Prints out the JSON representation of the active playlist. + requiresOpenPlaylist() + console.log(JSON.stringify(curPlaylist, null, 2)) } }) + if (curPlaylist === null) { + throw new Error( + "Cannot play - no open playlist. Try --open ?" + ) + } + if (willPlay || (willPlay === null && shouldPlay)) { let picker if (pickerType === 'shuffle') { -- cgit 1.3.0-6-gf8a5