Skip to content

Commit

Permalink
Clarify signatures of various internal functions (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 authored Sep 10, 2024
1 parent 5dec300 commit b339fa7
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 38 deletions.
7 changes: 7 additions & 0 deletions change/beachball-dc9b80b5-e785-4805-ae63-1c9c9b387632.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Clarify signatures of various internal functions",
"packageName": "beachball",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions src/bump/bumpInPlace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { setDependentVersions } from './setDependentVersions';
*/
export function bumpInPlace(bumpInfo: BumpInfo, options: BeachballOptions): void {
const { bumpDeps } = options;
const { packageInfos, scopedPackages, calculatedChangeTypes, changeFileChangeInfos, modifiedPackages } = bumpInfo;
const { calculatedChangeTypes, changeFileChangeInfos, modifiedPackages } = bumpInfo;

// pass 1: figure out all the change types for all the packages taking into account the bumpDeps option and version groups
const dependents = bumpDeps ? getDependentsForPackages(bumpInfo) : {};
Expand Down Expand Up @@ -45,6 +45,6 @@ export function bumpInPlace(bumpInfo: BumpInfo, options: BeachballOptions): void
});

// step 4: Bump all the dependencies packages
bumpInfo.dependentChangedBy = setDependentVersions(packageInfos, scopedPackages, options);
bumpInfo.dependentChangedBy = setDependentVersions(bumpInfo, options);
Object.keys(bumpInfo.dependentChangedBy).forEach(pkg => modifiedPackages.add(pkg));
}
6 changes: 5 additions & 1 deletion src/bump/bumpPackageInfoVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { BeachballOptions } from '../types/BeachballOptions';
* Bumps an individual package version based on the change type.
* **This mutates `info.version`!**
*/
export function bumpPackageInfoVersion(pkgName: string, bumpInfo: BumpInfo, options: BeachballOptions): void {
export function bumpPackageInfoVersion(
pkgName: string,
bumpInfo: Pick<BumpInfo, 'calculatedChangeTypes' | 'packageInfos' | 'modifiedPackages'>,
options: Pick<BeachballOptions, 'prereleasePrefix' | 'identifierBase'>
): void {
const { calculatedChangeTypes, packageInfos, modifiedPackages } = bumpInfo;
const info = packageInfos[pkgName];
const changeType = calculatedChangeTypes[pkgName];
Expand Down
6 changes: 3 additions & 3 deletions src/bump/setDependentVersions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BeachballOptions } from '../types/BeachballOptions';
import { BumpInfo } from '../types/BumpInfo';
import { consideredDependencies, type PackageInfos } from '../types/PackageInfo';
import { consideredDependencies } from '../types/PackageInfo';
import { bumpMinSemverRange } from './bumpMinSemverRange';

/**
Expand All @@ -9,10 +9,10 @@ import { bumpMinSemverRange } from './bumpMinSemverRange';
* **This mutates dep versions in `packageInfos`** as well as returning `dependentChangedBy`.
*/
export function setDependentVersions(
packageInfos: PackageInfos,
scopedPackages: ReadonlySet<string>,
bumpInfo: Pick<BumpInfo, 'packageInfos' | 'scopedPackages'>,
options: Pick<BeachballOptions, 'verbose'>
): BumpInfo['dependentChangedBy'] {
const { packageInfos, scopedPackages } = bumpInfo;
const { verbose } = options;
const dependentChangedBy: BumpInfo['dependentChangedBy'] = {};

Expand Down
9 changes: 4 additions & 5 deletions src/changefile/getChangedPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function getAllChangedPackages(options: BeachballOptions, packageInfos: PackageI
/**
* Gets all the changed packages which do not already have a change file
*/
export function getChangedPackages(options: BeachballOptions, packageInfos: PackageInfos): string[] {
export function getChangedPackages(options: BeachballOptions, packageInfos: PackageInfos) {
const { path: cwd, branch, changeDir } = options;

const changePath = getChangePath(options);
Expand All @@ -139,17 +139,16 @@ export function getChangedPackages(options: BeachballOptions, packageInfos: Pack

const changedPackages = getAllChangedPackages(options, packageInfos);

const changeFilesResult = git(
const changedFilesResult = git(
['diff', '--name-only', '--relative', '--no-renames', '--diff-filter=A', `${branch}...`],
{ cwd }
);

if (!fs.existsSync(changePath) || !changeFilesResult.success) {
if (!fs.existsSync(changePath) || !changedFilesResult.success) {
return changedPackages;
}

const changes = changeFilesResult.stdout.split(/\n/);
const changeFiles = changes.filter(name => path.dirname(name) === changeDir);
const changeFiles = changedFilesResult.stdout.split('\n').filter(name => path.dirname(name) === changeDir);
const changeFilePackageSet = new Set<string>();

// Loop through the change files, building up a set of packages that we can skip
Expand Down
48 changes: 26 additions & 22 deletions src/changelog/writeChangelog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs-extra';
import _ from 'lodash';
import { PackageInfo, PackageInfos } from '../types/PackageInfo';
import { PackageInfo } from '../types/PackageInfo';
import { getPackageChangelogs } from './getPackageChangelogs';
import { renderChangelog } from './renderChangelog';
import { renderJsonChangelog } from './renderJsonChangelog';
Expand All @@ -10,21 +10,14 @@ import { BumpInfo } from '../types/BumpInfo';
import { isPathIncluded } from '../monorepo/isPathIncluded';
import { PackageChangelog, ChangelogJson } from '../types/ChangeLog';
import { mergeChangelogs } from './mergeChangelogs';
import { ChangeSet } from '../types/ChangeInfo';
import { DeepReadonly } from '../types/DeepReadonly';

export async function writeChangelog(
bumpInfo: Pick<BumpInfo, 'changeFileChangeInfos' | 'calculatedChangeTypes' | 'dependentChangedBy' | 'packageInfos'>,
options: BeachballOptions
options: Pick<BeachballOptions, 'changeDir' | 'changelog' | 'generateChangelog' | 'path'>
): Promise<void> {
const { changeFileChangeInfos, calculatedChangeTypes, dependentChangedBy, packageInfos } = bumpInfo;

const groupedChangelogDirs = await writeGroupedChangelog(
options,
changeFileChangeInfos,
calculatedChangeTypes,
packageInfos
);
const groupedChangelogDirs = await writeGroupedChangelog(bumpInfo, options);

const changelogs = getPackageChangelogs({
changeFileChangeInfos,
Expand All @@ -40,7 +33,12 @@ export async function writeChangelog(
for (const pkg of Object.keys(changelogs)) {
const packagePath = path.dirname(packageInfos[pkg].packageJsonPath);
if (!groupedChangelogDirs.includes(packagePath)) {
await writeChangelogFiles(options, changelogs[pkg], packagePath, false);
await writeChangelogFiles({
options,
newVersionChangelog: changelogs[pkg],
changelogPath: packagePath,
isGrouped: false,
});
}
}
}
Expand All @@ -50,11 +48,11 @@ export async function writeChangelog(
* @returns The list of directories where grouped changelogs were written.
*/
async function writeGroupedChangelog(
options: BeachballOptions,
changeFileChangeInfos: DeepReadonly<ChangeSet>,
calculatedChangeTypes: BumpInfo['calculatedChangeTypes'],
packageInfos: PackageInfos
bumpInfo: Pick<BumpInfo, 'changeFileChangeInfos' | 'calculatedChangeTypes' | 'packageInfos'>,
options: Pick<BeachballOptions, 'changeDir' | 'changelog' | 'generateChangelog' | 'path'>
): Promise<string[]> {
const { changeFileChangeInfos, calculatedChangeTypes, packageInfos } = bumpInfo;

// Get the changelog groups with absolute paths.
const changelogGroups = options.changelog?.groups?.map(({ changelogPath, ...rest }) => ({
...rest,
Expand Down Expand Up @@ -107,7 +105,12 @@ async function writeGroupedChangelog(
for (const [changelogAbsDir, { masterPackage, changelogs }] of Object.entries(groupedChangelogs)) {
const groupedChangelog = mergeChangelogs(changelogs, masterPackage);
if (groupedChangelog) {
await writeChangelogFiles(options, groupedChangelog, changelogAbsDir, true);
await writeChangelogFiles({
options,
newVersionChangelog: groupedChangelog,
changelogPath: changelogAbsDir,
isGrouped: true,
});
}
}

Expand All @@ -119,12 +122,13 @@ async function writeGroupedChangelog(
return Object.keys(groupedChangelogs);
}

async function writeChangelogFiles(
options: BeachballOptions,
newVersionChangelog: PackageChangelog,
changelogPath: string,
isGrouped: boolean
): Promise<void> {
async function writeChangelogFiles(params: {
options: Pick<BeachballOptions, 'generateChangelog' | 'changelog'>;
newVersionChangelog: PackageChangelog;
changelogPath: string;
isGrouped: boolean;
}): Promise<void> {
const { options, newVersionChangelog, changelogPath, isGrouped } = params;
let previousJson: ChangelogJson | undefined;

// Update CHANGELOG.json
Expand Down
2 changes: 1 addition & 1 deletion src/commands/canary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function canary(options: BeachballOptions): Promise<void> {
bumpInfo.packageInfos[pkg].version = newVersion;
}

setDependentVersions(bumpInfo.packageInfos, bumpInfo.scopedPackages, options);
setDependentVersions(bumpInfo, options);

await performBump(bumpInfo, options);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function errorExit(message: string): void {
process.exit(1);
}

export async function init(options: BeachballOptions): Promise<void> {
export async function init(options: Pick<BeachballOptions, 'path'>): Promise<void> {
let root: string;
try {
root = findProjectRoot(options.path);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function sync(options: BeachballOptions): Promise<void> {
}
}

const dependentModifiedPackages = setDependentVersions(packageInfos, scopedPackages, options);
const dependentModifiedPackages = setDependentVersions({ packageInfos, scopedPackages }, options);
Object.keys(dependentModifiedPackages).forEach(pkg => modifiedPackages.add(pkg));

updatePackageJsons(modifiedPackages, packageInfos);
Expand Down
6 changes: 4 additions & 2 deletions src/validation/isChangeFileNeeded.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getChangedPackages } from '../changefile/getChangedPackages';
import { BeachballOptions } from '../types/BeachballOptions';
import { PackageInfos } from '../types/PackageInfo';

export function isChangeFileNeeded(options: BeachballOptions, packageInfos: PackageInfos): boolean {
export function isChangeFileNeeded(
options: Parameters<typeof getChangedPackages>[0],
packageInfos: PackageInfos
): boolean {
const { branch } = options;

console.log(`Checking for changes against "${branch}"`);
Expand Down

0 comments on commit b339fa7

Please sign in to comment.