hexo-shell.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * This file is part of HexoEditor.
  3. *
  4. * Copyright (c) 2018 zhuzhuyule
  5. */
  6. 'use strict'
  7. const Promise = require('bluebird');
  8. const thread_kill = require('./thread_kill');
  9. const exec = require('child_process').exec;
  10. const util = require('util');
  11. let shellServer = false;
  12. class ShellServer {
  13. constructor() {
  14. this.shellProcess = null;
  15. this.isForce = false;
  16. this.oldbiu = null;
  17. this.drags = null;
  18. shellServer = this;
  19. }
  20. processRunning() {
  21. return (this.shellProcess && this.shellProcess != null)
  22. }
  23. sendConsole(content, type, btnTip) {
  24. if (shellServer.closeMsg) return;
  25. try {
  26. shellServer.lastWindow.hexoeditorWindow.window.webContents.send('pop-message-shell', {
  27. subProcess: this.shellProcess,
  28. content: content,
  29. type: type,
  30. btnTip: btnTip,
  31. });
  32. } catch (e) {
  33. content.error(e)
  34. }
  35. }
  36. execCmd(command) {
  37. console.log('execute', command);
  38. let flagOK = false;
  39. clearTimeout(shellServer.timeID);
  40. shellServer.closeMsg = false;
  41. shellServer.lastWindow = require('electron').BrowserWindow.getFocusedWindow();
  42. shellServer.isForce = false;
  43. shellServer.shellProcess = exec(command, {cwd: moeApp.hexo.config.__basedir});
  44. shellServer.sendConsole('<i class="fa fa-spinner fa-pulse fa-fw margin-bottom"></i>'+__("Executing"), 'info', 'ban');
  45. shellServer.shellProcess.stderr.on('data', (data) => {
  46. console.log(data);
  47. });
  48. shellServer.shellProcess.stdout.on('data', (data) => {
  49. clearTimeout(shellServer.timeID);
  50. console.log(data);
  51. if ( /INFO Hexo is running at https?:\/+/.test(data) ) {
  52. flagOK = true;
  53. shellServer.sendConsole('<i class="fa fa-spinner fa-pulse fa-fw margin-bottom"></i>'+__('ServerStart'), 'info', 'stop');
  54. require('electron').shell.openExternal(data.match(/INFO Hexo is running at (https?:\/+[^\/]+\/). Press Ctrl.C to stop./i)[1])
  55. } else {
  56. shellServer.timeID = setTimeout(() => {
  57. if (!flagOK) {
  58. shellServer.kill(shellServer.shellProcess)
  59. flagOK = -1;
  60. }
  61. }, 10000)
  62. }
  63. });
  64. shellServer.shellProcess.on('close', (code, signal) => {
  65. if (flagOK === -1)
  66. shellServer.sendConsole(__('Operation Execution Timeout'), 'danger', 'close');
  67. else if (shellServer.isForce)
  68. shellServer.sendConsole(__('Operation Canceled'), 'success', 'check');
  69. else if (code == 0)
  70. shellServer.sendConsole(__('Operation Finished'), 'success', 'check');
  71. shellServer.shellProcess = null;
  72. });
  73. shellServer.shellProcess.on('error', err => {
  74. if (shellServer.shellProcess)
  75. shellServer.sendConsole(err, 'danger', 'close')
  76. console.error(err);
  77. })
  78. }
  79. kill(subProcess) {
  80. shellServer.isForce = true;
  81. shellServer.closeMsg = (typeof subProcess === "boolean");
  82. if (!subProcess)
  83. subProcess = shellServer.shellProcess
  84. if (subProcess)
  85. thread_kill(subProcess.pid, function (err) {
  86. console.error(err);
  87. });
  88. }
  89. checkPort(ip, port) {
  90. shellServer.isForce = false;
  91. shellServer.closeMsg = false;
  92. return new Promise(function (resolve, reject) {
  93. if (port > 65535 || port < 1) {
  94. return reject(new Error('Port number ' + port + ' is invalid. Try a number between 1 and 65535.'));
  95. }
  96. var server = require('net').createServer();
  97. server.once('error', reject);
  98. server.once('listening', function () {
  99. server.close();
  100. resolve(`${ip}:${port}`);
  101. });
  102. server.listen(port, ip);
  103. });
  104. }
  105. serverFail(err) {
  106. if (err.code === 'EADDRINUSE') { // 端口Ip地址占用
  107. shellServer.sendConsole(util.format(__('PortOccupied'),err.address,err.port), 'danger', 'close');
  108. }
  109. }
  110. server() {
  111. this.checkPort(moeApp.hexo.config.server.ip, moeApp.hexo.config.server.port)
  112. .then(this.execCmd('hexo s'), this.serverFail);
  113. }
  114. clean() {
  115. Promise.resolve()
  116. .then(this.execCmd('hexo clean'));
  117. }
  118. general() {
  119. Promise.resolve()
  120. .then(this.execCmd('hexo g'));
  121. }
  122. deploy() {
  123. Promise.resolve()
  124. .then(this.execCmd('hexo d'));
  125. }
  126. generalAndDeploy() {
  127. Promise.resolve()
  128. .then(this.execCmd('hexo g -d'));
  129. }
  130. stopServerForce() {
  131. const port = moeApp.hexo.config.server.port;
  132. const ip = moeApp.hexo.config.server.ip;
  133. let command = '';
  134. switch (process.platform) {
  135. case 'win32':
  136. command = 'netstat -nao | findstr ';
  137. break;
  138. case 'darwin':
  139. command = 'netstat -anp|grep ';
  140. break;
  141. default:
  142. command = 'netstat -anp|grep ';
  143. break;
  144. }
  145. this.execCmd(command + port);
  146. let portList = [];
  147. this.shellProcess.stdout.on('data', (data) => {
  148. let reg;
  149. switch (process.platform) {
  150. case 'win32':
  151. reg = new RegExp(util.format('TCP\\s+%s:%s\\s+\\d+.\\d+.\\d+.\\d+:\\d+\\s+LISTENING\\s+(\\d+)', ip, port),'i');
  152. break;
  153. case 'darwin':
  154. reg = new RegExp(util.format('tcp\\s+\\d+\\s+\\d*\\s+%s:%s\\s+\\d+.\\d+.\\d+.\\d+:[*\\d]+\\s+LISTEN\\s+(\\d+)', ip, port),'i');
  155. break;
  156. default:
  157. reg = new RegExp(util.format('tcp\\s+\\d+\\s+\\d*\\s+%s:%s\\s+\\d+.\\d+.\\d+.\\d+:[*\\d]+\\s+LISTEN\\s+(\\d+)', ip, port),'i');
  158. break;
  159. }
  160. if (reg.test(data)) {
  161. let pid = data.match(reg)[1]
  162. if (pid)
  163. portList.push(pid)
  164. }
  165. });
  166. this.shellProcess.on('close', (code, signal) => {
  167. if (portList.length > 0) {
  168. for (let i = 0, len = portList.length; i < len; i++) {
  169. thread_kill(portList[i])
  170. }
  171. }
  172. this.sendConsole(__('Operation Finished'), 'success', 'check');
  173. this.shellProcess = null;
  174. });
  175. }
  176. }
  177. module.exports = ShellServer;