Skip to content

Commit

Permalink
fix(cli): Substitute env vars before IR generation (#4520)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Sep 3, 2024
1 parent 566c51a commit 17be1c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
9 changes: 9 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
- changelog_entry:
- summary: |
Fix an issue where some postman environment variables (e.g. API key) were not substituted
when running fern generate.
type: fix
created_at: '2024-09-03'
ir_version: 53
version: 0.41.0-rc1

- changelog_entry:
- summary: |
Every fern folder that is using OpenAPI must configure an explicit location to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ROOT_API_FILENAME
} from "@fern-api/configuration";
import { createFiddleService, getFiddleOrigin } from "@fern-api/core";
import { replaceEnvVariables } from "@fern-api/core-utils";
import { AbsoluteFilePath, dirname, join, RelativeFilePath, stringifyLargeObject } from "@fern-api/fs-utils";
import {
migrateIntermediateRepresentationForGenerator,
Expand Down Expand Up @@ -94,7 +93,8 @@ async function createJob({
whitelabel: FernFiddle.WhitelabelConfig | undefined;
absolutePathToPreview: AbsoluteFilePath | undefined;
}): Promise<FernFiddle.remoteGen.CreateJobResponse> {
const isPreview = absolutePathToPreview != null;
const remoteGenerationService = createFiddleService({ token: token.value });

const generatorConfig: FernFiddle.GeneratorConfigV2 = {
id: generatorInvocation.name,
version: generatorInvocation.version,
Expand All @@ -103,19 +103,6 @@ async function createJob({
publishMetadata: generatorInvocation.publishMetadata
};

/** Sugar to substitute templated env vars in a standard way */
const substituteEnvVars = <T>(stringOrObject: T) =>
replaceEnvVariables(
stringOrObject,
{ onError: (e) => context.failAndThrow(e) },
{ substituteAsEmpty: isPreview }
);

const generatorConfigsWithEnvVarSubstitutions = substituteEnvVars(generatorConfig);
const whitelabelWithEnvVarSubstiutions = whitelabel != null ? substituteEnvVars(whitelabel) : undefined;

const remoteGenerationService = createFiddleService({ token: token.value });

let fernDefinitionMetadata: FernFiddle.remoteGen.FernDefinitionMetadata | undefined;
// Only write definition if output mode is github
if (generatorInvocation.outputMode.type.startsWith("github")) {
Expand Down Expand Up @@ -214,14 +201,14 @@ async function createJob({
apiName: workspace.definition.rootApiFile.contents.name,
version,
organizationName: organization,
generators: [generatorConfigsWithEnvVarSubstitutions],
generators: [generatorConfig],
uploadToS3: shouldUploadToS3({
outputMode: generatorInvocation.outputMode,
generatorInvocation,
absolutePathToPreview,
shouldLogS3Url
}),
whitelabel: whitelabelWithEnvVarSubstiutions,
whitelabel,
fernDefinitionMetadata,
preview: absolutePathToPreview != null
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createAndStartJob } from "./createAndStartJob";
import { pollJobAndReportStatus } from "./pollJobAndReportStatus";
import { RemoteTaskHandler } from "./RemoteTaskHandler";
import { SourceUploader } from "./SourceUploader";
import { replaceEnvVariables } from "@fern-api/core-utils";

export async function runRemoteGenerationForGenerator({
projectConfig,
Expand Down Expand Up @@ -91,20 +92,31 @@ export async function runRemoteGenerationForGenerator({
ir.sourceConfig = sourceConfig;
}

/** Sugar to substitute templated env vars in a standard way */
const isPreview = absolutePathToPreview != null;
const substituteEnvVars = <T>(stringOrObject: T) =>
replaceEnvVariables(
stringOrObject,
{ onError: (e) => interactiveTaskContext.failAndThrow(e) },
{ substituteAsEmpty: isPreview }
);

const generatorInvocationWithEnvVarSubstitutions = substituteEnvVars(generatorInvocation);

const job = await createAndStartJob({
projectConfig,
workspace,
organization,
generatorInvocation,
generatorInvocation: generatorInvocationWithEnvVarSubstitutions,
context: interactiveTaskContext,
version,
intermediateRepresentation: {
...ir,
publishConfig: getPublishConfig({ generatorInvocation })
publishConfig: getPublishConfig({ generatorInvocation: generatorInvocationWithEnvVarSubstitutions })
},
shouldLogS3Url,
token,
whitelabel,
whitelabel: whitelabel != null ? substituteEnvVars(whitelabel) : undefined,
irVersionOverride,
absolutePathToPreview
});
Expand Down

0 comments on commit 17be1c8

Please sign in to comment.