From a08c138c034c789cd8be90bb2f9496676b5b2347 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 8 Sep 2021 17:02:42 -0700 Subject: [PATCH] check-openapi: Add option to automatically fix errors. Signed-off-by: Anders Kaseorg --- package.json | 1 + tools/check-openapi | 12 +++++++++++- tools/lint | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e290cc0fc9..f3bafd6a2c 100644 --- a/package.json +++ b/package.json @@ -126,6 +126,7 @@ "webpack-dev-server": "^3.5.1", "xvfb": "^0.4.0", "yaml": "^2.0.0-8", + "yargs": "^15.0.2", "yarn-deduplicate": "^3.0.0", "zulip-js": "^2.0.8" }, diff --git a/tools/check-openapi b/tools/check-openapi index 555549c574..6ef028dfe5 100755 --- a/tools/check-openapi +++ b/tools/check-openapi @@ -9,6 +9,12 @@ const ExampleValidator = require("openapi-examples-validator"); const Prettier = require("prettier"); const SwaggerParser = require("swagger-parser"); 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) { const yaml = await fs.promises.readFile(file, "utf8"); @@ -92,6 +98,10 @@ async function checkFile(file) { if (!ok) { 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 { await SwaggerParser.validate(file); @@ -112,7 +122,7 @@ async function checkFile(file) { } (async () => { - for (const file of process.argv.slice(2)) { + for (const file of argv._) { await checkFile(file); } })().catch((error) => { diff --git a/tools/lint b/tools/lint index 94495d59d8..c90e6d22d4 100755 --- a/tools/lint +++ b/tools/lint @@ -112,6 +112,7 @@ def run() -> None: ["yaml"], description="Validates our OpenAPI/Swagger API documentation " "(zerver/openapi/zulip.yaml) ", + fix_arg="--fix", ) linter_config.external_linter( "shellcheck",