-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
cd89f97
commit 873d4c6
Showing
3 changed files
with
267 additions
and
20 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
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 |
---|---|---|
@@ -1,24 +1,34 @@ | ||
import { program } from "commander"; | ||
import { version } from "../package.json" assert { type: "json" }; | ||
import { simplifySchemaString } from "./index"; | ||
import fs from "fs"; | ||
import { version } from "../package.json" assert { type: "json" }; | ||
import { availableOptimizations, simplifySchemaString, type OptimizationKey } from "./index"; | ||
|
||
program | ||
.name("openapi-simplifier") | ||
.description("CLI to simplify OpenAPI schemas to ease code generation") | ||
.version(version) | ||
.argument("<string>", "input file") | ||
.option("-o, --output <string>", "output file"); | ||
.option("-o, --output <string>", "output file") | ||
.option( | ||
"-i, --include [string...]", | ||
`apply only certain optimizations. available optimizations: ${Object.keys(availableOptimizations).join(", ")}` | ||
); | ||
|
||
program.parse(); | ||
|
||
const [inputFile] = program.args; | ||
const { output } = program.opts(); | ||
const { output, include }: { output?: string; include?: string[] } = program.opts(); | ||
|
||
const wrongOpt = include?.find((o) => !(o in availableOptimizations)); | ||
if (wrongOpt) { | ||
program.error(`Unexpected optimization: "${wrongOpt}" is not defined.`, { exitCode: 2 }); | ||
} | ||
|
||
const outputWriter = output | ||
? (content: string) => fs.writeFileSync(output, content) | ||
: (content: string) => process.stdout.write(content); | ||
|
||
const content = fs.readFileSync(inputFile === "-" ? 0 : inputFile).toString(); | ||
const simplfiedContent = simplifySchemaString(content); | ||
const simplfiedContent = simplifySchemaString(content, include as OptimizationKey[] | undefined); | ||
|
||
outputWriter(simplfiedContent); |