|
| 1 | +import { preprocessOASDocument } from "@patchlogr/adapter-oas"; |
| 2 | +import { |
| 3 | + detectVersionBump, |
| 4 | + diffSpec, |
| 5 | + partitionByMethod, |
| 6 | + partitionByTag, |
| 7 | + type PartitionedSpec, |
| 8 | +} from "@patchlogr/core"; |
| 9 | + |
| 10 | +import { Command } from "commander"; |
| 11 | +import { OpenAPI } from "openapi-types"; |
| 12 | + |
| 13 | +import fs from "fs/promises"; |
| 14 | + |
| 15 | +export type DiffOptions = { |
| 16 | + partition: "tag" | "method"; |
| 17 | + output?: "stdout" | string; |
| 18 | + skipValidation: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +export const diffCommand = new Command("diff") |
| 22 | + .description("Diff two OpenAPI Specification files") |
| 23 | + .argument("<base>", "Base OpenAPI Specification file") |
| 24 | + .argument("<head>", "Head OpenAPI Specification file") |
| 25 | + .option("-o, --output <file>", "Output file") |
| 26 | + .option("--skipValidation", "Skip validation", true) |
| 27 | + .option("-p, --partition <partition>", "Partition Strategy", "tag") |
| 28 | + .action(diffAction); |
| 29 | + |
| 30 | +export async function diffAction( |
| 31 | + basePath: OpenAPI.Document, |
| 32 | + headPath: OpenAPI.Document, |
| 33 | + options: DiffOptions, |
| 34 | +) { |
| 35 | + const preprocessedBase = await preprocessOASDocument(basePath, { |
| 36 | + skipValidation: options.skipValidation, |
| 37 | + }); |
| 38 | + const preprocessedHead = await preprocessOASDocument(headPath, { |
| 39 | + skipValidation: options.skipValidation, |
| 40 | + }); |
| 41 | + |
| 42 | + const baseCanonicalSpec = preprocessedBase.canonicalSpec; |
| 43 | + const headCanonicalSpec = preprocessedHead.canonicalSpec; |
| 44 | + |
| 45 | + if (!baseCanonicalSpec || !headCanonicalSpec) { |
| 46 | + throw new Error("Failed to preprocess OpenAPI Specification files"); |
| 47 | + } |
| 48 | + |
| 49 | + let partitionedBase: PartitionedSpec; |
| 50 | + let partitionedHead: PartitionedSpec; |
| 51 | + |
| 52 | + switch (options.partition) { |
| 53 | + case "method": |
| 54 | + partitionedBase = partitionByMethod(baseCanonicalSpec); |
| 55 | + partitionedHead = partitionByMethod(headCanonicalSpec); |
| 56 | + break; |
| 57 | + case "tag": |
| 58 | + default: |
| 59 | + partitionedBase = partitionByTag(baseCanonicalSpec); |
| 60 | + partitionedHead = partitionByTag(headCanonicalSpec); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + const specChangeSet = diffSpec(partitionedBase, partitionedHead); |
| 65 | + const versionBump = detectVersionBump(specChangeSet); |
| 66 | + |
| 67 | + if (options.output === "stdout" || options.output === undefined) { |
| 68 | + console.log(JSON.stringify(specChangeSet, null, 2)); |
| 69 | + console.log(JSON.stringify(versionBump, null, 2)); |
| 70 | + } else { |
| 71 | + try { |
| 72 | + await fs.writeFile( |
| 73 | + options.output.concat(".json"), |
| 74 | + JSON.stringify(specChangeSet, null, 2), |
| 75 | + "utf-8", |
| 76 | + ); |
| 77 | + await fs.writeFile( |
| 78 | + options.output.concat(".version.json"), |
| 79 | + JSON.stringify(versionBump, null, 2), |
| 80 | + "utf-8", |
| 81 | + ); |
| 82 | + } catch (error) { |
| 83 | + throw new Error(`Failed to write to file ${options.output}:`, { |
| 84 | + cause: error, |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments