Control.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { exec } from 'child_process';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. const __rootPath = path.resolve(__dirname, '../../');
  7. const GLOBAL_DATA = {
  8. gitLock: false,
  9. buildLock: false,
  10. };
  11. /**
  12. * 拉取指定仓库的代码
  13. * @param {string} repoPath 仓库路径
  14. * @returns {Promise<string>} 拉取结果
  15. */
  16. const gitPull = (repoPath) => {
  17. if (GLOBAL_DATA.gitLock) {
  18. return Promise.reject(new Error('git is locked'));
  19. }
  20. GLOBAL_DATA.gitLock = true;
  21. return new Promise((resolve, reject) => {
  22. const cwd = path.resolve(repoPath);
  23. exec("git pull", { cwd }, (error, stdout, stderr) => {
  24. GLOBAL_DATA.gitLock = false;
  25. if (error) {
  26. reject(new Error(stderr) || error);
  27. return;
  28. }
  29. resolve(stdout || "No output");
  30. });
  31. });
  32. }
  33. /**
  34. * 拉当前项目的代码
  35. */
  36. const gitPullCurrent = () => {
  37. return gitPull(__rootPath);
  38. }
  39. /**
  40. * 启动服务
  41. * @param {string} serviceName 服务名称
  42. * @returns {Promise<string>} 启动结果
  43. */
  44. const pm2Start = (serviceName) => {
  45. // if (process.env.NODE_ENV == 'development') {
  46. // return Promise.resolve('development mode, skip');
  47. // }
  48. return new Promise((resolve, reject) => {
  49. exec(`pm2 start ${serviceName}`, (error, stdout, stderr) => {
  50. if (error) {
  51. reject(new Error(stderr) || error);
  52. return;
  53. }
  54. resolve(stdout || "No output");
  55. });
  56. });
  57. }
  58. /**
  59. * 停止服务
  60. * @param {string} serviceName 服务名称
  61. * @returns {Promise<string>} 停止结果
  62. */
  63. const pm2Stop = (serviceName) => {
  64. // if (process.env.NODE_ENV == 'development') {
  65. // return Promise.resolve('development mode, skip');
  66. // }
  67. return new Promise((resolve, reject) => {
  68. exec(`pm2 stop ${serviceName}`, (error, stdout, stderr) => {
  69. if (error) {
  70. reject(new Error(stderr) || error);
  71. return;
  72. }
  73. resolve(stdout || "No output");
  74. });
  75. });
  76. }
  77. /**
  78. * PM2 重启服务
  79. * @param {string} serviceName 服务名称
  80. * @returns {Promise<string>} 重启结果
  81. */
  82. const pm2Restart = (serviceName) => {
  83. // if (process.env.NODE_ENV == 'development') {
  84. // return Promise.resolve('development mode, skip');
  85. // }
  86. return new Promise((resolve, reject) => {
  87. exec(`pm2 restart ${serviceName}`, (error, stdout, stderr) => {
  88. if (error) {
  89. reject(new Error(stderr) || error);
  90. return;
  91. }
  92. resolve(stdout || "No output");
  93. });
  94. });
  95. }
  96. /**
  97. * 清除缓存
  98. */
  99. const clearCache = (cachePath) => {
  100. return new Promise((resolve, reject) => {
  101. exec(`rm -rf ${cachePath}/*`, (error, stdout, stderr) => {
  102. if (error) {
  103. reject(new Error(stderr) || error);
  104. return;
  105. }
  106. resolve(stdout || "No output");
  107. });
  108. });
  109. }
  110. /**
  111. * 清除缓存并重启服务
  112. */
  113. const pm2RestartClearCache = async (serviceName, cachePath) => {
  114. const stopResult = await pm2Stop(serviceName);
  115. const clearResult = await clearCache(cachePath);
  116. const startResult = await pm2Start(serviceName);
  117. return [stopResult, clearResult, startResult].join('\n\n\n====================\n\n\n');
  118. }
  119. /**
  120. * PM2 重启 sporttery 服务
  121. * @param {boolean} hot 是否热重启
  122. * @returns {Promise<string>} 重启结果
  123. */
  124. const pm2RestartSporttery = (hot) => {
  125. if (hot) {
  126. return pm2Restart('sporttery');
  127. }
  128. else {
  129. return pm2RestartClearCache('sporttery', path.resolve(__rootPath, 'server/data'));
  130. }
  131. }
  132. /**
  133. * PM2 重启 pinnacle 服务
  134. * @param {boolean} hot 是否热重启
  135. * @returns {Promise<string>} 重启结果
  136. */
  137. const pm2RestartPinnacle = (hot) => {
  138. if (hot) {
  139. return pm2Restart('pinnacle');
  140. }
  141. else {
  142. return pm2RestartClearCache('pinnacle', path.resolve(__rootPath, 'pinnacle/data'));
  143. }
  144. }
  145. /**
  146. * 发布 web 服务
  147. */
  148. const releaseWeb = () => {
  149. if (GLOBAL_DATA.buildLock) {
  150. return Promise.reject(new Error('build is locked'));
  151. }
  152. GLOBAL_DATA.buildLock = true;
  153. const webRootPath = path.resolve(__rootPath, 'web');
  154. return new Promise((resolve, reject) => {
  155. exec('npm run build', { cwd: webRootPath }, (error, stdout, stderr) => {
  156. GLOBAL_DATA.buildLock = false;
  157. if (error) {
  158. reject(new Error(stderr) || error);
  159. return;
  160. }
  161. resolve(stdout || "No output");
  162. });
  163. });
  164. }
  165. export { gitPullCurrent, pm2RestartSporttery, pm2RestartPinnacle, releaseWeb };
  166. export default { gitPullCurrent, pm2RestartSporttery, pm2RestartPinnacle, releaseWeb };