Skip to content

feat: Add check option #1768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/stupid-pans-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"openapi-typescript-docs": minor
"openapi-typescript": minor
---

feat: Add check option
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
3 changes: 2 additions & 1 deletion docs/zh/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | 生成不可变类型(只读属性和只读数组) |
Expand Down
22 changes: 22 additions & 0 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,6 +65,7 @@ const flags = parser(args, {
"emptyObjectsUnknown",
"enum",
"enumValues",
"check",
"excludeDeprecated",
"exportType",
"help",
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down