22import { program } from 'commander'
33import inquirer from 'inquirer'
44import fs from 'fs'
5- import yaml from 'js-yaml'
65import ejs from 'ejs'
76import path from 'path'
87import { fileURLToPath } from 'url'
9- import $RefParser from '@apidevtools/json-schema-ref-parser'
108import { UI } from './constants/index.js'
119import {
1210 validateCommandInput ,
@@ -16,9 +14,8 @@ import {
1614 validateInquirerInput ,
1715 validateInquirerOutput ,
1816 isValidUrl ,
19- isJSON ,
20- isYAML ,
2117} from './utils/validate.js'
18+ import { parseOpenApi } from './utils/parser.js'
2219
2320const __filename = fileURLToPath ( import . meta. url )
2421const __dirname = path . dirname ( __filename )
@@ -50,7 +47,11 @@ function getCliOptions() {
5047 'Output HTML file name' ,
5148 validateCommandOutput ,
5249 )
53- . option ( '--ui <ui>' , `Choose UI (${ UI . join ( ', ' ) } )` , validateCommandUi )
50+ . option (
51+ '--ui <ui>' ,
52+ `Choose UI (${ Object . values ( UI ) . join ( ', ' ) } )` ,
53+ validateCommandUi ,
54+ )
5455 . option ( '--title <title>' , 'Title of the HTML page' , 'OpenAPI Docs' )
5556 . option ( '--description <description>' , 'Description of the HTML page' , '' )
5657 . option (
@@ -68,8 +69,8 @@ async function askQuestions(options) {
6869 type : 'list' ,
6970 name : 'ui' ,
7071 message : 'Which UI would you like to use?' ,
71- default : UI [ 0 ] ,
72- choices : UI ,
72+ default : UI . STOPLIGHT ,
73+ choices : Object . values ( UI ) ,
7374 when : ! options . ui ,
7475 } ,
7576 {
@@ -114,29 +115,15 @@ async function renderOpenApiHtml(result) {
114115 } else {
115116 rawApiDocsText = fs . readFileSync ( input , 'utf-8' )
116117 }
117-
118- let rawApiDocs
119- if ( isJSON ( rawApiDocsText ) ) {
120- rawApiDocs = JSON . parse ( rawApiDocsText )
121- } else if ( isYAML ( rawApiDocsText ) ) {
122- rawApiDocs = yaml . load ( rawApiDocsText )
123- } else {
124- throw new Error (
125- 'Unsupported file format. Please provide a .json or .yaml/.yml file.' ,
126- )
127- }
128-
129- // resolve $ref pointers
130- // https://github.com/APIDevTools/json-schema-ref-parser/blob/main/docs/ref-parser.md#bundleschema-options-callback
131- const apiDocs = await $RefParser . bundle ( rawApiDocs )
118+ const apiDocs = await parseOpenApi ( rawApiDocsText , ui )
132119
133120 return ejs . render ( template , {
134121 theme : result . theme ,
135122 title : result . title ,
136123 description : result . description ,
137124 jsContent,
138125 cssContent,
139- apiDocs : JSON . stringify ( apiDocs ) ,
126+ apiDocs,
140127 } )
141128}
142129
0 commit comments