From aa91cd69acffd06db4febf10768d9003f1fd8426 Mon Sep 17 00:00:00 2001 From: Brian Hudson Date: Mon, 29 Jan 2024 15:54:06 -0500 Subject: [PATCH] Fix safeEntityName createOrMergeEntity was replacing capital letters with -. For example "@internal/pluginApi" would result in "internal-plugin-pi". Updated to correctly handle capital letters, prefixing them with - unless they are the first character. For example "@internal/pluginApi" would result in "internal-plugin-api". Signed-off-by: Brian Hudson m> --- .changeset/calm-onions-exercise.md | 5 ++++ .../generate-catalog-info.ts | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 .changeset/calm-onions-exercise.md diff --git a/.changeset/calm-onions-exercise.md b/.changeset/calm-onions-exercise.md new file mode 100644 index 0000000000..f1c58a173f --- /dev/null +++ b/.changeset/calm-onions-exercise.md @@ -0,0 +1,5 @@ +--- +'@backstage/repo-tools': patch +--- + +Resolved an issue with generate-catalog-info where it was replacing upper case characters with -. diff --git a/packages/repo-tools/src/commands/generate-catalog-info/generate-catalog-info.ts b/packages/repo-tools/src/commands/generate-catalog-info/generate-catalog-info.ts index b8a473c67b..c15f478bd0 100644 --- a/packages/repo-tools/src/commands/generate-catalog-info/generate-catalog-info.ts +++ b/packages/repo-tools/src/commands/generate-catalog-info/generate-catalog-info.ts @@ -165,8 +165,17 @@ async function fixCatalogInfoYaml(options: FixOptions) { relativePath('.', yamlPath), ); const safeName = packageJson.name - .replace(/[^a-z0-9_\-\.]+/g, '-') - .replace(/^[^a-z0-9]|[^a-z0-9]$/g, ''); + .replace(/^[^\w\s]|[^a-z0-9]$/g, '') + .replace(/[^A-Za-z0-9_\-.]+/g, '-') + .replace(/([A-Z])/g, (_, letter, index, original) => { + if (index !== 0) { + const previousChar = original[index - 1]; + if (previousChar !== '-') { + return `-${letter.toLowerCase()}`; + } + } + return letter.toLowerCase(); + }); let yamlJson: BackstagePackageEntity; try { @@ -241,8 +250,17 @@ function createOrMergeEntity( existingEntity: BackstagePackageEntity | Record = {}, ): BackstagePackageEntity { const safeEntityName = packageJson.name - .replace(/[^a-z0-9_\-\.]+/g, '-') - .replace(/^[^a-z0-9]|[^a-z0-9]$/g, ''); + .replace(/^[^\w\s]|[^a-z0-9]$/g, '') + .replace(/[^A-Za-z0-9_\-.]+/g, '-') + .replace(/([A-Z])/g, (_, letter, index, original) => { + if (index !== 0) { + const previousChar = original[index - 1]; + if (previousChar !== '-') { + return `-${letter.toLowerCase()}`; + } + } + return letter.toLowerCase(); + }); return { ...existingEntity,