5 var childProcess = require("child_process");
7 function opener(args, options, callback) {
8 // http://stackoverflow.com/q/1480971/3191, but see below for Windows.
9 var command = process.platform === "win32" ? "cmd" :
10 process.platform === "darwin" ? "open" :
13 if (typeof args === "string") {
17 if (typeof options === "function") {
22 if (options && typeof options === "object" && options.command) {
23 if (process.platform === "win32") {
24 // *always* use cmd on windows
25 args = [options.command].concat(args);
27 command = options.command;
31 if (process.platform === "win32") {
32 // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
33 // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
34 // responsibility to "cmd /c", which has that logic built in.
36 // Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
37 // so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
39 // Additionally, on Windows ampersand needs to be escaped when passed to "start"
40 args = args.map(function(value) {
41 return value.replace(/&/g, '^&');
43 args = ["/c", "start", '""'].concat(args);
46 return childProcess.execFile(command, args, options, callback);
49 // Export `opener` for programmatic access.
50 // You might use this to e.g. open a website: `opener("http://google.com")`
51 module.exports = opener;
53 // If we're being called from the command line, just execute, using the command-line arguments.
54 if (require.main && require.main.id === module.id) {
55 opener(process.argv.slice(2), function (error) {