updateChangelog.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fs = require('fs');
  2. const path = require('path');
  3. const changelog = fs.readFileSync(
  4. path.join(__dirname, '../CHANGELOG.md'),
  5. 'utf8'
  6. );
  7. const typeList = [
  8. { type: 'fix', icon: '🐛' },
  9. { type: 'feat', icon: '✨' },
  10. { type: 'chore', icon: '👷' },
  11. { type: 'docs', icon: '📝 ' },
  12. { type: 'style', icon: '💄' },
  13. { type: 'refactor', icon: '🎨' },
  14. { type: 'perf', icon: '⚡' },
  15. { type: 'test', icon: '✅ ' }
  16. ];
  17. const replaceMd = {
  18. content: '',
  19. setContent(content) {
  20. replaceMd.content = content;
  21. return replaceMd;
  22. },
  23. getContent() {
  24. return replaceMd.content;
  25. },
  26. //版本号改成二级标题
  27. changeTitle() {
  28. replaceMd.content = replaceMd.content.replace(
  29. /### (?=\[\d\.\d\.\d\])/g,
  30. '## '
  31. );
  32. return replaceMd;
  33. },
  34. //修改日期位置
  35. changeDate() {
  36. replaceMd.content = replaceMd.content.replace(
  37. /(?<=\[\d\.\d\.\d\]\([\s\S]+\)) \((\d\d\d\d\-\d\d\-\d\d)\)(?=\n)/g,
  38. '\n`$1`'
  39. );
  40. return replaceMd;
  41. },
  42. //增加type内容
  43. changeType() {
  44. function replaceType(type, icon) {
  45. replaceMd.content = replaceMd.content.replace(
  46. new RegExp(
  47. `(?<=### ${type}\\n\\n)\\* ([\\s\\S]+?)\\n+(?=[###|##])`,
  48. 'g'
  49. ),
  50. function(match) {
  51. return match.replace(
  52. new RegExp(`\\* ([\\s\\S]+?)(?=\\n)`, 'g'),
  53. `* ${icon} ${type}: $1`
  54. );
  55. }
  56. );
  57. }
  58. typeList.forEach(e => {
  59. replaceType(e.type, e.icon);
  60. });
  61. return replaceMd;
  62. },
  63. //删除type标题
  64. deleteType() {
  65. typeList.forEach(e => {
  66. replaceMd.content = replaceMd.content.replace(
  67. new RegExp(`### ${e.type}\\n+`, 'g'),
  68. ''
  69. );
  70. });
  71. return replaceMd;
  72. }
  73. };
  74. const newChangelog = replaceMd
  75. .setContent(changelog)
  76. .changeTitle()
  77. .changeDate()
  78. .changeType()
  79. .deleteType();
  80. fs.writeFileSync(
  81. path.join(__dirname, '../CHANGELOG.md'),
  82. newChangelog.getContent(),
  83. 'utf8'
  84. );