Skip to content

Commit

Permalink
Add writeChangelogJson option
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Sep 5, 2024
1 parent 34049ac commit 7873867
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 22 deletions.
7 changes: 7 additions & 0 deletions change/beachball-526ee2eb-1931-449a-b24e-cc00cf631525.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"comment": "Add writeChangelogJson option",
"type": "minor",
"packageName": "beachball",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion src/__e2e__/bump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ describe('version bumping', () => {
expect(modified).toContain('package2');

const changelogJson = readChangelogJson(repo.pathTo('packages/package2'));
expect(changelogJson.entries[0].comments.patch![0].comment).toBe('Bump package1 to v0.0.2');
expect(changelogJson?.entries[0].comments.patch![0].comment).toBe('Bump package1 to v0.0.2');
});

it('calls sync prebump hook before packages are bumped', async () => {
Expand Down
15 changes: 12 additions & 3 deletions src/__fixtures__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import { SortedChangeTypes } from '../changefile/changeTypes';
import { ChangelogJson } from '../types/ChangeLog';

/** Read the CHANGELOG.md under the given package path, sanitizing any dates for snapshots */
export function readChangelogMd(packagePath: string): string {
export function readChangelogMd(packagePath: string): string | undefined {
const changelogFile = path.join(packagePath, 'CHANGELOG.md');
if (!fs.existsSync(changelogFile)) {
return undefined;
}
const text = fs.readFileSync(changelogFile, { encoding: 'utf-8' });
return text.replace(/\w\w\w, \d\d \w\w\w [\d :]+?GMT/gm, '(date)');
}

/** Read the CHANGELOG.json under the given package path */
export function readChangelogJson(packagePath: string, cleanForSnapshot: boolean = false): ChangelogJson {
export function readChangelogJson(packagePath: string, cleanForSnapshot: boolean = false): ChangelogJson | undefined {
const changelogJsonFile = path.join(packagePath, 'CHANGELOG.json');
if (!fs.existsSync(changelogJsonFile)) {
return undefined;
}
const json = fs.readJSONSync(changelogJsonFile, { encoding: 'utf-8' });
return cleanForSnapshot ? cleanChangelogJson(json) : json;
}
Expand All @@ -22,7 +28,10 @@ export function readChangelogJson(packagePath: string, cleanForSnapshot: boolean
* Clean changelog json for a snapshot: replace dates and SHAs with placeholders.
* Note: this clones the changelog object rather than modifying the original.
*/
export function cleanChangelogJson(changelog: ChangelogJson): ChangelogJson {
export function cleanChangelogJson(changelog: ChangelogJson | undefined): ChangelogJson | undefined {
if (!changelog) {
return undefined;
}
changelog = _.cloneDeep(changelog);
// for a better snapshot, make the fake commit match if the real commit did
const fakeCommits: { [commit: string]: string } = {};
Expand Down
32 changes: 26 additions & 6 deletions src/__functional__/changelog/writeChangelog.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, beforeAll, afterAll, afterEach } from '@jest/globals';
import fs from 'fs';
import { generateChangeFiles } from '../../__fixtures__/changeFiles';
import { cleanChangelogJson, readChangelogJson, readChangelogMd } from '../../__fixtures__/changelog';
import { initMockLogs } from '../../__fixtures__/mockLogs';
Expand All @@ -8,16 +9,16 @@ import { writeChangelog } from '../../changelog/writeChangelog';
import { getPackageInfos } from '../../monorepo/getPackageInfos';
import { readChangeFiles } from '../../changefile/readChangeFiles';
import { BeachballOptions } from '../../types/BeachballOptions';
import { ChangeFileInfo, ChangeInfo } from '../../types/ChangeInfo';
import { ChangeFileInfo, ChangeInfo, ChangeType } from '../../types/ChangeInfo';
import type { Repository } from '../../__fixtures__/repository';
import { getDefaultOptions } from '../../options/getDefaultOptions';

function getChange(packageName: string, comment: string): ChangeFileInfo {
function getChange(packageName: string, comment: string, type: ChangeType = 'patch'): ChangeFileInfo {
return {
comment,
email: '[email protected]',
packageName,
type: 'patch',
type,
dependentChangeType: 'patch',
};
}
Expand Down Expand Up @@ -77,7 +78,7 @@ describe('writeChangelog', () => {
expect(cleanChangelogJson(changelogJson)).toMatchSnapshot('changelog json');

// Every entry should have a different commit hash
const patchComments = changelogJson.entries[0].comments.patch!;
const patchComments = changelogJson!.entries[0].comments.patch!;
const commits = patchComments.map(entry => entry.commit);
expect(new Set(commits).size).toEqual(patchComments.length);

Expand Down Expand Up @@ -111,7 +112,7 @@ describe('writeChangelog', () => {
expect(cleanChangelogJson(changelogJson)).toMatchSnapshot('changelog json');

// Every entry should have a different commit hash
const patchComments = changelogJson.entries[0].comments.patch!;
const patchComments = changelogJson!.entries[0].comments.patch!;
const commits = patchComments.map(entry => entry.commit);
expect(new Set(commits).size).toEqual(patchComments.length);

Expand Down Expand Up @@ -151,7 +152,7 @@ describe('writeChangelog', () => {
expect(readChangelogJson(repo.pathTo('packages/bar'), true /*clean*/)).toMatchSnapshot('bar CHANGELOG.json');

// Every entry should have a different commit hash
const patchComments = fooJson.entries[0].comments.patch!;
const patchComments = fooJson!.entries[0].comments.patch!;
const commits = patchComments.map(entry => entry.commit);
expect(new Set(commits).size).toEqual(patchComments.length);

Expand Down Expand Up @@ -343,4 +344,23 @@ describe('writeChangelog', () => {
}
}
});

it('does not write CHANGELOG.json if writeChangelogJson is false', async () => {
repo = repositoryFactory.cloneRepository();
const options = getOptions({ writeChangelogJson: false });

repo.commitChange('foo');
generateChangeFiles([getChange('foo', 'comment 1')], options);

const packageInfos = getPackageInfos(repo.rootPath);
const changes = readChangeFiles(options, packageInfos);

await writeChangelog(options, changes, { foo: 'patch' }, { foo: new Set(['foo']) }, packageInfos);

// CHANGELOG.md is written
expect(readChangelogMd(repo.rootPath)).toContain('## 1.0.0');

// CHANGELOG.json is not written
expect(fs.existsSync(repo.pathTo('CHANGELOG.json'))).toBe(false);
});
});
26 changes: 14 additions & 12 deletions src/changelog/writeChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,20 @@ async function writeChangelogFiles(
): Promise<void> {
let previousJson: ChangelogJson | undefined;

// Update CHANGELOG.json
const changelogJsonFile = path.join(changelogPath, 'CHANGELOG.json');
try {
previousJson = fs.existsSync(changelogJsonFile) ? fs.readJSONSync(changelogJsonFile) : undefined;
} catch (e) {
console.warn(`${changelogJsonFile} is invalid: ${e}`);
}
try {
const nextJson = renderJsonChangelog(newVersionChangelog, previousJson);
fs.writeJSONSync(changelogJsonFile, nextJson, { spaces: 2 });
} catch (e) {
console.warn(`Problem writing to ${changelogJsonFile}: ${e}`);
if (options.writeChangelogJson) {
// Update CHANGELOG.json
const changelogJsonFile = path.join(changelogPath, 'CHANGELOG.json');
try {
previousJson = fs.existsSync(changelogJsonFile) ? fs.readJSONSync(changelogJsonFile) : undefined;
} catch (e) {
console.warn(`${changelogJsonFile} is invalid: ${e}`);
}
try {
const nextJson = renderJsonChangelog(newVersionChangelog, previousJson);
fs.writeJSONSync(changelogJsonFile, nextJson, { spaces: 2 });
} catch (e) {
console.warn(`Problem writing to ${changelogJsonFile}: ${e}`);
}
}

// Update CHANGELOG.md
Expand Down
1 change: 1 addition & 0 deletions src/options/getDefaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function getDefaultOptions(): BeachballOptions {
timeout: undefined,
type: null,
version: false,
writeChangelogJson: true,
yes: env.isCI,
};
}
5 changes: 5 additions & 0 deletions src/types/BeachballOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ export interface RepoOptions {
groupChanges?: boolean;
/** For shallow clones only: Depth of git history to consider when doing fetch */
depth?: number;
/**
* Whether to write CHANGELOG.json files.
* @default true
*/
writeChangelogJson?: boolean;
}

export interface PackageOptions {
Expand Down

0 comments on commit 7873867

Please sign in to comment.