Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/create-release-branch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
"test:watch": "vitest --watch"
},
"dependencies": {
"@changesets/config": "^4.0.1",
"@changesets/get-release-plan": "^5.0.2",
"@changesets/types": "^7.0.0",
"@clack/prompts": "^1.8.1",
"@manypkg/get-packages": "^3.1.0",
"@metamask/action-utils": "^2.0.0",
"@metamask/auto-changelog": "^7.1.0",
"@metamask/utils": "^11.12.0",
Expand Down
12 changes: 3 additions & 9 deletions packages/create-release-branch/src/core/candidates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Why a package is offered for release: it has changes, it depends on a
* package that is being released, or it has entries in its changelog.
* Why a package is offered for release: it has changes, or it depends on a
* package that is being released.
*/
export type CandidateKind =
/**
Expand All @@ -11,13 +11,7 @@ export type CandidateKind =
/**
* The package is released only because one of its dependencies is released.
*/
| 'dependency'
/**
* Changesets mode only: the package has no changesets, but its changelog has
* hand-written entries in its Unreleased section (during the migration to
* Changesets).
*/
| 'legacy-entries';
| 'dependency';

/**
* A package that the maintainer can choose to include in the release.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,26 +387,23 @@ describe('release-steps', () => {
});
});

it('passes only changes and legacy-entries candidates as packages with changes', async () => {
it('passes only changes candidates as packages with changes', async () => {
const project = buildMockProject({
workspacePackages: {
a: buildMockPackage('a', '1.0.0', {
validatedManifest: {
dependencies: { changed: '1.0.0', dependent: '1.0.0' },
peerDependencies: { legacy: '1.0.0' },
},
}),
changed: buildMockPackage('changed', '1.0.0'),
dependent: buildMockPackage('dependent', '1.0.0', {
hasChangesSinceLatestRelease: true,
}),
legacy: buildMockPackage('legacy', '1.0.0'),
},
});
const candidates: Candidate[] = [
{ name: 'changed', currentVersion: '1.0.0', kind: 'changes' },
{ name: 'dependent', currentVersion: '1.0.0', kind: 'dependency' },
{ name: 'legacy', currentVersion: '1.0.0', kind: 'legacy-entries' },
];
const plan = vi.fn();
const mode = buildMockMode({
Expand All @@ -424,7 +421,7 @@ describe('release-steps', () => {
a: {
missingDirectDependentNames: [],
missingPeerDependentNames: [],
missingDependencies: ['changed', 'legacy'],
missingDependencies: ['changed'],
},
});
expect(plan).not.toHaveBeenCalled();
Expand Down
4 changes: 1 addition & 3 deletions packages/create-release-branch/src/core/release-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ export async function checkChoices<Data>({
}): Promise<CheckChoicesResult<Data>> {
const candidates = await mode.listCandidates(choices);
const packagesWithChanges = new Set(
candidates
.filter(({ kind }) => kind === 'changes' || kind === 'legacy-entries')
.map(({ name }) => name),
candidates.filter(({ kind }) => kind === 'changes').map(({ name }) => name),
);

const { errors, dependencyFindings } = checkReleaseChoices(project, choices, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { describe, expect, it } from 'vitest';

import {
buildMockChangesetsState,
buildMockPackage,
buildMockProject,
} from '../../../tests/unit/helpers.js';
import { compareNames, listChangesetsCandidates } from './candidates.js';

describe('candidates', () => {
describe('listChangesetsCandidates', () => {
it('lists the packages with changes, then with a dependency bump, each sorted by name', () => {
const project = buildMockProject({
workspacePackages: {
'@scope/b': buildMockPackage('@scope/b', '1.0.0'),
'@scope/a': buildMockPackage('@scope/a', '1.0.0'),
'@scope/c': buildMockPackage('@scope/c', '1.0.0'),
'@scope/z': buildMockPackage('@scope/z', '1.0.0'),
},
});
const state = buildMockChangesetsState({
fullPlan: {
changesets: [
{
id: 'brave-cats-sing',
summary: '',
releases: [{ name: '@scope/b', type: 'major' }],
},
{
id: 'calm-dogs-run',
summary: '',
releases: [
{ name: '@scope/a', type: 'patch' },
{ name: '@scope/b', type: 'patch' },
],
},
{
id: 'eager-owls-fly',
summary: '',
releases: [{ name: '@scope/a', type: 'patch' }],
},
],
releases: [
{
name: '@scope/z',
type: 'patch',
oldVersion: '1.0.0',
newVersion: '1.0.1',
changesets: [],
},
{
name: '@scope/b',
type: 'major',
oldVersion: '1.0.0',
newVersion: '2.0.0',
changesets: ['brave-cats-sing', 'calm-dogs-run'],
},
{
name: '@scope/c',
type: 'patch',
oldVersion: '1.0.0',
newVersion: '1.0.1',
changesets: [],
},
{
name: '@scope/a',
type: 'patch',
oldVersion: '1.0.0',
newVersion: '1.0.1',
changesets: ['calm-dogs-run', 'eager-owls-fly'],
},
],
preState: undefined,
},
});

expect(listChangesetsCandidates(project, state)).toStrictEqual([
{
name: '@scope/a',
currentVersion: '1.0.0',
plannedVersion: '1.0.1',
kind: 'changes',
changeCount: 2,
},
{
name: '@scope/b',
currentVersion: '1.0.0',
plannedVersion: '2.0.0',
kind: 'changes',
changeCount: 2,
},
{
name: '@scope/c',
currentVersion: '1.0.0',
plannedVersion: '1.0.1',
kind: 'dependency',
},
{
name: '@scope/z',
currentVersion: '1.0.0',
plannedVersion: '1.0.1',
kind: 'dependency',
},
]);
});

it('does not list or count a release of type "none"', () => {
const project = buildMockProject({
workspacePackages: {
'@scope/a': buildMockPackage('@scope/a', '1.0.0'),
'@scope/b': buildMockPackage('@scope/b', '1.0.0'),
},
});
const state = buildMockChangesetsState({
fullPlan: {
changesets: [
{
id: 'brave-cats-sing',
summary: '',
releases: [
{ name: '@scope/a', type: 'none' },
{ name: '@scope/b', type: 'patch' },
],
},
],
releases: [
{
name: '@scope/a',
type: 'none',
oldVersion: '1.0.0',
newVersion: '1.0.0',
changesets: ['brave-cats-sing'],
},
{
name: '@scope/b',
type: 'patch',
oldVersion: '1.0.0',
newVersion: '1.0.1',
changesets: ['brave-cats-sing'],
},
],
preState: undefined,
},
});

expect(listChangesetsCandidates(project, state)).toStrictEqual([
{
name: '@scope/b',
currentVersion: '1.0.0',
plannedVersion: '1.0.1',
kind: 'changes',
changeCount: 1,
},
]);
});

it('lists no package when there are no pending Changesets', () => {
const project = buildMockProject({
workspacePackages: {
'@scope/a': buildMockPackage('@scope/a', '1.0.0'),
},
});

expect(
listChangesetsCandidates(project, buildMockChangesetsState()),
).toStrictEqual([]);
});
});

describe('compareNames', () => {
it('orders names by code unit', () => {
expect(['b', 'B', 'a', 'a'].sort(compareNames)).toStrictEqual([
'B',
'a',
'a',
'b',
]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { ComprehensiveRelease } from '@changesets/types';

import type { Candidate, CandidateKind } from '../../core/candidates.js';
import type { Project } from '../../project/project.js';
import type { ChangesetsState } from './load.js';

/**
* A release that bumps the version of its package.
*/
export type BumpingRelease = Exclude<ComprehensiveRelease, { type: 'none' }>;

const KIND_ORDER: CandidateKind[] = ['changes', 'dependency'];

/**
* Compares two strings by code unit, whatever the locale.
*
* @param left - The first string.
* @param right - The second string.
* @returns A negative number, zero, or a positive number.
*/
export function compareNames(left: string, right: string): number {
return Number(left > right) - Number(left < right);
}

/**
* Determines whether a release bumps the version of its package.
*
* @param release - The release.
* @returns Whether the release type is not `none`.
*/
export function isBumpingRelease(
release: ComprehensiveRelease,
): release is BumpingRelease {
return release.type !== 'none';
}

/**
* Lists the packages that the full release plan bumps.
*
* @param project - The project.
* @param state - The Changesets state.
* @returns The candidates, grouped by kind and sorted by name.
*/
export function listChangesetsCandidates(
project: Project,
state: ChangesetsState,
): Candidate[] {
return state.fullPlan.releases
.filter(isBumpingRelease)
.map(({ name, newVersion }): Candidate => {
const currentVersion =
project.workspacePackages[name].validatedManifest.version.version;
const changeCount = state.fullPlan.changesets.filter(({ releases }) =>
releases.some(
(release) => release.name === name && release.type !== 'none',
),
).length;
return changeCount > 0
? {
name,
currentVersion,
plannedVersion: newVersion,
kind: 'changes',
changeCount,
}
: {
name,
currentVersion,
plannedVersion: newVersion,
kind: 'dependency',
};
})
.sort(
(left, right) =>
KIND_ORDER.indexOf(left.kind) - KIND_ORDER.indexOf(right.kind) ||
compareNames(left.name, right.name),
);
}
Loading
Loading