@@ -13,7 +13,7 @@ function increaseVerbosity(dummyValue, previous) {
1313}
1414
1515program
16- . arguments ( '< oaFile> ' )
16+ . arguments ( '[ oaFile] ' )
1717 . usage ( '<file> [options]' )
1818 . description ( 'Format an OpenAPI document by ordering, formatting and filtering fields.' )
1919 . option ( '-o, --output <output>' , 'save the formatted OpenAPI file as JSON/YAML' )
@@ -59,11 +59,6 @@ async function run(oaFile, options) {
5959 let cliLog = { } ;
6060 const consoleLine = process . stdout . columns ? '=' . repeat ( process . stdout . columns ) : '=' . repeat ( 80 ) ;
6161
62- if ( ! oaFile ) {
63- console . error ( 'Please provide a file path for the OpenAPI document' ) ;
64- return ;
65- }
66-
6762 infoOut ( `${ consoleLine } ` ) ; // LOG - horizontal rule
6863 infoOut ( `OpenAPI-Format CLI settings:` ) ; // LOG - config file
6964
@@ -216,24 +211,72 @@ async function run(oaFile, options) {
216211 }
217212 }
218213
214+ // Allow missing input file if overlay extends is provided
215+ if ( ! oaFile ) {
216+ const hasOverlay = ! ! options ?. overlaySet ;
217+ const extendsRef = options ?. overlaySet ?. extends ;
218+ if ( extendsRef ) {
219+ // Resolve local relative paths against the overlay file location
220+ const isRemote =
221+ typeof extendsRef === 'string' && ( extendsRef . startsWith ( 'http://' ) || extendsRef . startsWith ( 'https://' ) ) ;
222+ if ( isRemote ) {
223+ oaFile = extendsRef ;
224+ } else {
225+ const baseDir = options ?. overlayFile ? path . dirname ( path . resolve ( options . overlayFile ) ) : process . cwd ( ) ;
226+ oaFile = path . isAbsolute ( extendsRef ) ? extendsRef : path . resolve ( baseDir , extendsRef ) ;
227+ }
228+ infoOut ( `- Input file (extends):\t${ oaFile } ` ) ;
229+ } else {
230+ if ( ! hasOverlay ) {
231+ console . error ( 'Please provide a file path for the OpenAPI document' ) ;
232+ } else {
233+ console . error ( 'Please provide an input file or an overlay with an "extends" property' ) ;
234+ }
235+ return ;
236+ }
237+ }
238+
219239 let resObj = { } ;
220240 let output = { } ;
221241 let input = { } ;
222242 let fileOptions = { keepComments : options . keepComments ?? false , bundle : options . bundle ?? true } ;
223243
224244 try {
225- infoOut ( `- Input file:\t\t${ oaFile } ` ) ; // LOG - Input file
245+ if ( ! options ?. overlaySet ?. extends ) {
246+ infoOut ( `- Input file:\t\t${ oaFile } ` ) ; // LOG - Input file (standard)
247+ }
226248
227249 // Parse input content
228250 resObj = await openapiFormat . parseFile ( oaFile , fileOptions ) ;
229251 input = resObj ;
230252 } catch ( err ) {
231- if ( err . code !== 'ENOENT' ) {
232- console . error ( '\x1b[31m' , `Input file error - Failed to read file: ${ err . message } ` ) ;
253+ // If input file missing but overlay extends is present, fallback to extends
254+ const extendsRef = options ?. overlaySet ?. extends ;
255+ if ( err . code === 'ENOENT' && extendsRef ) {
256+ const isRemote =
257+ typeof extendsRef === 'string' && ( extendsRef . startsWith ( 'http://' ) || extendsRef . startsWith ( 'https://' ) ) ;
258+ if ( isRemote ) {
259+ oaFile = extendsRef ;
260+ } else {
261+ const baseDir = options ?. overlayFile ? path . dirname ( path . resolve ( options . overlayFile ) ) : process . cwd ( ) ;
262+ oaFile = path . isAbsolute ( extendsRef ) ? extendsRef : path . resolve ( baseDir , extendsRef ) ;
263+ }
264+ infoOut ( `- Input file (extends):\t${ oaFile } ` ) ;
265+ try {
266+ resObj = await openapiFormat . parseFile ( oaFile , fileOptions ) ;
267+ input = resObj ;
268+ } catch ( err2 ) {
269+ console . error ( '\x1b[31m' , `Input file error - Failed to read file: ${ err2 . message } ` ) ;
270+ process . exit ( 1 ) ;
271+ }
272+ } else {
273+ if ( err . code !== 'ENOENT' ) {
274+ console . error ( '\x1b[31m' , `Input file error - Failed to read file: ${ err . message } ` ) ;
275+ process . exit ( 1 ) ;
276+ }
277+ console . error ( '\x1b[31m' , `Input file error - Failed to read file: ${ oaFile } ` ) ;
233278 process . exit ( 1 ) ;
234279 }
235- console . error ( '\x1b[31m' , `Input file error - Failed to read file: ${ oaFile } ` ) ;
236- process . exit ( 1 ) ;
237280 }
238281
239282 // Generate elements for OpenAPI document
0 commit comments