From 1903973a3d38f9307f7f4e31d9ab8791a725399a Mon Sep 17 00:00:00 2001 From: Norbert Dopjera Date: Wed, 11 Sep 2024 02:01:41 +0200 Subject: [PATCH] feat: [#631] implemented refOptions CLI argument --- src/cli.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index e57eb497..be27d35b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,6 +7,8 @@ import isGlob from 'is-glob' import {join, resolve, dirname} from 'path' import {compile, DEFAULT_OPTIONS, Options} from './index' import {pathTransform, error, parseFileAsJSONSchema, justName} from './utils' +import {ParserOptions as $RefOptions} from '@apidevtools/json-schema-ref-parser' +import { merge } from 'lodash' main( minimist(process.argv.slice(2), { @@ -26,7 +28,7 @@ main( 'unreachableDefinitions', ], default: DEFAULT_OPTIONS, - string: ['bannerComment', 'cwd'], + string: ['bannerComment', 'cwd', 'refOptions'], }), ) @@ -36,6 +38,7 @@ async function main(argv: minimist.ParsedArgs) { process.exit(0) } + parseRefOptions(argv) const argIn: string = argv._[0] || argv.input const argOut: string | undefined = argv._[1] || argv.output // the output can be omitted so this can be undefined @@ -159,6 +162,20 @@ async function readStream(stream: NodeJS.ReadStream): Promise { return Buffer.concat(chunks).toString('utf8') } +function parseRefOptions(argv: minimist.ParsedArgs) { + try { + // Parse --refOptions CLI argument and merge with default value + // argv default value already contains predefined $refOptions key + if (argv.refOptions) { + const parsedRefOptions: Partial<$RefOptions> = JSON.parse(argv.refOptions) + merge(argv, { $refOptions: parsedRefOptions }) + } + } catch (e) { + error("Couldn't parse argument --refOptions, make sure it's a valid JSON string.") + throw e + } +} + function printHelp() { const pkg = require('../../package.json')