open.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var childProcess = require("child_process");
  4. /**
  5. * open a file or uri using the default application for the file type.
  6. *
  7. * @return {ChildProcess} - the child process object.
  8. * @param {string} target - the file/uri to open.
  9. * @param {string} appName - (optional) the application to be used to open the
  10. * file (for example, "chrome", "firefox")
  11. * @param {function(Error)} callback - called with null on success, or
  12. * an error object that contains a property 'code' with the exit
  13. * code of the process.
  14. */
  15. function default_1(target, appName, callback) {
  16. var opener;
  17. if (typeof appName === 'function') {
  18. callback = appName;
  19. appName = null;
  20. }
  21. switch (process.platform) {
  22. case 'darwin':
  23. if (typeof appName === 'string') {
  24. opener = 'open -a "' + escape(appName) + '"';
  25. }
  26. else {
  27. opener = 'open';
  28. }
  29. break;
  30. case 'win32':
  31. // if the first parameter to start is quoted, it uses that as the title
  32. // so we pass a blank title so we can quote the file we are opening
  33. if (typeof appName === 'string') {
  34. opener = 'start "" "' + escape(appName) + '"';
  35. }
  36. else {
  37. opener = 'start ""';
  38. }
  39. break;
  40. default:
  41. if (typeof appName === 'string') {
  42. opener = escape(appName);
  43. }
  44. else {
  45. // use system installed Portlands xdg-open everywhere else
  46. opener = 'xdg-open';
  47. }
  48. break;
  49. }
  50. if (process.env.SUDO_USER) {
  51. opener = 'sudo -u ' + process.env.SUDO_USER + ' ' + opener;
  52. }
  53. return childProcess.exec(opener + ' "' + escape(target) + '"', callback);
  54. }
  55. exports.default = default_1;
  56. function escape(s) {
  57. return s.replace(/"/g, '\\\"');
  58. }