check-openapi: Add option to automatically fix errors.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-09-08 17:02:42 -07:00 committed by Tim Abbott
parent 7af2647d64
commit a08c138c03
3 changed files with 13 additions and 1 deletions

View File

@ -126,6 +126,7 @@
"webpack-dev-server": "^3.5.1", "webpack-dev-server": "^3.5.1",
"xvfb": "^0.4.0", "xvfb": "^0.4.0",
"yaml": "^2.0.0-8", "yaml": "^2.0.0-8",
"yargs": "^15.0.2",
"yarn-deduplicate": "^3.0.0", "yarn-deduplicate": "^3.0.0",
"zulip-js": "^2.0.8" "zulip-js": "^2.0.8"
}, },

View File

@ -9,6 +9,12 @@ const ExampleValidator = require("openapi-examples-validator");
const Prettier = require("prettier"); const Prettier = require("prettier");
const SwaggerParser = require("swagger-parser"); const SwaggerParser = require("swagger-parser");
const {Composer, LineCounter, Parser, Scalar, YAMLMap, YAMLSeq, visit} = require("yaml"); const {Composer, LineCounter, Parser, Scalar, YAMLMap, YAMLSeq, visit} = require("yaml");
const yargs = require("yargs");
const {argv} = yargs.option("fix", {
type: "boolean",
description: "Automatically fix some problems",
});
async function checkFile(file) { async function checkFile(file) {
const yaml = await fs.promises.readFile(file, "utf8"); const yaml = await fs.promises.readFile(file, "utf8");
@ -92,6 +98,10 @@ async function checkFile(file) {
if (!ok) { if (!ok) {
process.exitCode = 1; process.exitCode = 1;
} }
if (reformats.size > 0) {
console.log("%s: Fixing problems", file);
await fs.promises.writeFile(file, tokens.map((token) => CST.stringify(token)).join(""));
}
try { try {
await SwaggerParser.validate(file); await SwaggerParser.validate(file);
@ -112,7 +122,7 @@ async function checkFile(file) {
} }
(async () => { (async () => {
for (const file of process.argv.slice(2)) { for (const file of argv._) {
await checkFile(file); await checkFile(file);
} }
})().catch((error) => { })().catch((error) => {

View File

@ -112,6 +112,7 @@ def run() -> None:
["yaml"], ["yaml"],
description="Validates our OpenAPI/Swagger API documentation " description="Validates our OpenAPI/Swagger API documentation "
"(zerver/openapi/zulip.yaml) ", "(zerver/openapi/zulip.yaml) ",
fix_arg="--fix",
) )
linter_config.external_linter( linter_config.external_linter(
"shellcheck", "shellcheck",