Skip to content

Commit

Permalink
(fix): fern generate --preview doesnt check for env variables (#3988)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jul 4, 2024
1 parent 1e4c6c6 commit 158277c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ async function createJob({
whitelabel: FernFiddle.WhitelabelConfig | undefined;
absolutePathToPreview: AbsoluteFilePath | undefined;
}): Promise<FernFiddle.remoteGen.CreateJobResponse> {
const isPreview = absolutePathToPreview != null;
const generatorConfig: FernFiddle.GeneratorConfigV2 = {
id: generatorInvocation.name,
version: generatorInvocation.version,
outputMode: generatorInvocation.outputMode,
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 });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ import { isPlainObject, mapValues } from "lodash-es";

const ENV_VAR_REGEX = /\$\{(\w+)\}/g;

export function substituteEnvVariables<T>(content: T, context: TaskContext): T {
export declare namespace SubstituteEnvVariables {
interface Opts {
/* Substitues all environment variables as empty strings. */
substituteAsEmpty?: boolean;
}
}

export function substituteEnvVariables<T>(
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.`);
Expand All @@ -20,7 +34,7 @@ export function substituteEnvVariables<T>(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;
}

0 comments on commit 158277c

Please sign in to comment.