Skip to content

Commit baab777

Browse files
committed
feat: Add check option
1 parent 855d5b8 commit baab777

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

docs/cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The following flags are supported in the CLI:
113113
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
114114
| `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. |
115115
| `--enum-values` | | `false` | Export enum values as arrays. |
116+
| `--check` | | `false` | Check that the generated types are up-to-date. |
116117
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
117118
| `--export-type` | `-t` | `false` | Export `type` instead of `interface` |
118119
| `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) |

docs/zh/cli.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ CLI 支持以下参数:
111111
| `--default-non-nullable` | | `false` | 将带有默认值的模式对象视为非可空 |
112112
| `--empty-objects-unknown` | | `false` | 允许在未指定属性和未指定 `additionalProperties` 的情况下,为模式对象设置任意属性 |
113113
| `--enum` | | `false` | 生成真实的 [TS 枚举](https://www.typescriptlang.org/docs/handbook/enums.html),而不是字符串联合。 |
114-
| `--enum-values | | `false` | |
114+
| `--enum-values` | | `false` | 将 enum 值导出为数组 |
115+
| `--check` | | `false` | 检查生成的类型是否是最新的 |
115116
| `--exclude-deprecated` | | `false` | 从类型中排除已弃用的字段 |
116117
| `--export-type` | `-t` | `false` | 导出 `type` 而不是 `interface` |
117118
| `--immutable` | | `false` | 生成不可变类型(只读属性和只读数组) |

packages/openapi-typescript/bin/cli.js

+22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Options
1616
--output, -o Specify output file (if not specified in redocly.yaml)
1717
--enum Export true TS enums instead of unions
1818
--enum-values Export enum values as arrays
19+
--check Check that the generated types are up-to-date. (default: false)
1920
--export-type, -t Export top-level \`type\` instead of \`interface\`
2021
--immutable Generate readonly types
2122
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
@@ -64,6 +65,7 @@ const flags = parser(args, {
6465
"emptyObjectsUnknown",
6566
"enum",
6667
"enumValues",
68+
"check",
6769
"excludeDeprecated",
6870
"exportType",
6971
"help",
@@ -90,6 +92,23 @@ function normalizeOutput(output) {
9092
return new URL(output, CWD);
9193
}
9294

95+
/**
96+
* Check if the generated types are up-to-date.
97+
* @param {string} current - The current generated types.
98+
* @param {URL} outputPath - The path to the output file.
99+
*/
100+
function checkStaleOutput(current, outputPath) {
101+
if (flags.check) {
102+
const previous = fs.readFileSync(outputPath, 'utf8');
103+
if (current === previous) {
104+
process.exit(0);
105+
} else {
106+
error('Generated types are not up-to-date!');
107+
process.exit(1);
108+
}
109+
}
110+
}
111+
93112
/**
94113
* @param {string | URL} schema
95114
* @param {@type import('@redocly/openapi-core').Config} redocly
@@ -174,6 +193,7 @@ async function main() {
174193
}
175194
const result = await generateSchema(new URL(api.root, configRoot), { redocly });
176195
const outFile = new URL(api[REDOC_CONFIG_KEY].output, configRoot);
196+
checkStaleOutput(result, outFile)
177197
fs.mkdirSync(new URL(".", outFile), { recursive: true });
178198
fs.writeFileSync(outFile, result, "utf8");
179199
done(name, api[REDOC_CONFIG_KEY].output, performance.now() - timeStart);
@@ -192,6 +212,7 @@ async function main() {
192212
process.stdout.write(result);
193213
} else {
194214
const outFile = normalizeOutput(flags.output);
215+
checkStaleOutput(result, outFile)
195216
fs.mkdirSync(new URL(".", outFile), { recursive: true });
196217
fs.writeFileSync(outFile, result, "utf8");
197218
done("stdin", flags.output, performance.now() - timeStart);
@@ -215,6 +236,7 @@ async function main() {
215236
process.stdout.write(result);
216237
} else {
217238
const outFile = normalizeOutput(flags.output);
239+
checkStaleOutput(result, outFile)
218240
fs.mkdirSync(new URL(".", outFile), { recursive: true });
219241
fs.writeFileSync(outFile, result, "utf8");
220242
done(input, flags.output, performance.now() - timeStart);

0 commit comments

Comments
 (0)