-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...i/cli/src/commands/generate-openapi-fdr/generateOpenApiToFdrApiDefinitionForWorkspaces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { AbsoluteFilePath, stringifyLargeObject } from "@fern-api/fs-utils"; | ||
import { Project } from "@fern-api/project-loader"; | ||
import { writeFile } from "fs/promises"; | ||
import path from "path"; | ||
import { CliContext } from "../../cli-context/CliContext"; | ||
import { getAllOpenAPISpecs, LazyFernWorkspace, OSSWorkspace } from "@fern-api/lazy-fern-workspace"; | ||
import { OpenApiDocumentConverterNode } from "@fern-api/docs-parsers/dist/openapi/3.1/OpenApiDocumentConverter.node"; | ||
import { ErrorCollector } from "@fern-api/docs-parsers/dist/ErrorCollector"; | ||
import fs from "fs"; | ||
import yaml from "js-yaml"; | ||
import { BaseOpenApiV3_1ConverterNodeContext } from "@fern-api/docs-parsers/dist/openapi/BaseOpenApiV3_1Converter.node"; | ||
import { OpenAPIV3_1 } from "openapi-types"; | ||
|
||
export async function generateOpenApiToFdrApiDefinitionForWorkspaces({ | ||
project, | ||
outputFilepath, | ||
cliContext | ||
}: { | ||
project: Project; | ||
outputFilepath: AbsoluteFilePath; | ||
cliContext: CliContext; | ||
}): Promise<void> { | ||
await Promise.all( | ||
project.apiWorkspaces.map(async (workspace) => { | ||
await cliContext.runTaskForWorkspace(workspace, async (context) => { | ||
await cliContext.runTaskForWorkspace(workspace, async (context) => { | ||
if (workspace instanceof LazyFernWorkspace) { | ||
context.logger.info("Skipping, API is specified as a Fern Definition."); | ||
return; | ||
} else if (!(workspace instanceof OSSWorkspace)) { | ||
return; | ||
} | ||
const openAPISpecs = await getAllOpenAPISpecs({ context, specs: workspace.specs }); | ||
|
||
const openApi = openAPISpecs[0]; | ||
|
||
if (openApi != null) { | ||
const input = yaml.load( | ||
fs.readFileSync(openApi.absoluteFilepath, "utf8") | ||
) as OpenAPIV3_1.Document; | ||
|
||
const oasContext: BaseOpenApiV3_1ConverterNodeContext = { | ||
document: input, | ||
logger: context.logger, | ||
errors: new ErrorCollector() | ||
}; | ||
|
||
const openApiFdrJson = new OpenApiDocumentConverterNode({ | ||
input, | ||
context: oasContext, | ||
accessPath: [], | ||
pathId: workspace.workspaceName ?? "openapi parser" | ||
}); | ||
|
||
const fdrApiDefinition = await openApiFdrJson.convert(); | ||
|
||
const resolvedOutputFilePath = path.resolve(outputFilepath); | ||
await writeFile( | ||
resolvedOutputFilePath, | ||
await stringifyLargeObject(fdrApiDefinition, { pretty: true }) | ||
); | ||
context.logger.info(`Wrote FDR API definition to ${resolvedOutputFilePath}`); | ||
} else { | ||
context.logger.error("No OpenAPI spec found in the workspace"); | ||
} | ||
}); | ||
}); | ||
}) | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.