generate-changelog.ts 3.3 KB

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