generate-changelog.ts 3.0 KB

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