From 71d9438c585d0ee64fdaaa24bdeedc5fc4ebe790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Mon, 10 Oct 2016 18:29:54 +0200 Subject: [PATCH] add support for passing options to child process --- README.md | 8 +++++++- index.js | 15 +++------------ tests.js | 12 ------------ 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 64197eb..9afa749 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 3cbeece..29ca852 100644 --- a/index.js +++ b/index.js @@ -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); }) } diff --git a/tests.js b/tests.js index ba026fd..a87596c 100644 --- a/tests.js +++ b/tests.js @@ -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: [] })