Skip to content

Commit

Permalink
fix(cli): load both generators.yml or generators.yaml (#5483)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Dec 26, 2024
1 parent 406561a commit 45fbf67
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
addGenerator,
generatorsYml,
getPathToGeneratorsConfiguration,
loadRawGeneratorsConfiguration
} from "@fern-api/configuration-loader";
Expand Down Expand Up @@ -38,13 +37,17 @@ export async function addGeneratorToWorkspaces({
cliVersion: cliContext.environment.packageVersion
});

await writeFile(
const absolutePathToGeneratorsConfiguration =
workspace.generatorsConfiguration?.absolutePathToConfiguration ??
getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
}),
yaml.dump(newConfiguration)
);
(await getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
}));

if (absolutePathToGeneratorsConfiguration == null) {
return;
}

await writeFile(absolutePathToGeneratorsConfiguration, yaml.dump(newConfiguration));
context.logger.info(chalk.green(`Added ${generatorName} generator`));
});
})
Expand Down
21 changes: 9 additions & 12 deletions packages/cli/cli/src/commands/upgrade/upgradeGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
generatorsYml,
getGeneratorNameOrThrow,
getLatestGeneratorVersion,
getPathToGeneratorsConfiguration,
Expand All @@ -8,12 +7,12 @@ import {
import { AbsoluteFilePath, doesPathExist } from "@fern-api/fs-utils";
import { Project } from "@fern-api/project-loader";
import { TaskContext } from "@fern-api/task-context";
import { FernRegistry } from "@fern-fern/generators-sdk";
import chalk from "chalk";
import { readFile, writeFile } from "fs/promises";
import path from "path";
import YAML from "yaml";
import { CliContext } from "../../cli-context/CliContext";
import { FernRegistry } from "@fern-fern/generators-sdk";

export async function loadAndUpdateGenerators({
absolutePathToWorkspace,
Expand All @@ -32,8 +31,8 @@ export async function loadAndUpdateGenerators({
channel: FernRegistry.generators.ReleaseType | undefined;
cliVersion: string;
}): Promise<string | undefined> {
const filepath = getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (!(await doesPathExist(filepath))) {
const filepath = await getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (filepath == null || !(await doesPathExist(filepath))) {
context.logger.debug("Generators configuration file was not found, no generators to upgrade.");
return undefined;
}
Expand Down Expand Up @@ -170,14 +169,12 @@ export async function upgradeGenerator({
cliVersion: cliContext.environment.packageVersion
});

if (updatedConfiguration != null) {
await writeFile(
workspace.generatorsConfiguration?.absolutePathToConfiguration ??
getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
}),
updatedConfiguration
);
const absolutePathToGeneratorsConfiguration = await getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
});

if (absolutePathToGeneratorsConfiguration != null && updatedConfiguration != null) {
await writeFile(absolutePathToGeneratorsConfiguration, updatedConfiguration);
}
});
})
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
- changelogEntry:
- summary: |
The CLI now supports both `generators.yml` and `generators.yaml` file extensions for generator configuration.
type: fix
irVersion: 53
version: 0.46.13


- changelogEntry:
- summary: |
Correctly omits readOnly query parameters during openapi to fern definition generation.
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/configuration-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"lodash-es": "^4.17.21",
"semver": "^7.6.2",
"tinycolor2": "^1.6.0",
"zod": "^3.22.3"
"zod": "^3.22.3",
"tmp-promise": "^3.0.3"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand All @@ -57,4 +58,4 @@
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { createMockTaskContext } from "@fern-api/task-context";
import { promises as fs } from "fs";
import yaml from "js-yaml";
import path from "path";
import tmp from "tmp-promise";
import { beforeEach, describe, expect, it } from "vitest";
import { loadRawGeneratorsConfiguration } from "../loadGeneratorsConfiguration";

describe("loadRawGeneratorsConfiguration", () => {
const mockContext = createMockTaskContext();
const validConfig = { groups: {} };
const configYaml = yaml.dump(validConfig);
let tmpDir: string;

beforeEach(async () => {
const { path: generatedPath } = await tmp.dir({ prefix: "generators-test-", postfix: "" });
tmpDir = generatedPath;
});

it("loads .yml extension", async () => {
const ymlPath = path.join(tmpDir, "generators.yml");
await fs.writeFile(ymlPath, configYaml);

const result = await loadRawGeneratorsConfiguration({
absolutePathToWorkspace: AbsoluteFilePath.of(tmpDir),
context: mockContext
});

expect(result).toEqual(validConfig);
});

it("loads .yaml extension", async () => {
const yamlPath = path.join(tmpDir, "generators.yaml");
await fs.writeFile(yamlPath, configYaml);

const result = await loadRawGeneratorsConfiguration({
absolutePathToWorkspace: AbsoluteFilePath.of(tmpDir),
context: mockContext
});

expect(result).toEqual(validConfig);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { generatorsYml } from "@fern-api/configuration";
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { TaskContext } from "@fern-api/task-context";
import { readFile } from "fs/promises";
import yaml from "js-yaml";
import path from "path";
import { convertGeneratorsConfiguration } from "./convertGeneratorsConfiguration";
import { GENERATORS_CONFIGURATION_FILENAME, generatorsYml } from "@fern-api/configuration";

export async function loadRawGeneratorsConfiguration({
absolutePathToWorkspace,
Expand All @@ -13,10 +13,11 @@ export async function loadRawGeneratorsConfiguration({
absolutePathToWorkspace: AbsoluteFilePath;
context: TaskContext;
}): Promise<generatorsYml.GeneratorsConfigurationSchema | undefined> {
const filepath = getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (!(await doesPathExist(filepath))) {
const filepath = await getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (filepath == null) {
return undefined;
}

const contentsStr = await readFile(filepath);
try {
const contentsParsed = yaml.load(contentsStr.toString());
Expand Down Expand Up @@ -54,16 +55,29 @@ export async function loadGeneratorsConfiguration({
if (rawGeneratorsConfiguration == null) {
return undefined;
}
const filepath = await getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (filepath == null) {
return undefined;
}
return convertGeneratorsConfiguration({
absolutePathToGeneratorsConfiguration: getPathToGeneratorsConfiguration({ absolutePathToWorkspace }),
absolutePathToGeneratorsConfiguration: filepath,
rawGeneratorsConfiguration
});
}

export function getPathToGeneratorsConfiguration({
export async function getPathToGeneratorsConfiguration({
absolutePathToWorkspace
}: {
absolutePathToWorkspace: AbsoluteFilePath;
}): AbsoluteFilePath {
return join(absolutePathToWorkspace, RelativeFilePath.of(GENERATORS_CONFIGURATION_FILENAME));
}): Promise<AbsoluteFilePath | undefined> {
const ymlPath = join(absolutePathToWorkspace, RelativeFilePath.of("generators.yml"));
const yamlPath = join(absolutePathToWorkspace, RelativeFilePath.of("generators.yaml"));

if (await doesPathExist(ymlPath)) {
return ymlPath;
}
if (await doesPathExist(yamlPath)) {
return yamlPath;
}
return undefined;
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45fbf67

Please sign in to comment.