« get me outta code hell

Move downloadPlaylistFromOptionValue into general-util - 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>2018-02-18 19:31:30 -0400
committerFlorrie <towerofnix@gmail.com>2018-02-18 19:31:30 -0400
commit820b7940db2f1d533848f024fbb1d6f4841ce598 (patch)
treecd074dac503a3a2e581ea37c58914a23217c923e
parent1b5d6f8f96baae53367a7a7d0f9485a42029eaa3 (diff)
Move downloadPlaylistFromOptionValue into general-util
-rw-r--r--src/general-util.js23
-rwxr-xr-xsrc/play.js21
2 files changed, 27 insertions, 17 deletions
diff --git a/src/general-util.js b/src/general-util.js
index 67f53e3..63ef1b2 100644
--- a/src/general-util.js
+++ b/src/general-util.js
@@ -1,3 +1,9 @@
+const { promisify } = require('util')
+const fs = require('fs')
+const fetch = require('node-fetch')
+
+const readFile = promisify(fs.readFile)
+
 module.exports.showTrackProcessStatus = function(
   total, doneCount, noLineBreak = false
 ) {
@@ -14,3 +20,20 @@ module.exports.showTrackProcessStatus = function(
     (noLineBreak ? '' : '\n')
   )
 }
+
+function downloadPlaylistFromURL(url) {
+  return fetch(url).then(res => res.text())
+}
+
+function downloadPlaylistFromLocalPath(path) {
+  return readFile(path)
+}
+
+module.exports.downloadPlaylistFromOptionValue = function(arg) {
+  // TODO: Verify things!
+  if (arg.startsWith('http://') || arg.startsWith('https://')) {
+    return downloadPlaylistFromURL(arg)
+  } else {
+    return downloadPlaylistFromLocalPath(arg)
+  }
+}
diff --git a/src/play.js b/src/play.js
index d5df591..158c887 100755
--- a/src/play.js
+++ b/src/play.js
@@ -20,29 +20,16 @@ const {
 } = require('./playlist-utils')
 
 const {
+  downloadPlaylistFromOptionValue
+} = require('./general-util')
+
+const {
   compileKeybindings, getComboForCommand, stringifyCombo
 } = require('./keybinder')
 
 const readFile = promisify(fs.readFile)
 const writeFile = promisify(fs.writeFile)
 
-function downloadPlaylistFromURL(url) {
-  return fetch(url).then(res => res.text())
-}
-
-function downloadPlaylistFromLocalPath(path) {
-  return readFile(path)
-}
-
-function downloadPlaylistFromOptionValue(arg) {
-  // TODO: Verify things!
-  if (arg.startsWith('http://') || arg.startsWith('https://')) {
-    return downloadPlaylistFromURL(arg)
-  } else {
-    return downloadPlaylistFromLocalPath(arg)
-  }
-}
-
 function clearConsoleLine() {
   process.stdout.write('\x1b[1K\r')
 }