add support for passing options to child process

This commit is contained in:
Hrvoje Šimić
2016-10-10 18:29:54 +02:00
parent c2d3f18287
commit 71d9438c58
3 changed files with 10 additions and 25 deletions
+7 -1
View File
@@ -12,7 +12,13 @@ Play sounds by shelling out to one of the available audio players.
```javascript
var player = require('play-sound')(opts = {})
player.play('foo.mp3', function(err){}) // $ mplayer foo.mp3
player.play('foo.mp3', function(err){
if (err) throw err
}) // $ mplayer foo.mp3
player.play('foo.mp3', { timeout: 300 }, function(err){ // opts will be passed to child process
if (err) throw err
})
```
## options
+3 -12
View File
@@ -18,27 +18,18 @@ function Play(opts){
this.urlRegex = /^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i
// Regex by @stephenhay from https://mathiasbynens.be/demo/url-regex
this.play = function(what, next, maxDuration){
this.play = function(what, options, next){
next = next || function(){}
next = typeof(options) === 'function' ? options : next
var isURL = this.player == 'mplayer' && this.urlRegex.test(what)
try {
isFile = fs.statSync(what).isFile()
} catch (err){
isFile = false
}
if (!what) return next(new Error("No audio file specified"));
if (!isURL && !isFile){
return next(new Error(what + " is not a file or URL"))
}
if (!this.player){
return next(new Error("Couldn't find a suitable audio player"))
}
child_process.execFile(this.player, [what], { timeout: maxDuration }, function(err, stdout, stderr){
child_process.execFile(this.player, [what], options, function(err, stdout, stderr){
next(err && !err.killed ? err : undefined);
})
}
-12
View File
@@ -36,18 +36,6 @@ describe('mplayer has the maximum priority', function(){
})
describe('error handling', function(){
it("throws errors if the file doesn't exist", function(done){
var spy = sinon.stub(),
player = proxyquire('./', { child_process: {execFile: spy}})({ player: 'mplayer'})
spy.callsArgWith(1, undefined, undefined, "file doesn't exist")
player.play('beep.mp3', function(err){
expect(err.message).to.be("beep.mp3 is not a file or URL")
done()
})
})
it("throws errors if suitable audio tool couldn't be found", function(done){
var cli = require('./')({ players: [] })