Skip to content

Commit

Permalink
optional callbacks and parsing version
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 15, 2011
1 parent 0995cc0 commit e2e92ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
44 changes: 23 additions & 21 deletions lib/growl.js
Expand Up @@ -5,8 +5,8 @@
* Module dependencies.
*/

var child_process = require('child_process'),
path = require('path')
var exec = require('child_process').exec
, path = require('path');

/**
* Node-growl version.
Expand All @@ -17,16 +17,17 @@ exports.version = '1.0.2'
/**
* Fetch the binary version when available.
*
* @param {function} callback
* @param {function} fn
* @api public
*/

exports.binVersion = function(callback) {
child_process.exec('growlnotify -v', function(err, stdout, stderr){
if (err) callback(err)
else callback(null, stdout)
})
}
exports.binVersion = function(fn) {
exec('growlnotify -v', function(err, stdout){
if (err) return fn(err);
var version = /(\d+\.\d+(?:\.\d+)?)/.exec(stdout)[1];
fn(null, parseFloat(version));
});
};

/**
* Send growl notification _msg_ with _options_.
Expand All @@ -53,16 +54,17 @@ exports.binVersion = function(callback) {
*
* @param {string} msg
* @param {object} options
* @param {function} callback
* @param {function} fn
* @api public
*/

exports.notify = function(msg, options, callback) {
var image,
args = ['growlnotify', '-m', '"' + msg + '"'],
options = options || {}
exports.notify = function(msg, options, fn) {
var image
, args = ['growlnotify', '-m', '"' + msg + '"']
, options = options || {}
, fn = fn || function(){};
exports.binVersion(function(err, version){
if (err) return callback(err)
if (err) return fn(err);
if (image = options.image) {
var flag, ext = path.extname(image).substr(1)
flag = flag || ext == 'icns' && 'iconpath'
Expand All @@ -72,9 +74,9 @@ exports.notify = function(msg, options, callback) {
flag = flag || 'icon'
args.push('--' + flag, image)
}
if (options.sticky) args.push('--sticky')
if (options.name) args.push('--name', options.name)
if (options.title) args.push(options.title)
child_process.exec(args.join(' '), callback)
})
}
if (options.sticky) args.push('--sticky');
if (options.name) args.push('--name', options.name);
if (options.title) args.push(options.title);
exec(args.join(' '), fn);
});
};
2 changes: 1 addition & 1 deletion test.js
Expand Up @@ -2,7 +2,7 @@
var growl = require('./lib/growl')

growl.binVersion(function(err, version){
require('sys').p(version)
console.log(version);
})
growl.notify('You have mail!')
growl.notify('5 new messages', { sticky: true })
Expand Down

0 comments on commit e2e92ee

Please sign in to comment.