server.js 3.1 KB

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