« get me outta code hell

Do a proper clone when creating active playlist - 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-07-18 17:18:54 -0300
committerliam4 <towerofnix@gmail.com>2017-07-18 17:18:54 -0300
commit3f7969bcd75a2cdc4c6b2273fa300eabfc6bd9ad (patch)
tree081605cdd1607dd38c389f24e3e8e50b1caa2a78
parentebada93f0d2b2ec3a158dae2ba43e98d81f3182a (diff)
Do a proper clone when creating active playlist
-rw-r--r--package.json1
-rwxr-xr-xsrc/http-music.js32
-rw-r--r--yarn.lock13
3 files changed, 27 insertions, 19 deletions
diff --git a/package.json b/package.json
index 60180a7..e2e06d3 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,7 @@
   ],
   "dependencies": {
     "cheerio": "^1.0.0-rc.1",
+    "clone": "^2.1.1",
     "fifo-js": "^2.1.0",
     "fs-extra": "^3.0.1",
     "ncp": "^2.0.0",
diff --git a/src/http-music.js b/src/http-music.js
index 12c93e7..a28b0f2 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -3,11 +3,12 @@
 'use strict'
 
 const { promisify } = require('util')
+const clone = require('clone')
 const fs = require('fs')
+const fetch = require('node-fetch')
 const pickers = require('./pickers')
 const loopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
-const fetch = require('node-fetch')
 
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString,
@@ -40,7 +41,7 @@ process.on('warning', e => console.warn(e.stack))
 Promise.resolve()
   .then(async () => {
     let sourcePlaylist = null
-    let activePlaylistGroup = null
+    let activePlaylist = null
 
     let pickerType = 'shuffle'
     let playOpts = []
@@ -71,14 +72,17 @@ Promise.resolve()
 
       const openedPlaylist = updatePlaylistFormat(JSON.parse(playlistText))
 
+      // The active playlist is a clone of the source playlist; after all it's
+      // quite possible we'll be messing with the value of the active playlist,
+      // and we don't want to reflect those changes in the source playlist.
       sourcePlaylist = openedPlaylist
-      activePlaylistGroup = {items: openedPlaylist.items}
+      activePlaylist = clone(openedPlaylist)
 
       processArgv(openedPlaylist.options, optionFunctions)
     }
 
     function requiresOpenPlaylist() {
-      if (activePlaylistGroup === null) {
+      if (activePlaylist === null) {
         throw new Error(
           "This action requires an open playlist - try --open (file)"
         )
@@ -117,7 +121,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        activePlaylistGroup = {items: []}
+        activePlaylist.items = []
       },
 
       'c': util => util.alias('-clear'),
@@ -133,7 +137,7 @@ Promise.resolve()
 
         const pathString = util.nextArg()
         const group = filterPlaylistByPathString(sourcePlaylist, pathString)
-        activePlaylistGroup.items.push(group)
+        activePlaylist.items.push(group)
       },
 
       'k': util => util.alias('-keep'),
@@ -146,7 +150,7 @@ Promise.resolve()
 
         const pathString = util.nextArg()
         console.log("Ignoring path: " + pathString)
-        removeGroupByPathString(activePlaylistGroup, pathString)
+        removeGroupByPathString(activePlaylist, pathString)
       },
 
       'r': util => util.alias('-remove'),
@@ -158,7 +162,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(getPlaylistTreeString(activePlaylistGroup))
+        console.log(getPlaylistTreeString(activePlaylist))
 
         // If this is the last item in the argument list, the user probably
         // only wants to get the list, so we'll mark the 'should run' flag
@@ -177,7 +181,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(getPlaylistTreeString(activePlaylistGroup, true))
+        console.log(getPlaylistTreeString(activePlaylist, true))
 
         // As with -l, if this is the last item in the argument list, we
         // won't actually be playing the playlist.
@@ -230,7 +234,7 @@ Promise.resolve()
 
         requiresOpenPlaylist()
 
-        console.log(JSON.stringify(activePlaylistGroup, null, 2))
+        console.log(JSON.stringify(activePlaylist, null, 2))
       }
     }
 
@@ -238,7 +242,7 @@ Promise.resolve()
 
     await processArgv(process.argv, optionFunctions)
 
-    if (activePlaylistGroup === null) {
+    if (activePlaylist === null) {
       throw new Error(
         "Cannot play - no open playlist. Try --open <playlist file>?"
       )
@@ -248,10 +252,10 @@ Promise.resolve()
       let picker
       if (pickerType === 'shuffle') {
         console.log("Using shuffle picker.")
-        picker = pickers.makeShufflePlaylistPicker(activePlaylistGroup)
+        picker = pickers.makeShufflePlaylistPicker(activePlaylist)
       } else if (pickerType === 'ordered') {
         console.log("Using ordered picker.")
-        picker = pickers.makeOrderedPlaylistPicker(activePlaylistGroup)
+        picker = pickers.makeOrderedPlaylistPicker(activePlaylist)
       } else {
         console.error("Invalid picker type: " + pickerType)
         return
@@ -348,7 +352,7 @@ Promise.resolve()
 
       return playPromise
     } else {
-      return activePlaylistGroup
+      return activePlaylist
     }
   })
   .catch(err => console.error(err))
diff --git a/yarn.lock b/yarn.lock
index 6e61ed0..12bc494 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1,7 +1,5 @@
 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
 # yarn lockfile v1
-
-
 "@types/node@^6.0.46":
   version "6.0.73"
   resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.73.tgz#85dc4bb6f125377c75ddd2519a1eeb63f0a4ed70"
@@ -25,6 +23,10 @@ cheerio@^1.0.0-rc.1:
     lodash "^4.15.0"
     parse5 "^3.0.1"
 
+clone:
+  version "2.1.1"
+  resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.1.tgz#d217d1e961118e3ac9a4b8bba3285553bf647cdb"
+
 core-util-is@~1.0.0:
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -46,14 +48,14 @@ css-what@2.1:
   version "2.1.0"
   resolved "https://registry.yarnpkg.com/css-what/-/css-what-2.1.0.tgz#9467d032c38cfaefb9f2d79501253062f87fa1bd"
 
-dom-serializer@0, dom-serializer@~0.1.0:
+dom-serializer@~0.1.0, dom-serializer@0:
   version "0.1.0"
   resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82"
   dependencies:
     domelementtype "~1.1.1"
     entities "~1.1.1"
 
-domelementtype@1, domelementtype@^1.3.0:
+domelementtype@^1.3.0, domelementtype@1:
   version "1.3.0"
   resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.0.tgz#b17aed82e8ab59e52dd9c19b1756e0fc187204c2"
 
@@ -67,7 +69,7 @@ domhandler@^2.3.0:
   dependencies:
     domelementtype "1"
 
-domutils@1.5.1, domutils@^1.5.1:
+domutils@^1.5.1, domutils@1.5.1:
   version "1.5.1"
   resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.5.1.tgz#dcd8488a26f563d61079e48c9f7b7e32373682cf"
   dependencies:
@@ -251,3 +253,4 @@ xmldoc:
   resolved "https://registry.yarnpkg.com/xmldoc/-/xmldoc-1.1.0.tgz#25c92f08f263f344dac8d0b32370a701ee9d0e93"
   dependencies:
     sax "^1.2.1"
+