« get me outta code hell

Store options in playlists (e.g. --downloader local) - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-26 13:16:33 -0300
committerliam4 <towerofnix@gmail.com>2017-06-26 13:16:33 -0300
commita10e44d9461adaaf72ac1f6fe51b4efa5f07b244 (patch)
tree0cedaf20c5add88b4963d72fb79f549a4713dd0b
parent44ae051daa38d5b1ec57b9da0c4891bb8794331b (diff)
Store options in playlists (e.g. --downloader local)
-rwxr-xr-xsrc/http-music.js37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/http-music.js b/src/http-music.js
index 494ec17..6abda75 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -48,8 +48,41 @@ Promise.resolve()
       }
 
       const openedPlaylist = JSON.parse(playlistText)
-      sourcePlaylist = openedPlaylist
-      activePlaylist = openedPlaylist
+
+      // Playlists can be in two formats...
+      if (Array.isArray(openedPlaylist)) {
+        // ..the first, a simple array of tracks and groups;
+
+        sourcePlaylist = openedPlaylist
+        activePlaylist = openedPlaylist
+      } else if (typeof openedPlaylist === 'object') {
+        // ..or an object including metadata and configuration as well as the
+        // array described in the first.
+
+        if (!('tracks' in openedPlaylist)) {
+          throw new Error(
+            "Trackless object-type playlist (requires 'tracks' property)"
+          )
+        }
+
+        sourcePlaylist = openedPlaylist.tracks
+        activePlaylist = openedPlaylist.tracks
+
+        // What's handy about the object-type playlist is that you can pass
+        // options that will be run every time the playlist is opened:
+        if ('options' in openedPlaylist) {
+          if (Array.isArray(openedPlaylist.options)) {
+            processArgv(openedPlaylist.options, optionFunctions)
+          } else {
+            throw new Error(
+              "Invalid 'options' property (expected array): " + file
+            )
+          }
+        }
+      } else {
+        // Otherwise something's gone horribly wrong..!
+        throw new Error("Invalid playlist file contents: " + file)
+      }
     }
 
     function requiresOpenPlaylist() {