« get me outta code hell

Make things work even without a playlist.json - 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-04 16:15:28 -0300
committerliam4 <towerofnix@gmail.com>2017-06-04 16:15:28 -0300
commita067d49f496d5205b02ba1358b124db5d10b3352 (patch)
tree3452b911bc6a8180df9810814a3daf7a85ba5778
parent63622c00b7b4d3dd6c3fcedf71c1cfb72581aa4b (diff)
Make things work even without a playlist.json
-rwxr-xr-xsrc/play.js36
-rw-r--r--todo.txt1
2 files changed, 35 insertions, 2 deletions
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 <file>  (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 <groupPath>  (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 <playlist file>?"
+      )
+    }
+
     if (willPlay || (willPlay === null && shouldPlay)) {
       let picker
       if (pickerType === 'shuffle') {
diff --git a/todo.txt b/todo.txt
index be258a0..29bdda0 100644
--- a/todo.txt
+++ b/todo.txt
@@ -79,3 +79,4 @@ TODO: Recursive local file playlist crawler.
 
 TODO: *Requiring* a literal `playlist.json` file doesn't seem quite right,
       especially since there's the `--open` option.
+      (Done!)