upload.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const {execSync} = require('child_process');
  2. const {isNullES} = require('js-fast-way')
  3. const fs = require('fs');
  4. const path = require('path');
  5. //获取参数
  6. const serverAddress = process.argv[2]
  7. const username = process.argv[3]
  8. const password = process.argv[4]
  9. const fileName = process.argv[5]
  10. const filePath = process.argv[6]
  11. const url = process.argv[7]
  12. // 解析服务器地址和端口
  13. const [serverIp, port] = serverAddress.split(':');
  14. const sshPort = port || '22'; // 如果没有指定端口,默认使用22
  15. // 获取主机密钥
  16. function getHostKey() {
  17. const tempFile = path.join(__dirname, 'temp_hostkey');
  18. try {
  19. execSync(`echo y | plink -P ${sshPort} ${username}@${serverIp} exit`, {stdio: 'ignore'});
  20. execSync(`plink -P ${sshPort} ${username}@${serverIp} exit >> ${tempFile} 2>&1`);
  21. const output = fs.readFileSync(tempFile, 'utf8');
  22. const match = output.match(/(?<=fingerprint is:).*?(?=\r?\n|$)/);
  23. if (match) {
  24. return match[0].trim();
  25. }
  26. throw new Error('Unable to extract host key');
  27. } finally {
  28. if (fs.existsSync(tempFile)) {
  29. fs.unlinkSync(tempFile);
  30. }
  31. }
  32. }
  33. // 获取主机密钥
  34. let hostKey = ''
  35. if (process.platform === 'win32') {
  36. if (!commandExists('pscp') || !commandExists('plink')) {
  37. console.log('putty 未安装,请先安装 putty');
  38. process.exit(1);
  39. return
  40. }
  41. hostKey = getHostKey();
  42. console.log('获取到的主机密钥:', hostKey);
  43. }
  44. // 检查命令是否可用
  45. function commandExists(command) {
  46. try {
  47. if (process.platform === 'win32') {
  48. execSync(`where ${command}`, {stdio: 'ignore'});
  49. } else {
  50. execSync(`which ${command}`, {stdio: 'ignore'});
  51. }
  52. return true;
  53. } catch (e) {
  54. return false;
  55. }
  56. }
  57. // 执行命令并打印输出
  58. function runCommand(command) {
  59. console.log(`执行命令: ${command}`);
  60. try {
  61. if (command.startsWith('sshpass') || command.startsWith('ssh')) {
  62. // 为SSH命令设置LC_ALL
  63. command = `LC_ALL=C ${command}`;
  64. }
  65. execSync(command, {stdio: 'inherit'});
  66. } catch (error) {
  67. console.error(`命令执行失败: ${error.message}`);
  68. process.exit(1);
  69. }
  70. }
  71. // 上传到服务器
  72. function uploadServer() {
  73. const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
  74. if (process.platform === 'win32') {
  75. // Windows
  76. runCommand(`pscp -P ${sshPort} -pw ${password} -hostkey "${hostKey}" ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  77. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
  78. } else {
  79. // Mac/Linux
  80. runCommand(`sshpass -p "${password}" scp -o StrictHostKeyChecking=no -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  81. runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  82. }
  83. console.log('编译打包后自动部署到服务器上完成');
  84. console.log(`服务器上的地址:${url}`);
  85. }
  86. // 删除 plugins 等目录
  87. function delPublic(name) {
  88. console.log(`准备移除 ${name} 等目录`);
  89. const sshCommand = `cd ${filePath} && rm -rf ${name}`;
  90. if (process.platform === 'win32') {
  91. // Windows
  92. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
  93. } else {
  94. // Mac/Linux
  95. runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  96. }
  97. console.log(`${name} 等目录移除完成`);
  98. }
  99. //执行
  100. function runCode() {
  101. //参数是否为空
  102. if (serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
  103. console.log('参数异常,终止执行:', {serverIp, username, password, fileName, filePath});
  104. process.exit(1);
  105. return
  106. }
  107. if (isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
  108. console.log('参数异常,终止执行:', {serverIp, username, password, fileName, filePath});
  109. process.exit(1);
  110. return
  111. }
  112. //判断命令是否支持
  113. if (process.platform === 'win32') {
  114. if (!commandExists('pscp') || !commandExists('plink')) {
  115. console.log('putty 未安装,请先安装 putty');
  116. process.exit(1);
  117. return
  118. }
  119. } else {
  120. if (!commandExists('sshpass')) {
  121. console.log('sshpass 未安装,请先安装 sshpass');
  122. process.exit(1);
  123. return
  124. }
  125. }
  126. //删除一些目录
  127. delPublic('app');
  128. delPublic('cdn');
  129. delPublic('css');
  130. delPublic('img');
  131. delPublic('js');
  132. delPublic('svg');
  133. delPublic('util');
  134. //上传到服务器
  135. uploadServer();
  136. }
  137. runCode()