hexo-shell.js 8.0 KB

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