« get me outta code hell

Gracefully exit if the active playlist has no items - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-11-24 23:07:11 -0400
committerFlorrie <towerofnix@gmail.com>2017-11-24 23:07:11 -0400
commitb11539dadd64cf283eb056414df383cfa15f7b3d (patch)
treea587f50cea9744e6d7c6f62f39a38314dabcb22b
parentb7e52919ebbf0b5c9b1772682e2d63c2e9aeb831 (diff)
Gracefully exit if the active playlist has no items
-rwxr-xr-xsrc/play.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/play.js b/src/play.js
index ff9e76a..c910b08 100755
--- a/src/play.js
+++ b/src/play.js
@@ -15,7 +15,8 @@ const processSmartPlaylist = require('./smart-playlist')
 
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString,
-  updatePlaylistFormat, collapseGrouplike, filterGrouplikeByProperty, isTrack
+  updatePlaylistFormat, collapseGrouplike, filterGrouplikeByProperty, isTrack,
+  flattenGrouplike
 } = require('./playlist-utils')
 
 const {
@@ -609,6 +610,22 @@ async function main(args) {
   }
 
   if (willPlay || (willPlay === null && shouldPlay)) {
+    // Quick and simple test - if there are no items in the playlist, don't
+    // continue. This is mainly to catch incomplete user-entered commands
+    // (like `http-music play -c`).
+    if (flattenGrouplike(activePlaylist).items.length === 0) {
+      console.error(
+        'Your playlist doesn\'t have any tracks in it, so it can\'t be ' +
+        'played.'
+      )
+      console.error(
+        '(Make sure your http-music command doesn\'t have any typos ' +
+        'and isn\'t incomplete? You might have used -c or --clear but not ' +
+        '--keep to actually pick tracks to play!)'
+      )
+      return false
+    }
+
     console.log(`Using sort: ${pickerSortMode} and loop: ${pickerLoopMode}.`)
     console.log(`Using ${playerCommand} player.`)
     console.log(`Using ${converterCommand} converter.`)