Skip to content

Commit

Permalink
start using deep readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Sep 10, 2024
1 parent 188352f commit 7539a62
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/bump/setDependentVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { bumpMinSemverRange } from './bumpMinSemverRange';
*/
export function setDependentVersions(
packageInfos: PackageInfos,
scopedPackages: Set<string>,
scopedPackages: ReadonlySet<string>,
options: Pick<BeachballOptions, 'verbose'>
): BumpInfo['dependentChangedBy'] {
const { verbose } = options;
Expand Down
11 changes: 5 additions & 6 deletions src/changefile/unlinkChangeFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import { ChangeSet } from '../types/ChangeInfo';
import { getChangePath } from '../paths';
import fs from 'fs-extra';
import path from 'path';
import { PackageInfo } from '../types/PackageInfo';
import { PackageInfos } from '../types/PackageInfo';
import type { BeachballOptions } from '../types/BeachballOptions';
import { DeepReadonly } from '../types/DeepReadonly';

/**
* Unlink only change files that are specified in the changes param
*
* @param changes existing change files to be removed
*/
export function unlinkChangeFiles(
changeSet: ChangeSet,
packageInfos: {
[pkg: string]: PackageInfo;
},
changeSet: DeepReadonly<ChangeSet>,
packageInfos: PackageInfos,
options: Pick<BeachballOptions, 'path' | 'changeDir'>
): void {
const changePath = getChangePath(options);
if (!changeSet || !changeSet.length) {
if (!changeSet?.length) {
return;
}
console.log('Removing change files:');
Expand Down
3 changes: 2 additions & 1 deletion src/changelog/getPackageChangelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getChangePath } from '../paths';
import { getFileAddedHash } from 'workspace-tools';
import { ChangeSet } from '../types/ChangeInfo';
import type { BeachballOptions } from '../types/BeachballOptions';
import { DeepReadonly } from '../types/DeepReadonly';

/**
* Used for `ChangelogEntry.commit` if the commit hash is not available.
Expand All @@ -18,7 +19,7 @@ const commitNotAvailable = 'not available';
* @returns Mapping from package name to package changelog.
*/
export function getPackageChangelogs(params: {
changeFileChangeInfos: ChangeSet;
changeFileChangeInfos: DeepReadonly<ChangeSet>;
calculatedChangeTypes: BumpInfo['calculatedChangeTypes'];
dependentChangedBy?: BumpInfo['dependentChangedBy'];
packageInfos: PackageInfos;
Expand Down
3 changes: 2 additions & 1 deletion src/changelog/writeChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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'>,
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function writeChangelog(
*/
async function writeGroupedChangelog(
options: BeachballOptions,
changeFileChangeInfos: ChangeSet,
changeFileChangeInfos: DeepReadonly<ChangeSet>,
calculatedChangeTypes: BumpInfo['calculatedChangeTypes'],
packageInfos: PackageInfos
): Promise<string[]> {
Expand Down
7 changes: 4 additions & 3 deletions src/types/BumpInfo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ChangeSet, ChangeType } from './ChangeInfo';
import { DeepReadonly } from './DeepReadonly';
import { PackageInfos, PackageGroups } from './PackageInfo';

export type BumpInfo = {
/** Changes coming from the change files */
changeFileChangeInfos: ChangeSet;
changeFileChangeInfos: DeepReadonly<ChangeSet>;

/**
* Cached version of package info (e.g. package.json, package path).
Expand All @@ -15,7 +16,7 @@ export type BumpInfo = {
calculatedChangeTypes: { [pkgName: string]: ChangeType };

/** Package grouping */
packageGroups: PackageGroups;
packageGroups: DeepReadonly<PackageGroups>;

/** Set of packages that had been modified */
modifiedPackages: Set<string>;
Expand All @@ -27,7 +28,7 @@ export type BumpInfo = {
dependentChangedBy: { [pkgName: string]: Set<string> };

/** Set of packages that are in scope for this bump */
scopedPackages: Set<string>;
scopedPackages: ReadonlySet<string>;
};

/** Dependents cache (child points to parents): if A depends on B, then `{ B: [A] }` */
Expand Down
17 changes: 17 additions & 0 deletions src/types/DeepReadonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type DeepReadonly<T> = T extends (infer R)[]
? DeepReadonlyArray<R>
: T extends Function
? T
: T extends Set<infer V>
? ReadonlySet<DeepReadonly<V>>
: T extends Map<infer K, infer V>
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
: T extends object
? DeepReadonlyObject<T>
: T;

interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}

type DeepReadonlyObject<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};

0 comments on commit 7539a62

Please sign in to comment.