|
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' |
47 | 2 |
|
48 | 3 | // example: node index.js test/openapi/petstore.yaml test/overlays/overlay.yaml
|
49 | 4 | const openapiFile = process.argv[2]
|
|
0 commit comments