« get me outta code hell

open-file.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/open-file.js
blob: f8af59506fab0123b27755951a43251762b0db8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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}