« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/crawlers.js1
-rw-r--r--src/general-util.js2
-rw-r--r--src/open-file.js35
3 files changed, 37 insertions, 1 deletions
diff --git a/src/crawlers.js b/src/crawlers.js
index 5ad7fb4..635cc1e 100644
--- a/src/crawlers.js
+++ b/src/crawlers.js
@@ -7,6 +7,7 @@ module.exports = {
       case 'crawl-local': return require('./crawl-local')
       case 'crawl-itunes': return require('./crawl-itunes')
       case 'crawl-youtube': return require('./crawl-youtube')
+      case 'open-file': return require('./open-file')
       default: return null
     }
   }
diff --git a/src/general-util.js b/src/general-util.js
index 63ef1b2..825dd90 100644
--- a/src/general-util.js
+++ b/src/general-util.js
@@ -26,7 +26,7 @@ function downloadPlaylistFromURL(url) {
 }
 
 function downloadPlaylistFromLocalPath(path) {
-  return readFile(path)
+  return readFile(path).then(buf => buf.toString())
 }
 
 module.exports.downloadPlaylistFromOptionValue = function(arg) {
diff --git a/src/open-file.js b/src/open-file.js
new file mode 100644
index 0000000..357bdae
--- /dev/null
+++ b/src/open-file.js
@@ -0,0 +1,35 @@
+// Internal "crawler" that simply opens a file and returns the playlist stored
+// in that file. This can also open web URLs; it uses the same code that the
+// play option --open-playlist does.
+
+const {
+  downloadPlaylistFromOptionValue
+} = require('./general-util')
+
+function crawl(input) {
+  return downloadPlaylistFromOptionValue(input)
+}
+
+async function main(args, shouldReturn = false) {
+  if (args.length !== 1) {
+    console.log("Usage: open-file /example/path.json")
+    console.log("Note that open-file is generally supposed to be used as a 'source' argument!")
+    console.log("So, for example, you could make a playlist that looks something like this:")
+    console.log('{"items": [')
+    console.log('  {"source": ["open-file", "jazz/playlist.json"]},')
+    console.log('  {"source": ["open-file", "noise/playlist.json"]}')
+    console.log(']}')
+    return
+  }
+
+  const playlist = await crawl(args[0])
+
+  const str = JSON.stringify(playlist, null, 2)
+  if (shouldReturn) {
+    return str
+  } else {
+    console.log(str)
+  }
+}
+
+module.exports = {crawl, main}