generate-changelog.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import fs from 'fs-extra';
  2. import path from 'path';
  3. import { addStream } from './add-stream';
  4. // eslint-disable-next-line @typescript-eslint/no-var-requires
  5. const conventionalChangelogCore = require('conventional-changelog-core');
  6. let changelogFileName = 'CHANGELOG.md';
  7. if (process.argv.includes('--next') || process.env.npm_config_argv?.includes('publish-prerelease')) {
  8. changelogFileName = 'CHANGELOG_NEXT.md';
  9. }
  10. /**
  11. * The types of commit which will be included in the changelog.
  12. */
  13. const VALID_TYPES = ['feat', 'fix', 'perf'];
  14. /**
  15. * Define which packages to create changelog entries for.
  16. */
  17. const VALID_SCOPES: string[] = [
  18. 'admin-ui-plugin',
  19. 'admin-ui',
  20. 'asset-server',
  21. 'asset-server-plugin',
  22. 'cli',
  23. 'common',
  24. 'core',
  25. 'create',
  26. 'elasticsearch-plugin',
  27. 'email-plugin',
  28. 'email',
  29. 'job-queue-plugin',
  30. 'payments-plugin',
  31. 'testing',
  32. 'ui-devkit',
  33. 'harden-plugin',
  34. 'stellate-plugin',
  35. 'sentry-plugin',
  36. ];
  37. const mainTemplate = fs.readFileSync(path.join(__dirname, 'template.hbs'), 'utf-8');
  38. const commitTemplate = fs.readFileSync(path.join(__dirname, 'commit.hbs'), 'utf-8');
  39. generateChangelogForPackage();
  40. /**
  41. * Generates changelog entries based on the conventional commits data.
  42. */
  43. function generateChangelogForPackage() {
  44. const changelogPath = path.join(__dirname, '../../', changelogFileName);
  45. const inStream = fs.createReadStream(changelogPath, { flags: 'a+' });
  46. const tempFile = path.join(__dirname, `__temp_changelog__`);
  47. conventionalChangelogCore(
  48. {
  49. transform: (commit: any, context: any) => {
  50. const includeCommit = VALID_TYPES.includes(commit.type) && scopeIsValid(commit.scope);
  51. if (includeCommit) {
  52. return context(null, commit);
  53. } else {
  54. return context(null, null);
  55. }
  56. },
  57. releaseCount: 1,
  58. outputUnreleased: true,
  59. },
  60. {
  61. version: require('../../lerna.json').version,
  62. },
  63. null,
  64. null,
  65. {
  66. mainTemplate,
  67. commitPartial: commitTemplate,
  68. finalizeContext(context: any, options: any, commits: any) {
  69. context.commitGroups.forEach(addHeaderToCommitGroup);
  70. return context;
  71. },
  72. },
  73. )
  74. .pipe(addStream(inStream))
  75. .pipe(fs.createWriteStream(tempFile))
  76. .on('finish', () => {
  77. fs.createReadStream(tempFile)
  78. .pipe(fs.createWriteStream(changelogPath))
  79. .on('finish', () => {
  80. fs.unlinkSync(tempFile);
  81. });
  82. });
  83. }
  84. function scopeIsValid(scope?: string): boolean {
  85. for (const validScope of VALID_SCOPES) {
  86. if (scope && scope.includes(validScope)) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * The `header` is a more human-readable version of the commit type, as used in the
  94. * template.hbs as a sub-heading.
  95. */
  96. function addHeaderToCommitGroup(commitGroup: any) {
  97. switch (commitGroup.title) {
  98. case 'fix':
  99. commitGroup.header = 'Fixes';
  100. break;
  101. case 'feat':
  102. commitGroup.header = 'Features';
  103. break;
  104. default:
  105. commitGroup.header = commitGroup.title.charAt(0).toUpperCase() + commitGroup.title.slice(1);
  106. break;
  107. }
  108. }