-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanonicalize.ts
More file actions
47 lines (42 loc) · 1.47 KB
/
canonicalize.ts
File metadata and controls
47 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Command } from "commander";
import { preprocessOASDocument } from "@patchlogr/oas";
import { type OASStageOptions } from "@patchlogr/oas";
import type { OpenAPI } from "openapi-types";
import fs from "fs/promises";
export type CanonicalizeOptions = OASStageOptions & {
output?: "stdout" | string;
};
export const canonicalizeCommand = new Command("canonicalize")
.argument("<api-docs>", "Path to the OpenAPI specification file")
.option("--skipValidation", "Skip validation of the OpenAPI specification")
.option(
"-o, --output <file>",
"Write result to file instead of stdout (default: stdout)",
"stdout",
)
.action(canonicalizeAction);
export async function canonicalizeAction(
apiDocs: OpenAPI.Document,
options: CanonicalizeOptions,
) {
try {
const rawOutput = await preprocessOASDocument(apiDocs, {
skipValidation: !!options.skipValidation,
});
const output = JSON.stringify(rawOutput, null, 2);
if (options.output === "stdout" || options.output === undefined) {
console.log(output);
} else {
try {
await fs.writeFile(options.output, output, "utf-8");
} catch (error) {
throw new Error(`Failed to write to file ${options.output}:`, {
cause: error,
});
}
}
} catch (error) {
console.error(error);
process.exitCode = 1;
}
}