Skip to content

Commit

Permalink
try again
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Dec 26, 2024
1 parent e44b1f8 commit 199147b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
19 changes: 10 additions & 9 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,12 +169,14 @@ export async function upgradeGenerator({
cliVersion: cliContext.environment.packageVersion
});

if (updatedConfiguration != null) {

const absolutePathToGeneratorsConfiguration = await getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
});

if (absolutePathToGeneratorsConfiguration != null && updatedConfiguration != null) {
await writeFile(
workspace.generatorsConfiguration?.absolutePathToConfiguration ??
getPathToGeneratorsConfiguration({
absolutePathToWorkspace: workspace.absoluteFilePath
}),
absolutePathToGeneratorsConfiguration,
updatedConfiguration
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@ export async function loadRawGeneratorsConfiguration({
absolutePathToWorkspace: AbsoluteFilePath;
context: TaskContext;
}): Promise<generatorsYml.GeneratorsConfigurationSchema | undefined> {
const filepathYml = getPathToGeneratorsConfiguration({ absolutePathToWorkspace, extension: "yml" });
const filepathYaml = getPathToGeneratorsConfiguration({ absolutePathToWorkspace, extension: "yaml" });

let filepath: AbsoluteFilePath;
if (await doesPathExist(filepathYml)) {
filepath = filepathYml;
} else if (await doesPathExist(filepathYaml)) {
filepath = filepathYaml;
} else {
const filepath = await getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (filepath == null) {
return undefined;
}

Expand Down Expand Up @@ -62,21 +55,29 @@ export async function loadGeneratorsConfiguration({
if (rawGeneratorsConfiguration == null) {
return undefined;
}
const filepathYml = getPathToGeneratorsConfiguration({ absolutePathToWorkspace, extension: "yml" });
const filepathYaml = getPathToGeneratorsConfiguration({ absolutePathToWorkspace, extension: "yaml" });
const filepath = (await doesPathExist(filepathYml)) ? filepathYml : filepathYaml;
const filepath = await getPathToGeneratorsConfiguration({ absolutePathToWorkspace });
if (filepath == null) {
return undefined;
}
return convertGeneratorsConfiguration({
absolutePathToGeneratorsConfiguration: filepath,
rawGeneratorsConfiguration
});
}

export function getPathToGeneratorsConfiguration({
absolutePathToWorkspace,
extension
export async function getPathToGeneratorsConfiguration({
absolutePathToWorkspace
}: {
absolutePathToWorkspace: AbsoluteFilePath;
extension: "yml" | "yaml";
}): AbsoluteFilePath {
return join(absolutePathToWorkspace, RelativeFilePath.of(`generators.${extension}`));
}): 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;
}

0 comments on commit 199147b

Please sign in to comment.