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 a0f9dd44..c7ef6362 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 { doesIndexExist, } from "./utility/algolia/algolia" import { logging } from "@angular-devkit/core" +import { attemptToAddEnvVariables } from "../../../lib/devkit/add-env-variables" export function createAlgoliaIntegrationCommand( ctx: CommandContext, @@ -295,6 +296,17 @@ export function createAlgoliaIntegrationCommandHandler( catalog.id.split("-")[0] }` + const envVarResult = await attemptToAddEnvVariables(ctx, spinner, { + NEXT_PUBLIC_ALGOLIA_INDEX_NAME: algoliaIndexName, + }) + + if (!envVarResult.success) { + return { + success: false, + error: envVarResult.error, + } + } + logger.info( boxen( `Published catalog should have an Algolia index of ${colors.bold.green( @@ -307,13 +319,6 @@ export function createAlgoliaIntegrationCommandHandler( ), ) - // TODO: tell the user the name of the published indexes so they can add them to their .env.local file - // - need to wait for the users integration publish job to be finished. - // - indexes are made up of the _ e.g. Default_11ce355f - // - example with space in name "Default Catalog" -> Default_Catalog_11ce355f - // - check if the user has an .env.local file in the directory they have executed the command from - // - better yet prompt the user to ask if they want that done for them. - spinner.start(`Checking Algolia index exists...`) while (true) { const indexCheckResult = await doesIndexExist({ @@ -328,17 +333,6 @@ export function createAlgoliaIntegrationCommandHandler( await timer(3000) } - // if (!indexCheckResult) { - // spinner.fail(`Failed to check Algolia index`) - // return { - // success: false, - // error: { - // code: "FAILED_TO_CHECK_ALGOLIA_INDEX", - // message: "Failed to check Algolia index", - // } - // } - // } - spinner.text = `Found index ${algoliaIndexName} performing additional setup...` const additionalAlgoliaSetupResult = await additionalAlgoliaSetup({ diff --git a/packages/composable-cli/src/commands/payments/ep-payments/ep-payments-command.tsx b/packages/composable-cli/src/commands/payments/ep-payments/ep-payments-command.tsx index 0df23b43..192c4a21 100644 --- a/packages/composable-cli/src/commands/payments/ep-payments/ep-payments-command.tsx +++ b/packages/composable-cli/src/commands/payments/ep-payments/ep-payments-command.tsx @@ -25,15 +25,8 @@ import { EPPaymentsSetup, epPaymentsSetupSchema, } from "./util/setup-ep-payments-schema" -import { - callRule, - HostTree, - SchematicContext, -} from "@angular-devkit/schematics" import { processUnknownError } from "../../../util/process-unknown-error" -import { Result } from "../../../types/results" -import { addEnvVariables } from "../../../lib/devkit/add-env-variables" -import { commitTree, createScopedHost } from "../../../lib/devkit/tree-util" +import { attemptToAddEnvVariables } from "../../../lib/devkit/add-env-variables" import { checkGateway } from "@elasticpath/composable-common" export function createEPPaymentsCommand( @@ -160,8 +153,8 @@ export function createEPPaymentsCommandHandler( } await attemptToAddEnvVariables(ctx, spinner, { - accountId: options.accountId, - publishableKey: options.publishableKey, + NEXT_PUBLIC_STRIPE_ACCOUNT_ID: options.accountId, + NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: options.publishableKey, }) spinner.succeed(`EP Payments setup successfully.`) @@ -187,83 +180,6 @@ export function createEPPaymentsCommandHandler( } } -async function attemptToAddEnvVariables( - ctx: CommandContext, - spinner: ora.Ora, - { accountId, publishableKey }: EpPaymentEnvVariableRecord, -): Promise> { - const { workspaceRoot, composableRc } = ctx - - if (!composableRc) { - return { - success: false, - error: { - code: "NO_COMPOSABLE_RC", - message: "Could not detect workspace root - missing composable.rc file", - }, - } - } - - spinner.start( - `Adding EP Payments environment variables to .env.local file...`, - ) - - if (!workspaceRoot) { - spinner.fail( - `Failed to add environment variables to .env.local file - missing workspace root`, - ) - return { - success: false, - error: { - code: "EP", - message: - "Setup of EP Payment gateway succeeded but failed to add env variables to .env.local file", - }, - } - } - - await addEpPaymentEnvVariables(workspaceRoot, { - accountId, - publishableKey, - }) - - spinner.succeed(`Added EP Payments environment variables to .env.local file.`) - - return { - success: true, - data: {}, - } -} - -type EpPaymentEnvVariableRecord = { accountId: string; publishableKey: string } - -async function addEpPaymentEnvVariables( - workspaceRoot: string, - { accountId, publishableKey }: EpPaymentEnvVariableRecord, -): Promise { - const host = createScopedHost(workspaceRoot) - - const initialTree = new HostTree(host) - - if (!initialTree.exists(".env.local")) { - initialTree.create(".env.local", "") - } - - const context = {} as unknown as SchematicContext - - const rule = addEnvVariables( - { - NEXT_PUBLIC_STRIPE_ACCOUNT_ID: accountId, - NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: publishableKey, - }, - ".env.local", - ) - - const tree = await callRule(rule, initialTree, context).toPromise() - - await commitTree(host, tree) -} - async function resolveOptions( args: EPPaymentsCommandArguments, logger: logging.Logger, diff --git a/packages/composable-cli/src/lib/devkit/add-env-variables.ts b/packages/composable-cli/src/lib/devkit/add-env-variables.ts index bbc05884..7dff0620 100644 --- a/packages/composable-cli/src/lib/devkit/add-env-variables.ts +++ b/packages/composable-cli/src/lib/devkit/add-env-variables.ts @@ -1,4 +1,36 @@ -import { Rule, Tree } from "@angular-devkit/schematics" +import { + callRule, + HostTree, + Rule, + SchematicContext, + Tree, +} from "@angular-devkit/schematics" +import { commitTree, createScopedHost } from "./tree-util" +import { CommandContext } from "../../types/command" +import ora from "ora" +import { Result } from "../../types/results" + +export async function addToEnvFile( + workspaceRoot: string, + filepath: string, + variables: Record, +): Promise { + const host = createScopedHost(workspaceRoot) + + const initialTree = new HostTree(host) + + if (!initialTree.exists(filepath)) { + initialTree.create(filepath, "") + } + + const context = {} as unknown as SchematicContext + + const rule = addEnvVariables(variables, filepath) + + const tree = await callRule(rule, initialTree, context).toPromise() + + await commitTree(host, tree) +} export function addEnvVariables( envVars: Record, @@ -41,3 +73,44 @@ export function parseEnv(src: string): EnvData { } return result } + +export async function attemptToAddEnvVariables( + ctx: CommandContext, + spinner: ora.Ora, + variables: Record, +): Promise> { + const { workspaceRoot, composableRc } = ctx + + if (!composableRc) { + return { + success: false, + error: { + code: "NO_COMPOSABLE_RC", + message: "Could not detect workspace root - missing composable.rc file", + }, + } + } + + spinner.start(`Adding environment variables to .env.local file...`) + + if (!workspaceRoot) { + spinner.fail( + `Failed to add environment variables to .env.local file - missing workspace root`, + ) + return { + success: false, + error: { + code: "FAILED_TO_ADD_ENV_VARS", + message: "Failed to add env variables to .env.local file", + }, + } + } + await addToEnvFile(workspaceRoot, ".env.local", variables) + + spinner.succeed(`Added environment variables to .env.local file.`) + + return { + success: true, + data: {}, + } +}