-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from jfrconley/joco/openapi-spec-gen
OpenAPI 3 spec generation
- Loading branch information
Showing
47 changed files
with
3,946 additions
and
764 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nornir/rest": major | ||
--- | ||
|
||
OpenAPI 3 spec generation |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { merge } from "lodash"; | ||
import path from "path"; | ||
import yargs from "yargs"; | ||
import { isErrorResult } from "../transform/openapi-merge"; | ||
import { getMergedSpec } from "./lib/collect"; | ||
import { resolveTsConfigOutdir } from "./lib/ts-utils"; | ||
|
||
yargs | ||
.scriptName("nornir-oas") | ||
.command("collect", "Build OpenAPI spec from collected spec files", args => | ||
args | ||
.option("output", { | ||
default: path.join(process.cwd(), "openapi.json"), | ||
alias: "o", | ||
type: "string", | ||
description: "Output file for the generated OpenAPI spec", | ||
}) | ||
.option("scanDirectory", { | ||
default: resolveTsConfigOutdir() ?? path.join(process.cwd(), "dist"), | ||
type: "string", | ||
alias: "s", | ||
description: | ||
"Directory to scan for spec files. This is probably the directory where your compiled typescript files are.", | ||
}) | ||
.option("overrideSpec", { | ||
type: "string", | ||
alias: "b", | ||
description: "Path to an openapi JSON file to deeply merge with collected specification", | ||
}), args => { | ||
const mergedSpec = getMergedSpec(args.scanDirectory); | ||
if (isErrorResult(mergedSpec)) { | ||
throw new Error("Failed to merge spec files"); | ||
} | ||
let spec = mergedSpec.output; | ||
if (args.overrideSpec) { | ||
const overrideSpec = JSON.parse(readFileSync(args.overrideSpec, "utf-8")); | ||
spec = merge(spec, overrideSpec); | ||
} | ||
|
||
writeFileSync(args.output, JSON.stringify(spec, null, 2)); | ||
}).strictCommands().demandCommand().parse(); |
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,30 @@ | ||
import { Swagger } from "atlassian-openapi"; | ||
import { readFileSync } from "fs"; | ||
import { sync } from "glob"; | ||
import { OpenAPIV3_1 } from "openapi-types"; | ||
import { merge } from "../../transform/openapi-merge/index.js"; | ||
import SwaggerV3 = Swagger.SwaggerV3; | ||
|
||
export function getSpecFiles(scanDir: string) { | ||
return sync(`${scanDir}/**/*.nornir.oas.json`); | ||
} | ||
|
||
export function readSpecFiles(paths: string[]) { | ||
return paths.map(path => { | ||
return JSON.parse(readFileSync(path, "utf-8")) as OpenAPIV3_1.Document; | ||
}); | ||
} | ||
|
||
export function getMergedSpec(scanDir: string) { | ||
const files = getSpecFiles(scanDir); | ||
const specs = readSpecFiles(files); | ||
return merge(specs.map(spec => ({ | ||
dispute: { | ||
// mergeDispute: true, | ||
// alwaysApply: true, | ||
// prefix: "", | ||
// suffix: "" | ||
}, | ||
oas: spec as SwaggerV3, | ||
}))); | ||
} |
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,24 @@ | ||
import { dirname, resolve } from "node:path"; | ||
import ts from "typescript"; | ||
|
||
export function resolveTsConfigOutdir(searchPath = process.cwd(), configName = "tsconfig.json") { | ||
const path = ts.findConfigFile(searchPath, ts.sys.fileExists, configName); | ||
if (path == undefined) { | ||
return; | ||
} | ||
const config = ts.readConfigFile(path, ts.sys.readFile); | ||
if (config.error) { | ||
return; | ||
} | ||
const compilerOptions = config.config as { compilerOptions: ts.CompilerOptions }; | ||
|
||
const pathDir = dirname(path); | ||
|
||
const outDir = compilerOptions.compilerOptions.outDir; | ||
|
||
if (outDir == undefined) { | ||
return undefined; | ||
} | ||
|
||
return resolve(pathDir, outDir); | ||
} |
Oops, something went wrong.