Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better escaping for shell commands

  • Loading branch information...
commit 6259b781fbdfb8b677f6b1ceec630218aba58eca 1 parent 68ec24d
@evanw evanw authored
Showing with 17 additions and 13 deletions.
  1. +14 −13 lib/growl.js
  2. +3 −0  test.js
View
27 lib/growl.js
@@ -8,6 +8,7 @@ var exec = require('child_process').exec
, exists = require('fs').existsSync
, path = require('path')
, os = require('os')
+ , quote = JSON.stringify
, cmd;
function which(name) {
@@ -165,7 +166,7 @@ function growl(msg, options, fn) {
args.push(cmd.icon + " " + image);
break;
case 'Windows':
- args.push(cmd.icon + '"' + image.replace(/\\/g, "\\\\") + '"');
+ args.push(cmd.icon + quote(image));
break;
}
}
@@ -190,38 +191,38 @@ function growl(msg, options, fn) {
switch(cmd.type) {
case 'Darwin-Growl':
args.push(cmd.msg);
- args.push('"' + msg + '"');
- if (options.title) args.push('"' + options.title + '"');
+ args.push(quote(msg));
+ if (options.title) args.push(quote(options.title));
break;
case 'Darwin-NotificationCenter':
args.push(cmd.msg);
- args.push('"' + msg + '"');
+ args.push(quote(msg));
if (options.title) {
args.push(cmd.title);
- args.push('"' + options.title + '"');
+ args.push(quote(options.title));
}
if (options.subtitle) {
args.push(cmd.subtitle);
- args.push('"' + options.subtitle + '"');
+ args.push(quote(options.title));
}
break;
case 'Darwin-Growl':
args.push(cmd.msg);
- args.push('"' + msg + '"');
- if (options.title) args.push('"' + options.title + '"');
+ args.push(quote(msg));
+ if (options.title) args.push(quote(options.title));
break;
case 'Linux':
if (options.title) {
- args.push("'" + options.title + "'");
+ args.push(quote(options.title));
args.push(cmd.msg);
- args.push("'" + msg + "'");
+ args.push(quote(msg));
} else {
- args.push("'" + msg + "'");
+ args.push(quote(msg));
}
break;
case 'Windows':
- args.push('"' + msg + '"');
- if (options.title) args.push(cmd.title + '"' + options.title + '"');
+ args.push(quote(msg));
+ if (options.title) args.push(cmd.title + quote(options.title));
break;
}
View
3  test.js
@@ -15,3 +15,6 @@ growl('Show pdf filesystem icon', { image: 'article.pdf' }, function(){
console.log('callback');
})
growl('Show pdf filesystem icon', { title: 'Use show()', image: 'article.pdf' })
+growl('here \' are \n some \\ characters that " need escaping', {}, function(error, stdout, stderr) {
+ if (error !== null) throw new Error('escaping failed:\n' + stdout + stderr);
+})
Please sign in to comment.
Something went wrong with that request. Please try again.