generate-changelog.ts 3.3 KB

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