Skip to content

refactor(generate:changeset): Condense front matter into single section #24251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class GenerateChangeLogCommand extends BaseCommand<
/**
* Removes any custom metadata from all changesets and writes the resulting changes back to the source files. This
* metadata needs to be removed prior to running `changeset version` from the \@changesets/cli package. If it is not,
* then the custom metadata is interpreted as part of the content and the changelogs end up with the metadata in them.
* then the custom metadata is interpreted as change metadata and the changeset tools fail.
*
* For more information about the custom metadata we use in our changesets, see
* https://github.com/microsoft/FluidFramework/wiki/Changesets#custom-metadata
Expand All @@ -113,10 +113,12 @@ export default class GenerateChangeLogCommand extends BaseCommand<

const toWrite: Promise<void>[] = [];
for (const changeset of changesets) {
const metadata = Object.entries(changeset.metadata).map((entry) => {
const [packageName, bump] = entry;
return `"${packageName}": ${bump}`;
});
// Filter out properties that start with __
const metadata = Object.entries(changeset.metadata)
.filter(([key]) => !key.startsWith("__"))
.map(([packageName, bump]) => {
return `"${packageName}": ${bump}`;
});
const output = `---\n${metadata.join("\n")}\n---\n\n${changeset.summary}\n\n${changeset.body}\n`;
this.info(`Writing canonical changeset: ${changeset.sourceFile}`);
toWrite.push(writeFile(changeset.sourceFile, output));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,21 @@ function createChangesetContent(
const frontMatterSeparator = "---";

const lines: string[] = [frontMatterSeparator];

// Add package version bumps
for (const [pkg, bump] of packages.entries()) {
lines.push(`"${pkg.name}": ${bump}`);
}
lines.push(frontMatterSeparator);

// Add Fluid-specific metadata with __ prefix
if (additionalMetadata !== undefined) {
lines.push(frontMatterSeparator);
for (const [name, value] of Object.entries(additionalMetadata)) {
lines.push(`"${name}": ${value}`);
lines.push(`"__${name}": ${value}`);
}
lines.push(
frontMatterSeparator,
// an extra empty line after the front matter
"",
);
}

lines.push(frontMatterSeparator);

const frontMatter = lines.join("\n");
const changesetContents = [frontMatter, body].join("\n");
return changesetContents;
Expand Down
34 changes: 17 additions & 17 deletions build-tools/packages/build-cli/src/library/changesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,21 @@ export async function loadChangesets(dir: string, log?: Logger): Promise<Changes
// eslint-disable-next-line no-await-in-loop
const rawFileContent = await readFile(file, { encoding: "utf8" });

// Parse out the first layer of metadata, which is the package --> change type mapping.
const firstParse = matter(rawFileContent);
const packageBumpTypeMetadata = firstParse.data;

// If there is a second frontmatter section, parse it as the additional metadata.
const hasAdditionalMetadata = hasFrontMatter(firstParse.content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have been the only use of hasFrontMatter... build-tools doesn't complain about unused imports?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks like the tsconfig option is disabled but the lint rule is not enabled. I'll follow up with another change to enable the lint rule and fix violations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up PR: #24272


let markdownContent: string;
let additionalMetadata: FluidCustomChangesetMetadata | undefined;
if (hasAdditionalMetadata) {
const secondParse = matter(firstParse.content);
additionalMetadata = secondParse.data;
markdownContent = secondParse.content.trim();
} else {
markdownContent = firstParse.content.trim();
// Parse the front matter
const { data: metadata, content: markdownContent } = matter(rawFileContent);

// Split the metadata into package version bumps and Fluid-specific metadata
const packageBumpTypeMetadata: { [pkg: string]: VersionBumpType } = {};
const additionalMetadata: FluidCustomChangesetMetadata = {};

for (const [key, value] of Object.entries(metadata)) {
if (key.startsWith("__")) {
// Remove __ prefix and add to additional metadata
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
additionalMetadata[key.slice(2) as keyof FluidCustomChangesetMetadata] = value;
} else {
packageBumpTypeMetadata[key] = value as VersionBumpType;
}
}

const paragraphs = markdownContent.trim().split("\n\n");
Expand All @@ -269,12 +269,12 @@ export async function loadChangesets(dir: string, log?: Logger): Promise<Changes
const newChangeset: Changeset = {
metadata: packageBumpTypeMetadata,
mainPackage: Object.keys(packageBumpTypeMetadata)[0],
additionalMetadata,
additionalMetadata:
Object.keys(additionalMetadata).length > 0 ? additionalMetadata : undefined,
body,
summary,
sourceFile: file,
commit,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
changeTypes: [...new Set(Object.values(packageBumpTypeMetadata))],
};

Expand Down