Skip to content

Commit 32ddb0c

Browse files
committed
Split into functions, to call/test separately
1 parent baab5ce commit 32ddb0c

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

index.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@ import jsonpath from 'jsonpath';
33
import {safeStringify,parseWithPointers} from "@stoplight/yaml";
44
import mergician from 'mergician';
55

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) {
207
// Use jsonpath.apply to do the changes
21-
228
overlay.actions.forEach((a)=>{
239
jsonpath.apply(spec, a.target, (chunk) => {
2410
// Is it a remove?
@@ -36,11 +22,32 @@ try {
3622
});
3723
})
3824

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);
4245

43-
} catch (err) {
44-
console.error(err);
4546
}
4647

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

Comments
 (0)