upload.js 3.7 KB

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