From 3e792c66c90f913f6f7ba8233f62d05c86b5f999 Mon Sep 17 00:00:00 2001 From: Robert Field Date: Mon, 30 Oct 2023 19:25:30 +0100 Subject: [PATCH] fix: .composablerc should now be found correctly after generate --- .../src/commands/generate/d2c/d2c-command.tsx | 40 ++++++++++++++++++- .../src/lib/config-middleware.ts | 12 ++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx b/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx index 3154450d..56a25974 100644 --- a/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx +++ b/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx @@ -52,6 +52,9 @@ import { isAlreadyExistsError, } from "../../payments/ep-payments/ep-payments-command" import { paramCase } from "change-case" +import { retrieveComposableRcFile } from "../../../lib/config-middleware" +import findUp from "find-up" +import path from "path" export function createD2CCommand( ctx: CommandContext, @@ -490,6 +493,11 @@ export function createD2CCommandHandler( } else { let notes: { title: string; description: string }[] = [] + const updatedCtx = await getUpdatedCtx( + ctx, + gatheredOptions.name ?? "unknown-project-name", + ) + if (gatheredOptions.plpType === "Algolia") { logger.info( boxen( @@ -512,7 +520,9 @@ export function createD2CCommandHandler( ]) if (configureAlgolia) { - const result = await createAlgoliaIntegrationCommandHandler(ctx)({ + const result = await createAlgoliaIntegrationCommandHandler( + updatedCtx, + )({ algoliaApplicationId: gatheredOptions.algoliaApplicationId, algoliaAdminApiKey: gatheredOptions.algoliaAdminApiKey, ...args, @@ -549,7 +559,7 @@ export function createD2CCommandHandler( ]) if (configureEpPayments) { - const result = await createEPPaymentsCommandHandler(ctx)({ + const result = await createEPPaymentsCommandHandler(updatedCtx)({ accountId: gatheredOptions.epPaymentsStripeAccountId, publishableKey: gatheredOptions.epPaymentsStripePublishableKey, ...args, @@ -884,3 +894,29 @@ function _createPromptProvider(): schema.PromptProvider { return inquirer.prompt(questions) } } + +export async function getUpdatedCtx(ctx: CommandContext, projectName: string) { + const configPath = await findUp([`${projectName}/.composablerc`]) + + if (!configPath) { + ctx.logger.debug("No .composablerc file found") + return ctx + } + + const parsedConfig = await retrieveComposableRcFile(configPath) + + if (!parsedConfig.success) { + ctx.logger.warn( + `Failed to parse .composablerc ${parsedConfig.error.message}`, + ) + return ctx + } + + ctx.logger.debug(`Successfully read config ${path.basename(configPath)}`) + + return { + ...ctx, + composableRc: parsedConfig.data, + workspaceRoot: path.dirname(configPath), + } +} diff --git a/packages/composable-cli/src/lib/config-middleware.ts b/packages/composable-cli/src/lib/config-middleware.ts index aea0f0cf..88f51e56 100644 --- a/packages/composable-cli/src/lib/config-middleware.ts +++ b/packages/composable-cli/src/lib/config-middleware.ts @@ -21,10 +21,7 @@ export function createConfigMiddleware( return } - const config = JSON.parse( - await promises.readFile(configPath, "utf8"), - ) as unknown - const parsedConfig = composableRcSchema.safeParse(config) + const parsedConfig = await retrieveComposableRcFile(configPath) if (!parsedConfig.success) { ctx.logger.warn( @@ -45,3 +42,10 @@ export function createConfigMiddleware( } } } + +export async function retrieveComposableRcFile(configPath: string) { + const config = JSON.parse( + await promises.readFile(configPath, "utf8"), + ) as unknown + return composableRcSchema.safeParse(config) +}