-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add new openapi-fdr command for direct openapi - fdr parsi…
…ng (#5292) * added new openapi-fdr command * update import paths * address comments * remove erroneous async * remove fdr dep and install inlined parser package --------- Co-authored-by: fern-bot <info@buildwithfern.com>
- Loading branch information
1 parent
04d1b5c
commit a5e0009
Showing
4 changed files
with
173 additions
and
10 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
81 changes: 81 additions & 0 deletions
81
...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,81 @@ | ||
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"; | ||
// TODO: clean up imports | ||
import { OpenApiDocumentConverterNode } from "@fern-api/docs-parsers"; | ||
import { ErrorCollector } from "@fern-api/docs-parsers"; | ||
import fs from "fs"; | ||
import yaml from "js-yaml"; | ||
import { BaseOpenApiV3_1ConverterNodeContext } from "@fern-api/docs-parsers"; | ||
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 }); | ||
|
||
if (openApiSpecs.length === 0) { | ||
context.logger.error("No OpenAPI specs found in the workspace"); | ||
return; | ||
} | ||
|
||
if (openApiSpecs.length > 1) { | ||
context.logger.error("Found multiple OpenAPI specs in the workspace."); | ||
return; | ||
} | ||
|
||
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 = 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.