opener.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. var childProcess = require("child_process");
  3. var os = require("os");
  4. module.exports = function opener(args, options, callback) {
  5. var platform = process.platform;
  6. // Attempt to detect Windows Subystem for Linux (WSL). WSL itself as Linux (which works in most cases), but in
  7. // this specific case we need to treat it as actually being Windows. The "Windows-way" of opening things through
  8. // cmd.exe works just fine here, whereas using xdg-open does not, since there is no X Windows in WSL.
  9. if (platform === "linux" && os.release().indexOf("Microsoft") !== -1) {
  10. platform = "win32";
  11. }
  12. // http://stackoverflow.com/q/1480971/3191, but see below for Windows.
  13. var command;
  14. switch (platform) {
  15. case "win32": {
  16. command = "cmd.exe";
  17. break;
  18. }
  19. case "darwin": {
  20. command = "open";
  21. break;
  22. }
  23. default: {
  24. command = "xdg-open";
  25. break;
  26. }
  27. }
  28. if (typeof args === "string") {
  29. args = [args];
  30. }
  31. if (typeof options === "function") {
  32. callback = options;
  33. options = {};
  34. }
  35. if (options && typeof options === "object" && options.command) {
  36. if (platform === "win32") {
  37. // *always* use cmd on windows
  38. args = [options.command].concat(args);
  39. } else {
  40. command = options.command;
  41. }
  42. }
  43. if (platform === "win32") {
  44. // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
  45. // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
  46. // responsibility to "cmd /c", which has that logic built in.
  47. //
  48. // Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
  49. // so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
  50. //
  51. // Additionally, on Windows ampersand and caret need to be escaped when passed to "start"
  52. args = args.map(function (value) {
  53. return value.replace(/[&^]/g, "^$&");
  54. });
  55. args = ["/c", "start", "\"\""].concat(args);
  56. }
  57. return childProcess.execFile(command, args, options, callback);
  58. };