upload.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // 检查命令是否可用
  15. function commandExists(command) {
  16. try {
  17. execSync(`which ${command}`, { stdio: 'ignore' });
  18. return true;
  19. } catch (e) {
  20. return false;
  21. }
  22. }
  23. // 执行命令并打印输出
  24. function runCommand(command) {
  25. console.log(`执行命令: ${command}`);
  26. try {
  27. if (command.startsWith('sshpass') || command.startsWith('ssh')) {
  28. // 为SSH命令设置LC_ALL
  29. command = `LC_ALL=C ${command}`;
  30. }
  31. execSync(command, { stdio: 'inherit' });
  32. } catch (error) {
  33. console.error(`命令执行失败: ${error.message}`);
  34. process.exit(1);
  35. }
  36. }
  37. // 上传到服务器
  38. function uploadServer() {
  39. const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
  40. if (process.platform === 'win32') {
  41. // Windows
  42. runCommand(`pscp -P ${sshPort} -pw ${password} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  43. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
  44. } else {
  45. // Mac/Linux
  46. runCommand(`sshpass -p "${password}" scp -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
  47. runCommand(`sshpass -p "${password}" ssh -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  48. }
  49. console.log('编译打包后自动部署到服务器上完成');
  50. console.log(`服务器上的地址:${url}`);
  51. }
  52. // 删除 plugins 等目录
  53. function delPublic() {
  54. console.log('准备移除 plugins 等目录');
  55. const sshCommand = `cd ${filePath} && rm -rf plugins`;
  56. if (process.platform === 'win32') {
  57. // Windows
  58. runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
  59. } else {
  60. // Mac/Linux
  61. runCommand(`sshpass -p "${password}" ssh -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
  62. }
  63. console.log('plugins 等目录移除完成');
  64. }
  65. //执行
  66. function runCode() {
  67. //参数是否为空
  68. if (args === 'undefined' || serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
  69. console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
  70. process.exit(1);
  71. return
  72. }
  73. if (isNullES(args) || isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
  74. console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
  75. process.exit(1);
  76. return
  77. }
  78. //判断命令是否支持
  79. if (process.platform === 'win32') {
  80. if (!commandExists('putty')) {
  81. console.log('putty 未安装,请先安装 putty');
  82. process.exit(1);
  83. return
  84. }
  85. } else {
  86. if (!commandExists('sshpass')) {
  87. console.log('sshpass 未安装,请先安装 sshpass');
  88. process.exit(1);
  89. return
  90. }
  91. }
  92. //如果是上传全部,就删除不常动的目录
  93. if (args === 'all') {
  94. delPublic();
  95. }
  96. //上传到服务器
  97. uploadServer();
  98. }
  99. runCode()