11#!/usr/bin/env node
22
3- 'use strict'
3+ 'use strict' ;
44
55const _ = require ( 'lodash' ) ;
6- const { getName, getProperty } = require ( './src/utils/schemaUtils' ) ;
6+ const fs = require ( 'fs' ) ;
7+ const path = require ( 'path' ) ;
8+ const { getName , getNameExample, getProperty } = require ( './src/utils/schemaUtils' ) ;
9+
10+ const { verifyProperties , errorsList} = require ( './src/utils/verifyProperties' )
11+
712const createComponent = require ( "./src/generator/createComponent" ) ( ) ;
813
914const argv = require ( 'yargs' ) ( process . argv . slice ( 2 ) )
1015 . usage ( 'Usage: $0 -f [file]' )
1116 . option ( 'f' , {
1217 alias : 'file' ,
1318 describe : 'Path to openapi file' ,
14- type : 'string'
19+ type : 'string' ,
20+ demandOption : true // Hacer que el archivo sea obligatorio
1521 } )
1622 . example ( '\x1b[32m $0 -f /path/to/openapi.yaml \x1b[0m' )
1723 . example ( '\x1b[32m $0 -f /path/to/openapi.yml \x1b[0m' )
@@ -21,82 +27,75 @@ const argv = require('yargs')(process.argv.slice(2))
2127
2228global . definition = require ( './src/parser/definition.js' ) ( ) ;
2329
24- Object . keys ( global . definition . paths ) . forEach ( path => {
25-
26- Object . keys ( global . definition . paths [ path ] ) . forEach ( method => {
27-
28- const methodObj = global . definition . paths [ path ] [ method ] ;
29-
30- Object . keys ( methodObj . responses ?? [ ] ) . forEach ( key => {
31-
32- const response = methodObj . responses [ key ] ;
33- const property = getProperty ( response , path , method , key ) ;
34- const schemaPath = `${ property } .content.application/json.schema` ;
35- const nameExample = getName ( schemaPath , key ) ;
36-
37-
38- if ( _ . get ( global . definition , schemaPath ) ?. [ 'example' ] || ! _ . get ( global . definition , schemaPath ) ?. [ '$ref' ] ) {
39-
40- const exampleKey = `${ property } .content.application/json.examples.${ nameExample } ` ;
4130
42- if ( _ . get ( global . definition , schemaPath ) ?. [ 'example' ] ) {
31+ function generateExample ( property , schemaPath , nameExample , example , nameSchema ) {
32+ const exampleKey = `${ property } .content.application/json.examples.${ nameExample } ` ;
33+ const routeKey = createComponent ( definition , example , exampleKey ) ;
4334
44- const example = _ . get ( global . definition , schemaPath ) ?. [ 'example' ] ;
45- const routeKey = createComponent ( global . definition , example , exampleKey ) ;
4635
47- _ . unset ( global . definition , `${ property } .content.application/json.schema.example` )
48- _ . set ( global . definition , `${ property } .content.application/json.examples.${ nameExample } ` , { $ref : routeKey } ) ;
36+ const validate = verifyProperties ( example , schemaPath , nameSchema , exampleKey ) ;
4937
50- require ( './src/utils/sucess' ) ( `Example generated for ${ exampleKey } ` )
5138
52- }
53-
54- }
39+ if ( ! validate ) {
40+ return ;
41+ }
5542
56- if ( _ . get ( global . definition , `${ property } .content.application/json.example` ) ) {
43+ _ . unset ( definition , `${ property } .content.application/json.example` ) ;
44+ _ . set ( definition , exampleKey , { $ref : routeKey } ) ;
5745
58- const exampleKey = ` ${ property } .content.application/json.examples. ${ nameExample } ` ;
59- const example = _ . get ( global . definition , ` ${ property } .content.application/json.example `) ;
46+
47+ require ( './src/utils/sucess' ) ( `Example generated in ${ exampleKey . replace ( 'paths./' , '#/' ) . replace ( / \. / g , '/' ) } `) ;
6048
61- const routeKey = createComponent ( global . definition , example , exampleKey ) ;
49+ }
6250
63- if ( _ . get ( global . definition , `${ property } .content.application/json.example` ) ) {
6451
65- _ . unset ( global . definition , `${ property } .content.application/json.example` )
66- _ . set ( global . definition , `${ property } .content.application/json.examples.${ nameExample } ` , { $ref : routeKey } ) ;
52+ Object . keys ( definition . paths ) . forEach ( path => {
53+ Object . keys ( definition . paths [ path ] ) . forEach ( method => {
54+ const methodObj = definition . paths [ path ] [ method ] ;
6755
68- require ( './src/utils/sucess' ) ( `Example generated for ${ exampleKey } ` )
56+ Object . keys ( methodObj . responses ?? [ ] ) . forEach ( key => {
57+ const response = methodObj . responses [ key ] ;
58+ const property = getProperty ( response , path , method , key ) ;
59+ const schemaPath = `${ property } .content.application/json.schema` ;
6960
70- }
7161
72- }
62+ const nameExample = getNameExample ( schemaPath ) ;
63+ const schema = _ . get ( definition , schemaPath ) ;
64+ const schemaDetails = require ( './src/parser/refs.js' ) ( schema , definition ) ;
7365
74- if ( response ?. [ '$ref' ] && ! _ . get ( global . definition , ` ${ property } .content.application/json.examples` ) ) {
75-
76- if ( _ . get ( global . definition , ` ${ property } .content.application/json. example` ) ) {
77- _ . unset ( global . definition , ` ${ property } .content.application/json. example` )
66+
67+ if ( schema ) {
68+ if ( schema . example ) {
69+ generateExample ( property , schemaDetails , nameExample , schema . example , getName ( schemaPath ) ) ;
7870 }
7971
80- const exampleKey = `${ property } .content.application/json.examples.${ nameExample } ` ;
81-
82- require ( './src/generator/examples.js' ) ( _ . get ( global . definition , schemaPath ) , global . definition , exampleKey ) ;
83-
84-
85- } else if ( _ . get ( response , 'content.application/json.schema' ) && ! _ . get ( response , 'content.application/json.examples' ) ) {
86-
87- const schemaPath = `${ property } .content.application/json.schema` ;
88- const exampleKey = `${ property } .content.application/json.examples.${ nameExample } ` ;
89-
90- if ( _ . get ( global . definition , `${ property } .content.application/json.example` ) ) {
91- _ . unset ( global . definition , `${ property } .content.application/json.example` )
72+ if ( _ . get ( response , 'content.application/json.example' ) ) {
73+ generateExample ( property , schemaDetails , nameExample , _ . get ( response , 'content.application/json.example' ) , getName ( schemaPath ) ) ;
9274 }
9375
76+ if ( response ?. [ '$ref' ] && ! _ . get ( definition , `${ property } .content.application/json.examples` ) ) {
77+ console . log ( 'entro' )
78+ if ( _ . get ( response , 'content.application/json.example' ) ) {
79+ _ . unset ( definition , `${ property } .content.application/json.example` ) ;
80+ }
81+ require ( './src/generator/examples.js' ) ( schema , definition , `${ property } .content.application/json.examples.${ nameExample } ` , schemaDetails , getName ( schemaPath ) )
82+ }
83+ }
9484
95- require ( './src/generator/examples.js' ) ( _ . get ( global . definition , schemaPath ) , global . definition , exampleKey ) ;
85+ if ( response ?. [ 'content' ] ?. [ 'application/json' ] ?. schema && ! response ?. [ 'content' ] ?. [ 'application/json' ] ?. examples ) {
86+ require ( './src/generator/examples.js' ) ( schema , definition , `${ property } .content.application/json.examples.${ nameExample } ` , schemaDetails , getName ( schemaPath ) )
9687 }
9788 } ) ;
9889 } ) ;
9990} ) ;
10091
10192
93+ if ( errorsList . length !== 0 ) {
94+ console . error ( 'Errors found in the examples:' ) ;
95+ console . log ( errorsList ) ;
96+ }
97+
98+
99+
100+
102101require ( './src/generator/file.js' ) ( ) ;
0 commit comments