Skip to content

Commit

Permalink
fix: .composablerc should now be found correctly after generate
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Oct 30, 2023
1 parent f729d31 commit 3e792c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
40 changes: 38 additions & 2 deletions packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
}
}
12 changes: 8 additions & 4 deletions packages/composable-cli/src/lib/config-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}

0 comments on commit 3e792c6

Please sign in to comment.