Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. // "linux": ["gnome-terminal", "konsole", "xfce4-terminal", "terminator", "xterm", "uxterm"],
  2. // "win32": ["cmd","powershell","bash"],
  3. // "darwin": ["bash"],
  4.  
  5. class Terminal {
  6. constructor() {
  7. this.get_linux_terminal = function () {
  8. switch (process.env.GDMSESSION) {
  9. // if session is using gtk
  10. case 'ubuntu':
  11. case 'ubuntu-2d':
  12. case 'gnome':
  13. case 'gnome-shell':
  14. case 'gnome-classic':
  15. case 'gnome-fallback':
  16. case 'cinnamon':
  17. return "gnome-terminal";
  18. // xfce session has its own terminal, xfce is gtk compatible
  19. case 'xfce':
  20. return "xfce4-terminal";
  21. // if session is using qt, kde and lxde are qt compatible
  22. case 'kde-plasma':
  23. return "konsole";
  24. case 'Lubuntu':
  25. return "lxterminal";
  26. // if unknown session, default to xterm
  27. default:
  28. // attempt to determine desktop session
  29. switch (process.env.XDG_CURRENT_DESKTOP) {
  30. case 'Unity':
  31. case 'GNOME':
  32. case 'X-Cinnamon':
  33. return "gnome-terminal";
  34. case 'XFCE':
  35. return "xfce4-terminal";
  36. case 'KDE':
  37. return "konsole";
  38. case 'LXDE':
  39. return "lxterminal";
  40. default:
  41. return "xterm";
  42. }
  43. return ""; // redundant LBYL response
  44. }
  45. };
  46.  
  47. this.set_linux_terminal = function (shell) {
  48. // creates an object containing the environments
  49. // default shell and execute option
  50. switch (shell) {
  51. case 'gnome-terminal':
  52. case 'xfce4-terminal':
  53. return {
  54. 'shell': shell,
  55. 'option': '-x'
  56. };
  57. case 'konsole':
  58. case 'lxterminal':
  59. case 'xterm':
  60. return {
  61. 'shell': shell,
  62. 'option': '-e'
  63. };
  64. default:
  65. return {
  66. 'shell': "xterm",
  67. 'option': '-e'
  68. };
  69. return ""; // redundant LBYL response
  70. }
  71. };
  72.  
  73. this.get_tty = function () {
  74. if ("linux" == process.platform) {
  75. // linux has a mass variety of environments and a variety of ways
  76. // for determining those environments. best to go for the base
  77. // environments by depending on the most common builtin shells instead.
  78. // https://askubuntu.com/questions/72549/how-to-determine-which-window-manager-is-running
  79. return this.set_linux_terminal(
  80. this.get_linux_terminal()
  81. );
  82. }
  83.  
  84. if ("darwin" == process.platform){
  85. // even though mac os x supports other shells, i assume that they
  86. // support the command option as $SHELL -c "command string".
  87. // some users report that $SHELL fails and that osascript works.
  88. // https://ss64.com/osx/osascript.html
  89. // return {
  90. // 'shell': process.env.SHELL,
  91. // 'option': '-c'
  92. // };
  93. return {
  94. 'shell': 'osascript',
  95. 'option': '-e',
  96. 'command': 'tell app "Terminal" to do script',
  97. };
  98. }
  99.  
  100. if ("win32" == process.platform) {
  101. // windows xp and up can be gaurenteed to have the cmd.exe shell.
  102. // %comspec% should default to %windir%system32cmd.exe unless
  103. // otherwise modified by the end user.
  104. // https://en.wikipedia.org/wiki/Environment_variable#Windows
  105. return {
  106. 'shell': process.env.COMSPEC,
  107. 'option': '/c',
  108. 'command': 'start',
  109. };
  110. }
  111.  
  112. return {}; // redundant LBYL response
  113. };
  114. }
  115. }
  116.  
  117. exports.cli = cli = new Terminal()
  118. exports.has_a_tty = cli.get_tty()
  119.  
  120. const terminal = require('./terminal');
  121.  
  122. const spawn = require('child_process').spawn;
  123.  
  124. switch (process.platform) {
  125. case 'linux':
  126. var echo = spawn(
  127. terminal.has_a_tty.shell,
  128. [
  129. terminal.has_a_tty.option,
  130. "python",
  131. "-c",
  132. "print('Hello, World');input('...enter to continue...')",
  133. ]
  134. );
  135. break;
  136. case 'win32':
  137. case 'darwin':
  138. var echo = spawn(
  139. terminal.has_a_tty.shell,
  140. [
  141. terminal.has_a_tty.option,
  142. terminal.has_a_tty.command,
  143. "python",
  144. "-c",
  145. "print('Hello, World');input('...enter to continue...')",
  146. ]
  147. );
  148. break;
  149. default:
  150. console.error(
  151. new Error("Error: Could not determine the OS platform type.")
  152. );
  153. break;
  154. }
  155.  
  156. echo.stdout.on('data', (data) => {
  157. console.log(`stdout: ${data}`);
  158. });
  159.  
  160. echo.stderr.on('data', (data) => {
  161. console.log(`stderr: ${data}`);
  162. });
  163.  
  164. echo.on('close', (code) => {
  165. console.log(`child process exited with code ${code}`);
  166. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement