server.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const { execSync} = require('child_process');
  2. const { getObjValue, arrIndex, isNullES } = require('js-fast-way')
  3. const fs = require('fs');
  4. // 配置
  5. const config = [
  6. {
  7. name: 'test', //测试服
  8. ip: '192.168.0.109', //服务器地址
  9. username: 'root', //登录账号
  10. password: 'admin123@', //登录密码
  11. fileName: 'archives', //本地文件名
  12. path: '/www/wwwroot/archives.hczcxx.cn', //服务器上的文件路径
  13. url: 'http://192.168.0.109:5175/', //demo地址
  14. },
  15. {
  16. name: 'user', //正式服
  17. ip: '39.108.216.210', //服务器地址
  18. username: 'root', //登录账号
  19. password: '%CX0&qPRk2BitWPH^tCa5jxZKPYuST', //登录密码
  20. fileName: 'archives', //本地文件名
  21. path: '/www/wwwroot/archives.hczcxx.cn', //服务器上的文件路径
  22. url: 'https://archives.hczcxx.cn/', //demo地址
  23. },
  24. {
  25. name: '183', //甬台温
  26. ip: '183.247.216.148:237', //服务器地址
  27. username: 'root', //登录账号
  28. password: 'DAJL@20240520@root', //登录密码
  29. fileName: 'archives-local', //本地文件名
  30. path: '/www/wwwroot/archives.hczcxx.cn', //服务器上的文件路径
  31. url: 'http://183.247.216.148:6070/tz_web/', //demo地址
  32. },
  33. ];
  34. const argv = process.argv[2]
  35. const args = process.argv[3]
  36. // 执行命令并打印输出
  37. function runCommand(command) {
  38. console.log(`执行命令: ${command}`);
  39. try {
  40. execSync(command, { stdio: 'inherit' });
  41. } catch (error) {
  42. console.error(`命令执行失败: ${error.message}`);
  43. process.exit(1);
  44. }
  45. }
  46. //处理数据
  47. function setConfigData() {
  48. if (argv === 'undefined' || isNullES(argv)) {
  49. process.exit(1);
  50. return
  51. }
  52. console.log(`----------------------------`)
  53. console.log(`准备开始上传到服务器...`);
  54. if (argv === 'all') {
  55. //全部上传
  56. for (let i = 0; i < config.length; i++) {
  57. const {name, ip, username, password, fileName, path, url} = getObjValue(config[i])
  58. const file_name = `${fileName}-${args}.zip`
  59. //判断本地文件是否存在
  60. if (!fs.existsSync(`./zip/${file_name}`)) {
  61. console.error(`文件 ./zip/${file_name} 不存在`);
  62. process.exit(1);
  63. }
  64. console.log(`----------------------------`)
  65. console.log(`准备上传到服务器:${name}`);
  66. runCommand(`node ./scripts/upload/upload.js ${args} ${ip} ${username} '${password}' ${file_name} ${path} ${url}`);
  67. }
  68. } else {
  69. const index = arrIndex(config, 'name', argv)
  70. if (index === -1) {
  71. console.log('此平台不存在')
  72. process.exit(1);
  73. return
  74. }
  75. const {name, ip, username, password, fileName, path, url} = getObjValue(config[index])
  76. const file_name = `${fileName}-${args}.zip`
  77. //判断本地文件是否存在
  78. if (!fs.existsSync(`./zip/${file_name}`)) {
  79. console.error(`文件 ./zip/${file_name} 不存在`);
  80. process.exit(1);
  81. }
  82. console.log(`----------------------------`)
  83. console.log(`准备上传到服务器:${name}`);
  84. runCommand(`node ./scripts/upload/upload.js ${args} ${ip} ${username} '${password}' ${file_name} ${path} ${url}`);
  85. }
  86. console.log(`----------------------------`)
  87. console.log(`全部上传并部署完成`);
  88. }
  89. setConfigData()