Code Infra

This is the documentation for the MUI Internal Code Infra package.

You can install this package using:

pnpm install @mui/internal-code-infra

Usage

TODO

Changelog data

fetchChangelogData collects what happened in a release as plain data: the release, its changes and its packages. Turn it into a changelog with code in your own repository:

import { fetchChangelogData } from '@mui/internal-code-infra/changelog';

const { previous, version, changes } = await fetchChangelogData();

const lines = changes.map((change) => `- ${change.title} (${change.url})`);
console.log(`## ${version}\n\nChanges since ${previous.ref}:\n\n${lines.join('\n')}`);

Run inside a checkout, every option has a default: the release is HEAD, and it starts at the closest version tag behind it. Commits without a pull request are left out unless includeDirectCommits is set.

Changes of Renovate are left out: a change is left out when one of the apps in excludeBots, by default ['app/renovate', 'app/code-infra-renovate'], authored it or opened its pull request. Bots are named as in GitHub search, such as app/renovate, or by login, such as renovate[bot]. [] keeps them all. To leave out changes of all bots, filter on change.author?.isBot. Copilot isn't one: a pull request it opened has the person who asked for it as the author. To leave those out too, filter on change.pullRequest?.openedBy?.login === 'Copilot'. A cherry-pick that a bot opened has the original author: filter on change.pullRequest?.openedBy?.isBot to leave it out too.

Changelog code

A repository formats its changelog with its own code, made of the pieces below. Keep it a function of the data:

import {
  bulletList,
  creditAuthors,
  formatAuthor,
  formatReference,
  formatSubHeader,
  groupBy,
  joinBlocks,
  joinWords,
  labelValues,
  readLabels,
  section,
  sortByTitle,
} from '@mui/internal-code-infra/changelog';

/** What the prefixes of a title stand for. */
const PREFIXES = { Button: ['component: button'] };

/** @param {import('@mui/internal-code-infra/changelog').ChangelogData} data */
export function formatChangelog(data) {
  const changes = data.changes.filter((change) => !change.labels.includes('dependencies'));
  const groups = groupBy(changes, (change) => {
    const components = labelValues(readLabels(change, PREFIXES), 'component');
    return components.length > 0 ? components : 'other';
  });

  return joinBlocks(
    `## ${data.version}`,
    formatSubHeader(data),
    [...groups].map(([name, group]) =>
      section(
        `### ${name}`,
        bulletList(
          sortByTitle(group).map((change) =>
            joinWords(change.title, `(${formatReference(change)})`, formatAuthor(change.author)),
          ),
        ),
      ),
    ),
    `Thanks to ${creditAuthors(changes).all.map(formatAuthor).join(', ')}`,
  );
}
import { parseArgs } from 'node:util';
import { fetchChangelogData } from '@mui/internal-code-infra/changelog';
import { formatChangelog } from './changelog.mjs';

const { values } = parseArgs({
  options: { release: { type: 'string' }, lastRelease: { type: 'string' } },
});
console.log(formatChangelog(await fetchChangelogData(values)));

The data is serializable: save it to format a release again, or to test the code of the changelog without network or Git.

PieceWhat it does
parseTitle(title)Splits the leading [prefixes] off a title.
matchPrefix(prefix, candidates)Finds the candidate that a prefix is a spelling of.
mapPrefixes(title, rewrite)Rewrites the prefixes of a title, for example to spell them the same everywhere.
readLabels(change, prefixes)Reads the labels of a change, and the ones that its prefixes stand for. A label wins over the prefixes for its kind.
labelValues(labels, kind)Reads the values of a kind: data grid for the scope in scope: data grid.
groupBy(items, getGroups)Groups items, which can be in several groups or in none.
sortByTitle(changes)Sorts by prefixes, then by the rest of the title.
creditAuthors(changes, { exclude })Lists the authors of changes once each, also split into team and community.
joinBlocks(...blocks), section(heading, ...)Join blocks with a blank line in between. Empty blocks, and sections with nothing in them, are left out.
bulletList(items), joinWords(...parts)Join the items of a list, and the parts of a line. Empty ones are left out.
formatReference(change), formatAuthor(author)Format the pull request or commit of a change, and its author. An author without a GitHub account is mentioned by name.
formatSubHeader(data)Formats what is below the heading of a release: a comment with the range it was generated from, and its date.
formatDate(date)Formats the date of a release.