diff --git a/packages/openapi-diff/bin/fresha-openapi-diff.js b/packages/openapi-diff/bin/fresha-openapi-diff.js index 5042d30d..4944ab27 100755 --- a/packages/openapi-diff/bin/fresha-openapi-diff.js +++ b/packages/openapi-diff/bin/fresha-openapi-diff.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -require('@fresha/openapi-diff/build/index'); +require("@fresha/openapi-diff/build/cli"); diff --git a/packages/openapi-diff/package.json b/packages/openapi-diff/package.json index 972dc449..837a11bd 100644 --- a/packages/openapi-diff/package.json +++ b/packages/openapi-diff/package.json @@ -14,6 +14,13 @@ "url": "git+https://github.com/surgeventures/api-tools.git", "directory": "packages/openapi-diff" }, + "exports": { + ".": { + "require": "./build/index.js", + "types": "./build/index.d.ts", + "default": "./build/index.js" + } + }, "scripts": { "build": "tsc --project tsconfig.build.json", "build:watch": "npm run build -- --watch", diff --git a/packages/openapi-diff/src/cli.ts b/packages/openapi-diff/src/cli.ts new file mode 100644 index 00000000..8e52194f --- /dev/null +++ b/packages/openapi-diff/src/cli.ts @@ -0,0 +1,66 @@ +import assert from "assert"; +import console from "console"; +import fs from "fs"; + +import { + OpenAPIObject, + OpenAPIReader, +} from "@fresha/openapi-model/build/3.0.3"; +import yaml from "yaml"; +import yargs from "yargs"; +import { hideBin } from "yargs/helpers"; + +import { Differ } from "./Differ"; + +try { + const argv = yargs(hideBin(process.argv)) + .epilog( + "For more information, see https://github.com/fresha/api-tools/tree/main/packages/openapi-diff" + ) + .usage("Usage: $0 [OPTIONS] FILE1 FILE2") + .boolean("print-version") + .describe("print-version", "Prints suggested new version") + .boolean("update-version") + .describe("update-version", "Updates schema version based on changes") + .boolean("verbose") + .alias("verbose", "v") + .describe("verbose", "Print more information on console") + .parseSync(); + + const [inputPath1, inputPath2] = argv._; + assert(typeof inputPath1 === "string", `Required a filename`); + assert(typeof inputPath2 === "string", `Required a filename`); + + const reader = new OpenAPIReader(); + const openapi1 = reader.parseFromFile(inputPath1); + const openapi2 = reader.parseFromFile(inputPath2); + + const differ = new Differ(openapi1, openapi2); + differ.calculate(); + + if (argv.printVersion) { + const suggestedNewVersion = differ.newVersion; + console.log(suggestedNewVersion); + } else if (argv.updateVersion) { + // temporarily use yaml, to retain attribute order + const inputText = fs.readFileSync(inputPath2, "utf-8"); + const data = yaml.parse(inputText) as OpenAPIObject; + + data.info.version = differ.newVersion; + + const text = yaml.stringify(data); + fs.writeFileSync(inputPath2, text, "utf-8"); + + // openapi2.info.version = differ.newVersion; + // const writer = new OpenAPIWriter(); + // writer.writeToFile(openapi2, inputPath2); + } else { + differ.print(); + if (differ.outdatedVersion) { + process.exit(1); + } + } +} catch (err) { + console.log(err); + process.exit(1); +} diff --git a/packages/openapi-diff/src/index.ts b/packages/openapi-diff/src/index.ts index 73b0ac7b..1ff7f761 100644 --- a/packages/openapi-diff/src/index.ts +++ b/packages/openapi-diff/src/index.ts @@ -1,65 +1,8 @@ -import assert from 'assert'; -import console from 'console'; -import fs from 'fs'; - -import { OpenAPIObject, OpenAPIReader } from '@fresha/openapi-model/build/3.0.3'; -import yaml from 'yaml'; -import yargs from 'yargs'; -import { hideBin } from 'yargs/helpers'; - -import { Differ } from './Differ'; - -import './model'; - -try { - const argv = yargs(hideBin(process.argv)) - .epilog( - 'For more information, see https://github.com/fresha/api-tools/tree/main/packages/openapi-diff', - ) - .usage('Usage: $0 [OPTIONS] FILE1 FILE2') - .boolean('print-version') - .describe('print-version', 'Prints suggested new version') - .boolean('update-version') - .describe('update-version', 'Updates schema version based on changes') - .boolean('verbose') - .alias('verbose', 'v') - .describe('verbose', 'Print more information on console') - .parseSync(); - - const [inputPath1, inputPath2] = argv._; - assert(typeof inputPath1 === 'string', `Required a filename`); - assert(typeof inputPath2 === 'string', `Required a filename`); - - const reader = new OpenAPIReader(); - const openapi1 = reader.parseFromFile(inputPath1); - const openapi2 = reader.parseFromFile(inputPath2); - - const differ = new Differ(openapi1, openapi2); - differ.calculate(); - - if (argv.printVersion) { - const suggestedNewVersion = differ.newVersion; - console.log(suggestedNewVersion); - } else if (argv.updateVersion) { - // temporarily use yaml, to retain attribute order - const inputText = fs.readFileSync(inputPath2, 'utf-8'); - const data = yaml.parse(inputText) as OpenAPIObject; - - data.info.version = differ.newVersion; - - const text = yaml.stringify(data); - fs.writeFileSync(inputPath2, text, 'utf-8'); - - // openapi2.info.version = differ.newVersion; - // const writer = new OpenAPIWriter(); - // writer.writeToFile(openapi2, inputPath2); - } else { - differ.print(); - if (differ.outdatedVersion) { - process.exit(1); - } - } -} catch (err) { - console.log(err); - process.exit(1); -} +export * from './Differ'; +export * from './DiffItem'; +export * from './types'; +export * from './model/types'; +import { Differ as DifferModal } from './model/Differ'; +export { + DifferModal, +} \ No newline at end of file