Skip to content
41 changes: 0 additions & 41 deletions packages/patchlogr-cli/src/cli.ts

This file was deleted.

47 changes: 47 additions & 0 deletions packages/patchlogr-cli/src/commands/canonicalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
7 changes: 7 additions & 0 deletions packages/patchlogr-cli/src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Command } from "commander";

export const helpCommand = new Command("help")
.description("Display help information about patchlogr commands")
.action((_options, command) => {
command.parent?.outputHelp();
});
29 changes: 0 additions & 29 deletions packages/patchlogr-cli/src/commands/runCanonicalize.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/patchlogr-cli/src/createCLI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Command } from "commander";
import pkg from "../package.json";

import { helpCommand } from "./commands/help";
import { canonicalizeCommand } from "./commands/canonicalize";

export function createCLI() {
return new Command()
.name("patchlogr")
.version(pkg.version)
.description("PatchlogrCLI : changelogs from openapi specs")
.addCommand(helpCommand)
.addCommand(canonicalizeCommand);
}
2 changes: 1 addition & 1 deletion packages/patchlogr-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCLI } from "./cli";
import { createCLI } from "./createCLI";

const program = createCLI();

Expand Down
4 changes: 3 additions & 1 deletion packages/patchlogr-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": false
"emitDeclarationOnly": false,

"resolveJsonModule": true
},
"include": ["src"]
}
Loading