« get me outta code hell

cli.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/cli.js
blob: 9021cc6f764e5e2e4c2977834da8bb229974c04f (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
36
37
38
39
40
41
42
43
44
#!/usr/bin/env node

// Let this forever be of use to people who run into
// maxlistenersexceededwarning.
process.on('warning', e => console.warn(e.stack))

const { getCrawlerByName } = require('./crawlers')

async function main(args) {
  let script

  if (args.length === 0) {
    console.error("No command provided.")
    console.error("Try 'man http-music'?")
    return
  }

  const module = getCrawlerByName(args[0])

  if (module) {
    script = module.main
  } else {
    switch (args[0]) {
      case 'play': script = require('./play'); break
      case 'download-playlist': script = require('./download-playlist'); break
      case 'smart-playlist': script = require('./smart-playlist'); break
      case 'setup': script = require('./setup'); break

      default:
        console.error(`Invalid command "${args[0]}" provided.`)
        console.error("Try 'man http-music'?")
        return
    }
  }

  await script(args.slice(1))
}

module.exports = main

if (require.main === module) {
  main(process.argv.slice(2))
    .catch(err => console.error(err))
}