« get me outta code hell

Desmartify playlists in play.js - 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-08-07 16:43:02 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-07 16:43:39 -0300
commit7cd692dc759b167967b83d2333d52169d2b45045 (patch)
tree8d03deefaee26fbb948dd7e82258d6f58f20c806
parente2d48882c6f7840f88bf663d42f6f0fa4dfb17ce (diff)
Desmartify playlists in play.js
-rwxr-xr-xsrc/play.js10
-rw-r--r--src/smart-playlist.js20
-rw-r--r--todo.txt5
3 files changed, 22 insertions, 13 deletions
diff --git a/src/play.js b/src/play.js
index b2c04e9..7807c93 100755
--- a/src/play.js
+++ b/src/play.js
@@ -10,6 +10,7 @@ const npmCommandExists = require('command-exists')
 const pickers = require('./pickers')
 const loopPlay = require('./loop-play')
 const processArgv = require('./process-argv')
+const processSmartPlaylist = require('./smart-playlist')
 
 const {
   filterPlaylistByPathString, removeGroupByPathString, getPlaylistTreeString,
@@ -95,13 +96,16 @@ async function main(args) {
 
     const openedPlaylist = updatePlaylistFormat(JSON.parse(playlistText))
 
+    // We also want to de-smart-ify (stupidify? - simplify?) the playlist.
+    const processedPlaylist = await processSmartPlaylist(openedPlaylist)
+
     // 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
-    activePlaylist = clone(openedPlaylist)
+    sourcePlaylist = processedPlaylist
+    activePlaylist = clone(processedPlaylist)
 
-    processArgv(openedPlaylist.options, optionFunctions)
+    processArgv(processedPlaylist.options, optionFunctions)
   }
 
   function requiresOpenPlaylist() {
diff --git a/src/smart-playlist.js b/src/smart-playlist.js
index e65ff1f..76cd877 100644
--- a/src/smart-playlist.js
+++ b/src/smart-playlist.js
@@ -8,30 +8,32 @@ const readFile = promisify(fs.readFile)
 
 async function processItem(item) {
   // Object.assign is used so that we keep original properties, e.g. "name"
-  // or "apply".
+  // or "apply". (It's also used so we return copies of original objects.)
 
-  if ('items' in item) {
-    return Object.assign(item, {
-      items: await Promise.all(item.items.map(processItem))
-    })
-  } else if ('source' in item) {
+  if ('source' in item) {
     const [ name, ...args ] = item.source
 
     const crawlModule = getCrawlerByName(name)
 
     if (crawlModule === null) {
       console.error(`No crawler by name ${name} - skipped item:`, item)
-      return Object.assign(item, {failed: true})
+      return Object.assign({}, item, {failed: true})
     }
 
     const { crawl } = crawlModule
 
-    return Object.assign(item, await crawl(...args))
+    return Object.assign({}, item, await crawl(...args))
+  } else if ('items' in item) {
+    return Object.assign({}, item, {
+      items: await Promise.all(item.items.map(processItem))
+    })
   } else {
-    return item
+    return Object.assign({}, item)
   }
 }
 
+module.exports = processItem
+
 async function main(opts) {
   // TODO: Error when no file is given
 
diff --git a/todo.txt b/todo.txt
index 266e67c..a6e4f48 100644
--- a/todo.txt
+++ b/todo.txt
@@ -252,7 +252,7 @@ TODO: Change usages of "/example/path" to a more specific "/path/to/playlist"
 TODO: Support smart playlists right inside of play - and ideally any other
       usage, e.g. download-playlist. For now the user can just run
       smart-playlist, save the result, and load that in whatever command
-      they're using.
+      they're using. (`play` supported.)
 
 TODO: Markdown documentation? Man pages are nice, but aren't really all that
       user-friendly (citation needed); for example you can't easily read them
@@ -264,3 +264,6 @@ TODO: Handle avconv failing (probably handle downloader rejections from within
       are loaded, but still relevant (for old playlists which may contain
       .DS_Stores or album cover arts, etc).
       (Done!)
+
+TODO: Delete temporary files when done with them - seriously! http-music alone
+      filled up a good 9GB of disk space, solely on temporary music files.