« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/open-file.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/open-file.js')
-rw-r--r--src/open-file.js35
1 files changed, 35 insertions, 0 deletions
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}