generate-changelog.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 'dashboard',
  27. 'elasticsearch-plugin',
  28. 'email-plugin',
  29. 'email',
  30. 'job-queue-plugin',
  31. 'payments-plugin',
  32. 'testing',
  33. 'ui-devkit',
  34. 'harden-plugin',
  35. 'stellate-plugin',
  36. 'sentry-plugin',
  37. ];
  38. const mainTemplate = fs.readFileSync(path.join(__dirname, 'template.hbs'), 'utf-8');
  39. const commitTemplate = fs.readFileSync(path.join(__dirname, 'commit.hbs'), 'utf-8');
  40. generateChangelogForPackage();
  41. /**
  42. * Generates changelog entries based on the conventional commits data.
  43. */
  44. function generateChangelogForPackage() {
  45. const changelogPath = path.join(__dirname, '../../', changelogFileName);
  46. const inStream = fs.createReadStream(changelogPath, { flags: 'a+' });
  47. const tempFile = path.join(__dirname, `__temp_changelog__`);
  48. conventionalChangelogCore(
  49. {
  50. transform: (commit: any, context: any) => {
  51. const includeCommit = VALID_TYPES.includes(commit.type) && scopeIsValid(commit.scope);
  52. if (includeCommit) {
  53. return context(null, commit);
  54. } else {
  55. return context(null, null);
  56. }
  57. },
  58. releaseCount: 1,
  59. outputUnreleased: true,
  60. },
  61. {
  62. version: require('../../lerna.json').version,
  63. },
  64. null,
  65. null,
  66. {
  67. mainTemplate,
  68. commitPartial: commitTemplate,
  69. finalizeContext(context: any, options: any, commits: any) {
  70. context.commitGroups.forEach(addHeaderToCommitGroup);
  71. return context;
  72. },
  73. },
  74. )
  75. .pipe(addStream(inStream))
  76. .pipe(fs.createWriteStream(tempFile))
  77. .on('finish', () => {
  78. fs.createReadStream(tempFile)
  79. .pipe(fs.createWriteStream(changelogPath))
  80. .on('finish', () => {
  81. fs.unlinkSync(tempFile);
  82. });
  83. });
  84. }
  85. function scopeIsValid(scope?: string): boolean {
  86. for (const validScope of VALID_SCOPES) {
  87. if (scope && scope.includes(validScope)) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * The `header` is a more human-readable version of the commit type, as used in the
  95. * template.hbs as a sub-heading.
  96. */
  97. function addHeaderToCommitGroup(commitGroup: any) {
  98. switch (commitGroup.title) {
  99. case 'fix':
  100. commitGroup.header = 'Fixes';
  101. break;
  102. case 'feat':
  103. commitGroup.header = 'Features';
  104. break;
  105. default:
  106. commitGroup.header = commitGroup.title.charAt(0).toUpperCase() + commitGroup.title.slice(1);
  107. break;
  108. }
  109. }