diff --git a/packages/patchlogr-cli/src/cli.ts b/packages/patchlogr-cli/src/cli.ts deleted file mode 100644 index 8bc66cf..0000000 --- a/packages/patchlogr-cli/src/cli.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Command } from "commander"; -import { runCanonicalize } from "./commands/runCanonicalize"; - -export function createCLI() { - const program = new Command(); - - program - .name("patchlogr") - .version("0.0.0") - .description("PatchlogrCLI : changelogs from openapi specs"); - - program - .command("help") - .description("Display help information about patchlogr commands") - .action(() => { - program.outputHelp(); - }); - - program - .command("canonicalize") - .argument("", "Path to the OpenAPI specification file") - .option( - "--skipValidation", - "Skip validation of the OpenAPI specification", - ) - .option( - "-o, --output ", - "Write result to file instead of stdout (default: stdout)", - "stdout", - ) - .action(async (apiDocs, options) => { - try { - await runCanonicalize(apiDocs, options); - } catch (error) { - console.error(error); - process.exitCode = 1; - } - }); - - return program; -} diff --git a/packages/patchlogr-cli/src/commands/canonicalize.ts b/packages/patchlogr-cli/src/commands/canonicalize.ts new file mode 100644 index 0000000..5392b52 --- /dev/null +++ b/packages/patchlogr-cli/src/commands/canonicalize.ts @@ -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("", "Path to the OpenAPI specification file") + .option("--skipValidation", "Skip validation of the OpenAPI specification") + .option( + "-o, --output ", + "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; + } +} diff --git a/packages/patchlogr-cli/src/commands/help.ts b/packages/patchlogr-cli/src/commands/help.ts new file mode 100644 index 0000000..445afc9 --- /dev/null +++ b/packages/patchlogr-cli/src/commands/help.ts @@ -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(); + }); diff --git a/packages/patchlogr-cli/src/commands/runCanonicalize.ts b/packages/patchlogr-cli/src/commands/runCanonicalize.ts deleted file mode 100644 index 03a7461..0000000 --- a/packages/patchlogr-cli/src/commands/runCanonicalize.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { preprocessOASDocument } from "@patchlogr/oas"; -import { type OASStageOptions } from "@patchlogr/oas"; -import type { OpenAPI } from "openapi-types"; -import fs from "fs/promises"; - -export type RunCanonicalizeOptions = OASStageOptions & { - output?: "stdout" | string; -}; - -export async function runCanonicalize( - apiDocs: OpenAPI.Document, - options: RunCanonicalizeOptions, -) { - const output = await preprocessOASDocument(apiDocs, { - skipValidation: !!options.skipValidation, - }); - - if (options.output === "stdout" || options.output === undefined) { - console.log(JSON.stringify(output, null, 2)); - } else { - try { - await fs.writeFile(options.output, JSON.stringify(output, null, 2)); - } catch (error) { - throw new Error(`Failed to write to file ${options.output}:`, { - cause: error, - }); - } - } -} diff --git a/packages/patchlogr-cli/src/createCLI.ts b/packages/patchlogr-cli/src/createCLI.ts new file mode 100644 index 0000000..a1498f2 --- /dev/null +++ b/packages/patchlogr-cli/src/createCLI.ts @@ -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); +} diff --git a/packages/patchlogr-cli/src/index.ts b/packages/patchlogr-cli/src/index.ts index 914ae17..67fe5fa 100644 --- a/packages/patchlogr-cli/src/index.ts +++ b/packages/patchlogr-cli/src/index.ts @@ -1,4 +1,4 @@ -import { createCLI } from "./cli"; +import { createCLI } from "./createCLI"; const program = createCLI(); diff --git a/packages/patchlogr-cli/tsconfig.json b/packages/patchlogr-cli/tsconfig.json index 8e56657..3eaafcb 100644 --- a/packages/patchlogr-cli/tsconfig.json +++ b/packages/patchlogr-cli/tsconfig.json @@ -7,7 +7,9 @@ "declaration": true, "declarationMap": true, - "emitDeclarationOnly": false + "emitDeclarationOnly": false, + + "resolveJsonModule": true }, "include": ["src"] }