8
0

upload.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const { execSync } = require('child_process');
  2. const { isNullES } = require('js-fast-way')
  3. //获取参数
  4. const args = process.argv[2]
  5. const serverAddress = process.argv[3]
  6. const username = process.argv[4]
  7. const password = process.argv[5]
  8. const fileName = process.argv[6]
  9. const filePath = process.argv[7]
  10. const url = process.argv[8]
  11. // 解析服务器地址和端口
  12. const [serverIp, port] = serverAddress.split(':');
  13. const sshPort = port || '22'; // 如果没有指定端口,默认使用22
  14. // 这里填入您之前获取的主机密钥的 SHA256 指纹
  15. //plink -ssh root@192.168.0.109 -P 22 exit
  16. const hostKey = "SHA256:qS+DxUoW0NM1Nc//Dh0ebUHTB+MXMNTr+BF0njciwrU"
  17. // 检查命令是否可用
  18. function commandExists(command) {
  19. try {
  20. execSync(`which ${command}`, { stdio: 'ignore' });
  21. return true;
  22. } catch (e) {
  23. return false;
  24. }
  25. }
  26. // 执行命令并打印输出
  27. function runCommand(command) {
  28. console.log(`执行命令: ${command}`);
  29. try {
  30. if (command.startsWith('sshpass') || command.startsWith('ssh')) {
  31. // 为SSH命令设置LC_ALL
  32. command = `LC_ALL=C ${command}`;
  33. }
  34. execSync(command, { stdio: 'inherit' });
  35. } catch (error) {
  36. console.error(`命令执行失败: ${error.message}`);
  37. process.exit(1);
  38. }
  39. }
  40. // 上传到服务器
  41. function uploadServer() {
  42. const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
  43. if (process.platform === 'win32') {
  44. // Windows
  45. runCommand(`pscp -P ${sshPort} -pw ${password} -hostkey "${hostKey}" ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  46. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
  47. } else {
  48. // Mac/Linux
  49. runCommand(`sshpass -p "${password}" scp -o StrictHostKeyChecking=no -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  50. runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  51. }
  52. console.log('编译打包后自动部署到服务器上完成');
  53. console.log(`服务器上的地址:${url}`);
  54. }
  55. // 删除 plugins 等目录
  56. function delPublic() {
  57. console.log('准备移除 plugins 等目录');
  58. const sshCommand = `cd ${filePath} && rm -rf plugins`;
  59. if (process.platform === 'win32') {
  60. // Windows
  61. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
  62. } else {
  63. // Mac/Linux
  64. runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  65. }
  66. console.log('plugins 等目录移除完成');
  67. }
  68. //执行
  69. function runCode() {
  70. //参数是否为空
  71. if (args === 'undefined' || serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
  72. console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
  73. process.exit(1);
  74. return
  75. }
  76. if (isNullES(args) || isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
  77. console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
  78. process.exit(1);
  79. return
  80. }
  81. //判断命令是否支持
  82. if (process.platform === 'win32') {
  83. if (!commandExists('pscp') || !commandExists('plink')) {
  84. console.log('putty 未安装,请先安装 putty');
  85. process.exit(1);
  86. return
  87. }
  88. } else {
  89. if (!commandExists('sshpass')) {
  90. console.log('sshpass 未安装,请先安装 sshpass');
  91. process.exit(1);
  92. return
  93. }
  94. }
  95. //如果是上传全部,就删除不常动的目录
  96. if (args === 'all') {
  97. delPublic();
  98. }
  99. //上传到服务器
  100. uploadServer();
  101. }
  102. runCode()