@@ -3,22 +3,8 @@ import jsonpath from 'jsonpath';
3
3
import { safeStringify , parseWithPointers } from "@stoplight/yaml" ;
4
4
import mergician from 'mergician' ;
5
5
6
- // example: node index.js test/specs/petstore.yaml test/overlays/overlay.yaml
7
- const openapiFile = process . argv [ 2 ]
8
- const overlayFile = process . argv [ 3 ]
9
-
10
- try {
11
-
12
- // Parse the "input" OpenAPI document
13
- const specraw = fs . readFileSync ( openapiFile , 'utf8' ) ;
14
- var spec = parseWithPointers ( specraw ) . data ;
15
-
16
- // Parse the "overlay" document
17
- const overlayraw = fs . readFileSync ( overlayFile , 'utf8' ) ;
18
- const overlay = parseWithPointers ( overlayraw ) . data ;
19
-
6
+ function applyOverlayToOpenAPI ( spec , overlay ) {
20
7
// Use jsonpath.apply to do the changes
21
-
22
8
overlay . actions . forEach ( ( a ) => {
23
9
jsonpath . apply ( spec , a . target , ( chunk ) => {
24
10
// Is it a remove?
@@ -36,11 +22,32 @@ try {
36
22
} ) ;
37
23
} )
38
24
39
- // Output the new spec
40
- //console.log(JSON.stringify(spec, null, 2));
41
- console . log ( safeStringify ( spec ) ) ;
25
+ return spec ;
26
+ }
27
+
28
+ export function overlayFiles ( openapiFile , overlayFile ) {
29
+ // Parse the "input" OpenAPI document
30
+ //return openapiFile;
31
+ console . debug ( "Hi" ) ;
32
+ console . debug ( openapiFile ) ;
33
+ console . debug ( typeof openapiFile ) ;
34
+ const specraw = fs . readFileSync ( openapiFile , 'utf8' ) ;
35
+ var spec = parseWithPointers ( specraw ) . data ;
36
+
37
+ // Parse the "overlay" document
38
+ const overlayraw = fs . readFileSync ( overlayFile , 'utf8' ) ;
39
+ const overlay = parseWithPointers ( overlayraw ) . data ;
40
+
41
+ spec = applyOverlayToOpenAPI ( spec , overlay ) ;
42
+
43
+ // Return the new spec
44
+ return safeStringify ( spec ) ;
42
45
43
- } catch ( err ) {
44
- console . error ( err ) ;
45
46
}
46
47
48
+ // example: node index.js test/openapi/petstore.yaml test/overlays/overlay.yaml
49
+ const openapiFile = process . argv [ 2 ]
50
+ const overlayFile = process . argv [ 3 ]
51
+ var spec = overlayFiles ( openapiFile , overlayFile ) ;
52
+ console . log ( spec ) ;
53
+
0 commit comments