« get me outta code hell

Merge branch 'master' of https://github.com/towerofnix/http-music - 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:
authorFlorrie <towerofnix@gmail.com>2018-01-17 18:33:52 -0400
committerFlorrie <towerofnix@gmail.com>2018-01-17 18:33:52 -0400
commita701fcf0d35f667b5ed32cfaed88db3d2872a9cc (patch)
treeaa2ae3d2e29b237f01a1a1936919be10d0b8e831 /src/open-file.js
parentfb2f9ecda0531a950a871c6b85d42e913f3874f7 (diff)
parent7831b28be25aae1e890ee1f4d3bd6969023c10da (diff)
Merge branch 'master' of https://github.com/towerofnix/http-music
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..f8af595
--- /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')
+
+async function crawl(input) {
+  return JSON.parse(await 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}