patch.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * @Author: liyxt
  3. * @Date: 2019-12-09 19:54:41
  4. * @LastEditors : liyxt
  5. * @LastEditTime : 2019-12-31 09:44:54
  6. * @Description: file content
  7. */
  8. const configJSON = require('../config.json');
  9. const fs = require('fs');
  10. const { resolve, join, sep } = require('path');
  11. const { spawn } = require('child_process');
  12. const yazl = require('yazl');
  13. var zipfile = new yazl.ZipFile();
  14. var patchConfig = configJSON.patch || {};
  15. // 先删除dist目录
  16. delDir('./dist');
  17. delDir('./patch');
  18. let folderSet = new Set();
  19. // windows下npm执行名不同
  20. const ls = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', [
  21. 'run',
  22. 'test',
  23. ...(patchConfig.path || []).map(e => '--env.buildPath=' + e)
  24. ]);
  25. ls.stdout.on('data', data => {
  26. if (data.includes('ERROR')) {
  27. throw new Error(data);
  28. } else {
  29. data && console.log(`${data}`);
  30. }
  31. });
  32. ls.stderr.on('data', data => {
  33. if (data.includes('ERROR')) {
  34. throw new Error(data);
  35. } else {
  36. data && console.log(`${data}`);
  37. }
  38. });
  39. ls.on('close', code => {
  40. folderSet.clear();
  41. // 加入到zip入口
  42. addEntry(resolve(__dirname, '../dist'));
  43. // 动态修改xml
  44. let xmlconfig = {
  45. id: uuid(),
  46. provider: patchConfig.provider,
  47. department: patchConfig.department,
  48. needRecreatedLoginJar: false,
  49. needDeploy: false,
  50. time: dateFormat('YYYY-mm-dd HH:MM:SS', new Date()),
  51. patchKey: [...folderSet].join(',')
  52. };
  53. let xml = fs.readFileSync(resolve(__dirname, '../config/packmetadata.xml'), 'utf-8');
  54. Object.entries(xmlconfig).forEach(([key, value]) => {
  55. xml = xml.replace(`<!-- ${key} -->`, `<${key}>${value}</${key}>`);
  56. });
  57. fs.writeFileSync(resolve(__dirname, '../dist/packmetadata.xml'), xml, 'utf-8');
  58. zipfile.addFile('./dist/packmetadata.xml', 'packmetadata.xml');
  59. zipfile.outputStream.pipe(fs.createWriteStream(`patch_${new Date().getTime()}.zip`)).on('close', function() {
  60. console.log('补丁已出!');
  61. });
  62. zipfile.end();
  63. });
  64. function delDir(path) {
  65. let files = [];
  66. if (fs.existsSync(path)) {
  67. files = fs.readdirSync(path);
  68. files.forEach(file => {
  69. let curPath = path + '/' + file;
  70. if (fs.statSync(curPath).isDirectory()) {
  71. delDir(curPath); //递归删除文件夹
  72. } else {
  73. fs.unlinkSync(curPath); //删除文件
  74. }
  75. });
  76. fs.rmdirSync(path);
  77. }
  78. }
  79. function uuid() {
  80. var s = [];
  81. var hexDigits = '0123456789abcdef';
  82. for (var i = 0; i < 36; i++) {
  83. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  84. }
  85. s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
  86. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  87. s[8] = s[13] = s[18] = s[23] = '-';
  88. var uuid = s.join('');
  89. return uuid;
  90. }
  91. function addEntry(prefix = './dist') {
  92. //读取目录
  93. var paths = fs.readdirSync(prefix);
  94. paths.forEach(function(path) {
  95. var from = join(prefix, path);
  96. var st = fs.statSync(from);
  97. if (st.isFile()) {
  98. let folder = '/hotwebs/nccloud/resources/' + prefix.split(`${sep}dist${sep}`)[1];
  99. folderSet.add(folder);
  100. folder = join('replacement', folder);
  101. zipfile.addFile(resolve(__dirname, 'ncc_patch'), join(folder, 'ncc_patch'));
  102. zipfile.addFile(from, join(folder, path));
  103. } else if (st.isDirectory()) {
  104. addEntry(from);
  105. }
  106. });
  107. }
  108. function dateFormat(fmt, date) {
  109. let ret;
  110. let opt = {
  111. 'Y+': date.getFullYear().toString(), // 年
  112. 'm+': (date.getMonth() + 1).toString(), // 月
  113. 'd+': date.getDate().toString(), // 日
  114. 'H+': date.getHours().toString(), // 时
  115. 'M+': date.getMinutes().toString(), // 分
  116. 'S+': date.getSeconds().toString() // 秒
  117. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  118. };
  119. for (let k in opt) {
  120. ret = new RegExp('(' + k + ')').exec(fmt);
  121. if (ret) {
  122. fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
  123. }
  124. }
  125. return fmt;
  126. }