generate-changelog.ts 3.0 KB

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