upload.js 3.4 KB

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