From b339fa7e541a0b48a88dcca2f3e2d75ac3aa8155 Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Tue, 10 Sep 2024 00:11:35 -0700 Subject: [PATCH] Clarify signatures of various internal functions (#992) --- ...-dc9b80b5-e785-4805-ae63-1c9c9b387632.json | 7 +++ src/bump/bumpInPlace.ts | 4 +- src/bump/bumpPackageInfoVersion.ts | 6 ++- src/bump/setDependentVersions.ts | 6 +-- src/changefile/getChangedPackages.ts | 9 ++-- src/changelog/writeChangelog.ts | 48 ++++++++++--------- src/commands/canary.ts | 2 +- src/commands/init.ts | 2 +- src/commands/sync.ts | 2 +- src/validation/isChangeFileNeeded.ts | 6 ++- 10 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 change/beachball-dc9b80b5-e785-4805-ae63-1c9c9b387632.json diff --git a/change/beachball-dc9b80b5-e785-4805-ae63-1c9c9b387632.json b/change/beachball-dc9b80b5-e785-4805-ae63-1c9c9b387632.json new file mode 100644 index 000000000..9da2710f8 --- /dev/null +++ b/change/beachball-dc9b80b5-e785-4805-ae63-1c9c9b387632.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "Clarify signatures of various internal functions", + "packageName": "beachball", + "email": "elcraig@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/src/bump/bumpInPlace.ts b/src/bump/bumpInPlace.ts index 0198615b7..47ea81a0a 100644 --- a/src/bump/bumpInPlace.ts +++ b/src/bump/bumpInPlace.ts @@ -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) : {}; @@ -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)); } diff --git a/src/bump/bumpPackageInfoVersion.ts b/src/bump/bumpPackageInfoVersion.ts index 18f8b096e..ef8e5ed9d 100644 --- a/src/bump/bumpPackageInfoVersion.ts +++ b/src/bump/bumpPackageInfoVersion.ts @@ -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, + options: Pick +): void { const { calculatedChangeTypes, packageInfos, modifiedPackages } = bumpInfo; const info = packageInfos[pkgName]; const changeType = calculatedChangeTypes[pkgName]; diff --git a/src/bump/setDependentVersions.ts b/src/bump/setDependentVersions.ts index 6104c41d8..f59c8cf78 100644 --- a/src/bump/setDependentVersions.ts +++ b/src/bump/setDependentVersions.ts @@ -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'; /** @@ -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, + bumpInfo: Pick, options: Pick ): BumpInfo['dependentChangedBy'] { + const { packageInfos, scopedPackages } = bumpInfo; const { verbose } = options; const dependentChangedBy: BumpInfo['dependentChangedBy'] = {}; diff --git a/src/changefile/getChangedPackages.ts b/src/changefile/getChangedPackages.ts index d68b881f1..859fb5005 100644 --- a/src/changefile/getChangedPackages.ts +++ b/src/changefile/getChangedPackages.ts @@ -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); @@ -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(); // Loop through the change files, building up a set of packages that we can skip diff --git a/src/changelog/writeChangelog.ts b/src/changelog/writeChangelog.ts index 7d24c34b9..7db871c87 100644 --- a/src/changelog/writeChangelog.ts +++ b/src/changelog/writeChangelog.ts @@ -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'; @@ -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, - options: BeachballOptions + options: Pick ): Promise { const { changeFileChangeInfos, calculatedChangeTypes, dependentChangedBy, packageInfos } = bumpInfo; - const groupedChangelogDirs = await writeGroupedChangelog( - options, - changeFileChangeInfos, - calculatedChangeTypes, - packageInfos - ); + const groupedChangelogDirs = await writeGroupedChangelog(bumpInfo, options); const changelogs = getPackageChangelogs({ changeFileChangeInfos, @@ -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, + }); } } } @@ -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, - calculatedChangeTypes: BumpInfo['calculatedChangeTypes'], - packageInfos: PackageInfos + bumpInfo: Pick, + options: Pick ): Promise { + const { changeFileChangeInfos, calculatedChangeTypes, packageInfos } = bumpInfo; + // Get the changelog groups with absolute paths. const changelogGroups = options.changelog?.groups?.map(({ changelogPath, ...rest }) => ({ ...rest, @@ -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, + }); } } @@ -119,12 +122,13 @@ async function writeGroupedChangelog( return Object.keys(groupedChangelogs); } -async function writeChangelogFiles( - options: BeachballOptions, - newVersionChangelog: PackageChangelog, - changelogPath: string, - isGrouped: boolean -): Promise { +async function writeChangelogFiles(params: { + options: Pick; + newVersionChangelog: PackageChangelog; + changelogPath: string; + isGrouped: boolean; +}): Promise { + const { options, newVersionChangelog, changelogPath, isGrouped } = params; let previousJson: ChangelogJson | undefined; // Update CHANGELOG.json diff --git a/src/commands/canary.ts b/src/commands/canary.ts index ba79ced3d..9a9213e56 100644 --- a/src/commands/canary.ts +++ b/src/commands/canary.ts @@ -33,7 +33,7 @@ export async function canary(options: BeachballOptions): Promise { bumpInfo.packageInfos[pkg].version = newVersion; } - setDependentVersions(bumpInfo.packageInfos, bumpInfo.scopedPackages, options); + setDependentVersions(bumpInfo, options); await performBump(bumpInfo, options); diff --git a/src/commands/init.ts b/src/commands/init.ts index 2cc69d00b..39c7ebd2d 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -13,7 +13,7 @@ function errorExit(message: string): void { process.exit(1); } -export async function init(options: BeachballOptions): Promise { +export async function init(options: Pick): Promise { let root: string; try { root = findProjectRoot(options.path); diff --git a/src/commands/sync.ts b/src/commands/sync.ts index 8e9aaa0a4..c1ec91d33 100644 --- a/src/commands/sync.ts +++ b/src/commands/sync.ts @@ -31,7 +31,7 @@ export async function sync(options: BeachballOptions): Promise { } } - const dependentModifiedPackages = setDependentVersions(packageInfos, scopedPackages, options); + const dependentModifiedPackages = setDependentVersions({ packageInfos, scopedPackages }, options); Object.keys(dependentModifiedPackages).forEach(pkg => modifiedPackages.add(pkg)); updatePackageJsons(modifiedPackages, packageInfos); diff --git a/src/validation/isChangeFileNeeded.ts b/src/validation/isChangeFileNeeded.ts index 6483861a5..1dcc0b653 100644 --- a/src/validation/isChangeFileNeeded.ts +++ b/src/validation/isChangeFileNeeded.ts @@ -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[0], + packageInfos: PackageInfos +): boolean { const { branch } = options; console.log(`Checking for changes against "${branch}"`);