diff --git a/.changeset/stupid-pans-hug.md b/.changeset/stupid-pans-hug.md new file mode 100644 index 000000000..3396bd531 --- /dev/null +++ b/.changeset/stupid-pans-hug.md @@ -0,0 +1,6 @@ +--- +"openapi-typescript-docs": minor +"openapi-typescript": minor +--- + +feat: Add check option diff --git a/docs/cli.md b/docs/cli.md index 6e3934689..91f0b2009 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -113,6 +113,7 @@ The following flags are supported in the CLI: | `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` | | `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. | | `--enum-values` | | `false` | Export enum values as arrays. | +| `--check` | | `false` | Check that the generated types are up-to-date. | | `--exclude-deprecated` | | `false` | Exclude deprecated fields from types | | `--export-type` | `-t` | `false` | Export `type` instead of `interface` | | `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) | diff --git a/docs/zh/cli.md b/docs/zh/cli.md index 38f646436..36d6e7520 100644 --- a/docs/zh/cli.md +++ b/docs/zh/cli.md @@ -111,7 +111,8 @@ CLI 支持以下参数: | `--default-non-nullable` | | `false` | 将带有默认值的模式对象视为非可空 | | `--empty-objects-unknown` | | `false` | 允许在未指定属性和未指定 `additionalProperties` 的情况下,为模式对象设置任意属性 | | `--enum` | | `false` | 生成真实的 [TS 枚举](https://www.typescriptlang.org/docs/handbook/enums.html),而不是字符串联合。 | -| `--enum-values | | `false` | | +| `--enum-values` | | `false` | 将 enum 值导出为数组 | +| `--check` | | `false` | 检查生成的类型是否是最新的 | | `--exclude-deprecated` | | `false` | 从类型中排除已弃用的字段 | | `--export-type` | `-t` | `false` | 导出 `type` 而不是 `interface` | | `--immutable` | | `false` | 生成不可变类型(只读属性和只读数组) | diff --git a/packages/openapi-typescript/bin/cli.js b/packages/openapi-typescript/bin/cli.js index 858f5c241..a32c39401 100755 --- a/packages/openapi-typescript/bin/cli.js +++ b/packages/openapi-typescript/bin/cli.js @@ -16,6 +16,7 @@ Options --output, -o Specify output file (if not specified in redocly.yaml) --enum Export true TS enums instead of unions --enum-values Export enum values as arrays + --check Check that the generated types are up-to-date. (default: false) --export-type, -t Export top-level \`type\` instead of \`interface\` --immutable Generate readonly types --additional-properties Treat schema objects as if \`additionalProperties: true\` is set @@ -64,6 +65,7 @@ const flags = parser(args, { "emptyObjectsUnknown", "enum", "enumValues", + "check", "excludeDeprecated", "exportType", "help", @@ -90,6 +92,23 @@ function normalizeOutput(output) { return new URL(output, CWD); } +/** + * Check if the generated types are up-to-date. + * @param {string} current - The current generated types. + * @param {URL} outputPath - The path to the output file. + */ +function checkStaleOutput(current, outputPath) { + if (flags.check) { + const previous = fs.readFileSync(outputPath, "utf8"); + if (current === previous) { + process.exit(0); + } else { + error("Generated types are not up-to-date!"); + process.exit(1); + } + } +} + /** * @param {string | URL} schema * @param {@type import('@redocly/openapi-core').Config} redocly @@ -174,6 +193,7 @@ async function main() { } const result = await generateSchema(new URL(api.root, configRoot), { redocly }); const outFile = new URL(api[REDOC_CONFIG_KEY].output, configRoot); + checkStaleOutput(result, outFile); fs.mkdirSync(new URL(".", outFile), { recursive: true }); fs.writeFileSync(outFile, result, "utf8"); done(name, api[REDOC_CONFIG_KEY].output, performance.now() - timeStart); @@ -192,6 +212,7 @@ async function main() { process.stdout.write(result); } else { const outFile = normalizeOutput(flags.output); + checkStaleOutput(result, outFile); fs.mkdirSync(new URL(".", outFile), { recursive: true }); fs.writeFileSync(outFile, result, "utf8"); done("stdin", flags.output, performance.now() - timeStart); @@ -215,6 +236,7 @@ async function main() { process.stdout.write(result); } else { const outFile = normalizeOutput(flags.output); + checkStaleOutput(result, outFile); fs.mkdirSync(new URL(".", outFile), { recursive: true }); fs.writeFileSync(outFile, result, "utf8"); done(input, flags.output, performance.now() - timeStart);