Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements to the command launching #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions openurl.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
var spawn = require('child_process').spawn;

var command;
var commands = {
'darwin': function (url) {
return spawn('open', [url]);
},
'win32': function (url) {
return spawn('cmd', ['/c', 'start', url]);
},
'default': function (url) {
return spawn('xdg-open', [url]);
},
};

switch(process.platform) {
case 'darwin':
command = 'open';
break;
case 'win32':
command = 'explorer.exe';
break;
case 'linux':
command = 'xdg-open';
break;
default:
throw new Error('Unsupported platform: ' + process.platform);
}
var launcher = commands[process.platform] || commands['default'];

/**
* Error handling is deliberately minimal, as this function is to be easy to use for shell scripting
Expand All @@ -24,13 +22,14 @@ switch(process.platform) {
*/

function open(url, callback) {
var child = spawn(command, [url]);
var child = launcher(url);
var errorText = "";
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
errorText += data;
});
child.stderr.on('end', function () {

if (errorText.length > 0) {
var error = new Error(errorText);
if (callback) {
Expand Down Expand Up @@ -65,4 +64,4 @@ function mailto(recipients, fields, recipientsSeparator, callback) {
}

exports.open = open;
exports.mailto = mailto;
exports.mailto = mailto;