Skip to content

Commit

Permalink
fix: cli index name (#121)
Browse files Browse the repository at this point in the history
* fix: algolia index name resolving

* chore: changeset
  • Loading branch information
field123 authored Nov 14, 2023
1 parent 34df0dc commit d5dcda0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-chicken-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"composable-cli": patch
---

algolia index name resolving fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
})
})
Original file line number Diff line number Diff line change
@@ -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, "_")
}

0 comments on commit d5dcda0

Please sign in to comment.