Skip to content

Commit 5de7d22

Browse files
committed
feat: diff command 추가
1 parent 8b31d0f commit 5de7d22

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

packages/patchlogr-cli/src/commands/canonicalize.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export type CanonicalizeOptions = OASStageOptions & {
1111

1212
export const canonicalizeCommand = new Command("canonicalize")
1313
.argument("<api-docs>", "Path to the OpenAPI specification file")
14-
.option("--skipValidation", "Skip validation of the OpenAPI specification")
14+
.option(
15+
"--skipValidation",
16+
"Skip validation of the OpenAPI specification",
17+
true,
18+
)
1519
.option(
1620
"-o, --output <file>",
1721
"Write result to file instead of stdout (default: stdout)",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

packages/patchlogr-cli/src/createCLI.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import pkg from "../package.json";
33

44
import { helpCommand } from "./commands/help";
55
import { canonicalizeCommand } from "./commands/canonicalize";
6+
import { diffCommand } from "./commands/diff";
67

78
export function createCLI() {
89
return new Command()
910
.name("patchlogr")
1011
.version(pkg.version)
1112
.description("PatchlogrCLI : changelogs from openapi specs")
1213
.addCommand(helpCommand)
13-
.addCommand(canonicalizeCommand);
14+
.addCommand(canonicalizeCommand)
15+
.addCommand(diffCommand);
1416
}

0 commit comments

Comments
 (0)