generate-changelog.ts 3.0 KB

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