-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
48 lines (39 loc) · 1.34 KB
/
index.js
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
45
46
47
48
'use strict'
const child = require('child_process')
const eos = require('end-of-stream')
const wrap = require('spawn-wrapper')
const path = require('path')
const which = require('which')
const jeopardy = path.resolve(__dirname, 'jeopardy.mp3')
const players = [
{ name: 'afplay', getCommand: (audioFile) => ['afplay', [ audioFile ]] },
{ name: 'mplayer', getCommand: (audioFile) => ['mplayer', [ audioFile ]] },
{ name: 'mpv', getCommand: (audioFile) => ['mpv', [ '--no-audio-display', audioFile]] },
{ name: 'vlc', getCommand: (audioFile) => ['vlc',[ '-I', 'rc', audioFile ]] }
]
const findPlayer = function (remainingPlayers, callback) {
if (remainingPlayers.length < 1) {
return callback(
new Error('No suitable player was found on your system 🙁 See https://github.com/bendrucker/jeopardy#readme for a list of supported ones.')
)
}
const { name, getCommand } = remainingPlayers.pop()
which(name, (err) => {
if (!err) {
return callback(null, getCommand)
}
findPlayer(remainingPlayers, callback)
})
}
module.exports = function run (command, args, callback) {
findPlayer(players, (err, getPlayerCommand) => {
if (err) {
return callback(err)
}
const main = child.spawn(command, args, {
stdio: 'inherit'
})
wrap(main, getPlayerCommand(jeopardy))
eos(main, callback)
})
}