From 158277c5f47b9fa862375a4b4946af35fa56e995 Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Thu, 4 Jul 2024 17:38:52 -0400 Subject: [PATCH] (fix): fern generate --preview doesnt check for env variables (#3988) --- .../src/createAndStartJob.ts | 7 +++++-- .../src/substituteEnvVariables.ts | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts index b3ca7257fe7..63cf1331c64 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/createAndStartJob.ts @@ -93,6 +93,7 @@ async function createJob({ whitelabel: FernFiddle.WhitelabelConfig | undefined; absolutePathToPreview: AbsoluteFilePath | undefined; }): Promise { + const isPreview = absolutePathToPreview != null; const generatorConfig: FernFiddle.GeneratorConfigV2 = { id: generatorInvocation.name, version: generatorInvocation.version, @@ -100,9 +101,11 @@ async function createJob({ customConfig: generatorInvocation.config, publishMetadata: generatorInvocation.publishMetadata }; - const generatorConfigsWithEnvVarSubstitutions = substituteEnvVariables(generatorConfig, context); + const generatorConfigsWithEnvVarSubstitutions = substituteEnvVariables(generatorConfig, context, { + substituteAsEmpty: isPreview + }); const whitelabelWithEnvVarSubstiutions = - whitelabel != null ? substituteEnvVariables(whitelabel, context) : undefined; + whitelabel != null ? substituteEnvVariables(whitelabel, context, { substituteAsEmpty: isPreview }) : undefined; const remoteGenerationService = createFiddleService({ token: token.value }); diff --git a/packages/cli/generation/remote-generation/remote-workspace-runner/src/substituteEnvVariables.ts b/packages/cli/generation/remote-generation/remote-workspace-runner/src/substituteEnvVariables.ts index 4b18e92a5dd..9071f0cd846 100644 --- a/packages/cli/generation/remote-generation/remote-workspace-runner/src/substituteEnvVariables.ts +++ b/packages/cli/generation/remote-generation/remote-workspace-runner/src/substituteEnvVariables.ts @@ -3,9 +3,23 @@ import { isPlainObject, mapValues } from "lodash-es"; const ENV_VAR_REGEX = /\$\{(\w+)\}/g; -export function substituteEnvVariables(content: T, context: TaskContext): T { +export declare namespace SubstituteEnvVariables { + interface Opts { + /* Substitues all environment variables as empty strings. */ + substituteAsEmpty?: boolean; + } +} + +export function substituteEnvVariables( + content: T, + context: TaskContext, + options: SubstituteEnvVariables.Opts = {} +): T { if (typeof content === "string") { const transformed = (content as string).replace(ENV_VAR_REGEX, (_substring, envVarName) => { + if (options.substituteAsEmpty) { + return ""; + } const envVarValue = process.env[envVarName]; if (envVarValue == null) { context.failAndThrow(`Environment variable ${envVarName} is not defined.`); @@ -20,7 +34,7 @@ export function substituteEnvVariables(content: T, context: TaskContext): T { } const transformed = mapValues(content as unknown as object, (value: unknown) => - substituteEnvVariables(value, context) + substituteEnvVariables(value, context, options) ); return transformed as unknown as T; }