From b960e80cca17d2d9773c21a99046d8edace3e41a Mon Sep 17 00:00:00 2001 From: Tyler Butler Date: Thu, 3 Apr 2025 17:13:19 -0700 Subject: [PATCH] refactor(generate:changeset): Condense front matter into single section --- .../src/commands/generate/changelog.ts | 12 ++++--- .../src/commands/generate/changeset.ts | 14 ++++---- .../build-cli/src/library/changesets.ts | 34 +++++++++---------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/build-tools/packages/build-cli/src/commands/generate/changelog.ts b/build-tools/packages/build-cli/src/commands/generate/changelog.ts index e3ade1e0083f..7b2a0ea7060a 100644 --- a/build-tools/packages/build-cli/src/commands/generate/changelog.ts +++ b/build-tools/packages/build-cli/src/commands/generate/changelog.ts @@ -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 @@ -113,10 +113,12 @@ export default class GenerateChangeLogCommand extends BaseCommand< const toWrite: Promise[] = []; 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)); diff --git a/build-tools/packages/build-cli/src/commands/generate/changeset.ts b/build-tools/packages/build-cli/src/commands/generate/changeset.ts index bf05c9eac0a5..343878a97357 100644 --- a/build-tools/packages/build-cli/src/commands/generate/changeset.ts +++ b/build-tools/packages/build-cli/src/commands/generate/changeset.ts @@ -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; diff --git a/build-tools/packages/build-cli/src/library/changesets.ts b/build-tools/packages/build-cli/src/library/changesets.ts index 16168bc63b80..02ae901f3a2f 100644 --- a/build-tools/packages/build-cli/src/library/changesets.ts +++ b/build-tools/packages/build-cli/src/library/changesets.ts @@ -239,21 +239,21 @@ export async function loadChangesets(dir: string, log?: Logger): Promise 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); - - 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"); @@ -269,12 +269,12 @@ export async function loadChangesets(dir: string, log?: Logger): Promise 0 ? additionalMetadata : undefined, body, summary, sourceFile: file, commit, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment changeTypes: [...new Set(Object.values(packageBumpTypeMetadata))], };