From a245fad6c75c784ae644d07c59fbc36b685337c6 Mon Sep 17 00:00:00 2001 From: Brian Hudson Date: Tue, 30 Jan 2024 07:34:38 -0500 Subject: [PATCH] Add util function and related tests Signed-off-by: Brian Hudson --- .../generate-catalog-info.ts | 29 +++------------ .../generate-catalog-info/utils.test.ts | 36 +++++++++++++++++++ .../commands/generate-catalog-info/utils.ts | 17 +++++++++ 3 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 packages/repo-tools/src/commands/generate-catalog-info/utils.test.ts 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 c15f478bd0..1893bd25c8 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 @@ -33,6 +33,7 @@ import { isFulfilled, readFile, writeFile, + safeEntityName, } from './utils'; import { CodeOwnersEntry } from 'codeowners-utils'; @@ -164,18 +165,7 @@ async function fixCatalogInfoYaml(options: FixOptions) { codeowners, relativePath('.', yamlPath), ); - const safeName = packageJson.name - .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(); - }); + const safeName = safeEntityName(packageJson.name); let yamlJson: BackstagePackageEntity; try { @@ -249,18 +239,7 @@ function createOrMergeEntity( owner: string, existingEntity: BackstagePackageEntity | Record = {}, ): BackstagePackageEntity { - const safeEntityName = packageJson.name - .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(); - }); + const entityName = safeEntityName(packageJson.name); return { ...existingEntity, @@ -269,7 +248,7 @@ function createOrMergeEntity( metadata: { ...existingEntity.metadata, // Provide default name/title/description values. - name: safeEntityName, + name: entityName, title: packageJson.name, ...(packageJson.description && !existingEntity.metadata?.description ? { description: packageJson.description } diff --git a/packages/repo-tools/src/commands/generate-catalog-info/utils.test.ts b/packages/repo-tools/src/commands/generate-catalog-info/utils.test.ts new file mode 100644 index 0000000000..40c1bb1e19 --- /dev/null +++ b/packages/repo-tools/src/commands/generate-catalog-info/utils.test.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { safeEntityName } from './utils'; + +describe('utils', () => { + describe('safeEntityName', () => { + it('should remove non-alphanumeric characters at the start and end', () => { + const result = safeEntityName('%entityname$'); + expect(result).toBe('entityname'); + }); + + it('should replace non-alphanumeric characters, except - and _, with -', () => { + const result = safeEntityName('entity@#name$'); + expect(result).toBe('entity-name'); + }); + + it('should replace capital letters with - followed by the same letter in lowercase', () => { + const result = safeEntityName('EntityName'); + expect(result).toBe('entity-name'); + }); + }); +}); diff --git a/packages/repo-tools/src/commands/generate-catalog-info/utils.ts b/packages/repo-tools/src/commands/generate-catalog-info/utils.ts index b00fbd57a8..3f23b35112 100644 --- a/packages/repo-tools/src/commands/generate-catalog-info/utils.ts +++ b/packages/repo-tools/src/commands/generate-catalog-info/utils.ts @@ -48,3 +48,20 @@ export const isRejected = ( export const isFulfilled = ( input: PromiseSettledResult, ): input is PromiseFulfilledResult => input.status === 'fulfilled'; + +/** + * Generates a suitable entity name from a package name by slugifying the given package name. + * + * @param packageName - The package name to generate an entity name from. + * @returns The generated entity name, a slugified version of the package name. + */ +export const safeEntityName = (packageName: string): string => { + return packageName + .replace(/^[^\w\s]|[^a-z0-9]$/g, '') + .replace(/[^A-Za-z0-9_\-.]+/g, '-') + .replace( + /([a-z])([A-Z])/g, + (_, a, b) => `${a}-${b.toLocaleLowerCase('en-US')}`, + ) + .replace(/^(.)/, (_, a) => a.toLocaleLowerCase('en-US')); +};