diff --git a/.changeset/rotten-chicken-jog.md b/.changeset/rotten-chicken-jog.md new file mode 100644 index 00000000..2e6e5264 --- /dev/null +++ b/.changeset/rotten-chicken-jog.md @@ -0,0 +1,5 @@ +--- +"composable-cli": patch +--- + +algolia index name resolving fixed diff --git a/packages/composable-cli/src/commands/integration/algolia/algolia-integration-command.tsx b/packages/composable-cli/src/commands/integration/algolia/algolia-integration-command.tsx index 61b5b48e..397d724a 100644 --- a/packages/composable-cli/src/commands/integration/algolia/algolia-integration-command.tsx +++ b/packages/composable-cli/src/commands/integration/algolia/algolia-integration-command.tsx @@ -50,6 +50,7 @@ import { } from "./utility/algolia/algolia" import { logging } from "@angular-devkit/core" import { attemptToAddEnvVariables } from "../../../lib/devkit/add-env-variables" +import { resolveIndexName } from "./utility/resolve-index-name" export function createAlgoliaIntegrationCommand( ctx: CommandContext, @@ -291,9 +292,10 @@ export function createAlgoliaIntegrationCommandHandler( spinner.succeed(`Published ${catalog.attributes.name} catalog!`) - const algoliaIndexName = `${catalog.attributes.name.replace(" ", "_")}_${ - catalog.id.split("-")[0] - }` + const algoliaIndexName = resolveIndexName( + catalog.attributes.name, + catalog.id, + ) const envVarResult = await attemptToAddEnvVariables(ctx, spinner, { NEXT_PUBLIC_ALGOLIA_INDEX_NAME: algoliaIndexName, diff --git a/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.test.ts b/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.test.ts new file mode 100644 index 00000000..3a257a70 --- /dev/null +++ b/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.test.ts @@ -0,0 +1,21 @@ +import { resolveIndexName } from "./resolve-index-name" + +describe("resolveIndexName", () => { + it("should resolve index name with escaped catalog name and first block of catalogId", () => { + const catalogName = "Flora standard catalog" + const catalogId = "7b479b7c-05b7-4a25-ae3a-dae4787b0811" + + const result = resolveIndexName(catalogName, catalogId) + + expect(result).toBe("Flora_standard_catalog_7b479b7c") + }) + + it("should handle catalog names with leading/trailing spaces", () => { + const catalogName = " Flora catalog with spaces " + const catalogId = "7b479b7c-05b7-4a25-ae3a-dae4787b0811" + + const result = resolveIndexName(catalogName, catalogId) + + expect(result).toBe("Flora_catalog_with_spaces_7b479b7c") + }) +}) diff --git a/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.ts b/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.ts new file mode 100644 index 00000000..e1f85d9b --- /dev/null +++ b/packages/composable-cli/src/commands/integration/algolia/utility/resolve-index-name.ts @@ -0,0 +1,14 @@ +export function resolveIndexName( + catalogName: string, + catalogId: string, +): string { + const escapedName = escapeCatalogName(catalogName) + const resolvedId = catalogId.split("-")[0] + + return `${escapedName}_${resolvedId}` +} + +function escapeCatalogName(catalogName: string): string { + // Replace leading and trailing spaces and then replace spaces with underscores + return catalogName.trim().replace(/\s/g, "_") +}