Skip to content

Commit 225fbeb

Browse files
committed
Refactor to src directory with index as entry point
1 parent 32ddb0c commit 225fbeb

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed

index.js

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,4 @@
1-
import fs from 'fs';
2-
import jsonpath from 'jsonpath';
3-
import {safeStringify,parseWithPointers} from "@stoplight/yaml";
4-
import mergician from 'mergician';
5-
6-
function applyOverlayToOpenAPI(spec, overlay) {
7-
// Use jsonpath.apply to do the changes
8-
overlay.actions.forEach((a)=>{
9-
jsonpath.apply(spec, a.target, (chunk) => {
10-
// Is it a remove?
11-
if (a.hasOwnProperty('remove')) {
12-
return {};
13-
}
14-
15-
// It must be an update
16-
17-
// Deep merge using a module (built-in spread operator is only shallow)
18-
const merger = mergician({appendArrays: true});
19-
const merged = merger(chunk, a.update);
20-
return merged;
21-
22-
});
23-
})
24-
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);
45-
46-
}
1+
import {overlayFiles} from './src/overlay.js'
472

483
// example: node index.js test/openapi/petstore.yaml test/overlays/overlay.yaml
494
const openapiFile = process.argv[2]

src/overlay.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fs from 'fs';
2+
import jsonpath from 'jsonpath';
3+
import {safeStringify,parse,parseWithPointers} from "@stoplight/yaml";
4+
import mergician from 'mergician';
5+
6+
function applyOverlayToOpenAPI(spec, overlay) {
7+
// Use jsonpath.apply to do the changes
8+
overlay.actions.forEach((a)=>{
9+
jsonpath.apply(spec, a.target, (chunk) => {
10+
// Is it a remove?
11+
if (a.hasOwnProperty('remove')) {
12+
return {};
13+
}
14+
15+
// It must be an update
16+
17+
// Deep merge using a module (built-in spread operator is only shallow)
18+
const merger = mergician({appendArrays: true});
19+
console.debug(a);
20+
const merged = merger(chunk, a.update);
21+
return merged;
22+
23+
});
24+
})
25+
26+
return spec;
27+
}
28+
29+
export function overlayFiles(openapiFile, overlayFile) {
30+
// Parse the "input" OpenAPI document
31+
console.debug(openapiFile);
32+
console.debug (typeof openapiFile);
33+
const specraw = fs.readFileSync(openapiFile, 'utf8');
34+
var spec = parseWithPointers(specraw).data;
35+
36+
// Parse the "overlay" document
37+
const overlayraw = fs.readFileSync(overlayFile, 'utf8');
38+
const overlay = parseWithPointers(overlayraw).data;
39+
40+
spec = applyOverlayToOpenAPI(spec, overlay);
41+
42+
// Return the new spec
43+
return safeStringify(spec);
44+
45+
}
46+

0 commit comments

Comments
 (0)