upload.js 4.1 KB

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