Skip to content

Commit

Permalink
feat: [631] implemented refOptions CLI argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Dopjera committed Sep 11, 2024
1 parent 118d6a8 commit 226cf8e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand All @@ -26,7 +28,7 @@ main(
'unreachableDefinitions',
],
default: DEFAULT_OPTIONS,
string: ['bannerComment', 'cwd'],
string: ['bannerComment', 'cwd', 'refOptions'],
}),
)

Expand All @@ -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

Expand Down Expand Up @@ -159,6 +162,20 @@ async function readStream(stream: NodeJS.ReadStream): Promise<string> {
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')

Expand Down

0 comments on commit 226cf8e

Please sign in to comment.