|
1 |
| -import fs from 'fs'; |
2 |
| -import jsonpath from 'jsonpath'; |
3 |
| -import {safeStringify,parse,parseWithPointers} from "@stoplight/yaml"; |
4 |
| -import mergician from 'mergician'; |
| 1 | +import fs from 'fs' |
| 2 | +import jsonpath from 'jsonpath' |
| 3 | +import { safeStringify, parseWithPointers } from '@stoplight/yaml' |
| 4 | +import mergician from 'mergician' |
5 | 5 |
|
6 |
| -function applyOverlayToOpenAPI(spec, overlay) { |
7 |
| - // Use jsonpath.apply to do the changes |
8 |
| - if (overlay.actions && overlay.actions.length >= 1) overlay.actions.forEach((a)=>{ |
9 |
| - // Is it a remove? |
10 |
| - if (a.hasOwnProperty('remove')) { |
11 |
| - while(true) { |
12 |
| - var path = jsonpath.paths(spec, a.target) |
13 |
| - if (path.length == 0) { |
14 |
| - break |
15 |
| - } |
16 |
| - var parent = jsonpath.parent(spec, a.target) |
17 |
| - const thingToRemove = path[0][path[0].length - 1] |
18 |
| - if (Array.isArray(parent)) { |
19 |
| - parent.splice(thingToRemove, 1); |
20 |
| - } else { |
21 |
| - delete parent[thingToRemove]; |
22 |
| - } |
23 |
| - } |
| 6 | +function applyOverlayToOpenAPI (spec, overlay) { |
| 7 | + // Use jsonpath.apply to do the changes |
| 8 | + if (overlay.actions && overlay.actions.length >= 1) { |
| 9 | + overlay.actions.forEach((a) => { |
| 10 | + // Is it a remove? |
| 11 | + if (Object.prototype.hasOwnProperty.call(a, 'remove')) { |
| 12 | + while (true) { |
| 13 | + const path = jsonpath.paths(spec, a.target) |
| 14 | + if (path.length === 0) { |
| 15 | + break |
| 16 | + } |
| 17 | + const parent = jsonpath.parent(spec, a.target) |
| 18 | + const thingToRemove = path[0][path[0].length - 1] |
| 19 | + if (Array.isArray(parent)) { |
| 20 | + parent.splice(thingToRemove, 1) |
| 21 | + } else { |
| 22 | + delete parent[thingToRemove] |
| 23 | + } |
| 24 | + } |
| 25 | + } else { |
| 26 | + try { |
| 27 | + // It must be an update |
| 28 | + jsonpath.apply(spec, a.target, (chunk) => { |
| 29 | + // Deep merge using a module (built-in spread operator is only shallow) |
| 30 | + const merger = mergician({ appendArrays: true }) |
| 31 | + return merger(chunk, a.update) |
| 32 | + }) |
| 33 | + } catch (ex) { |
| 34 | + process.stderr.write(`Error applying overlay: ${ex.message}\n`) |
| 35 | + // return chunk |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
24 | 40 |
|
25 |
| - } else { |
26 |
| - try { |
27 |
| - // It must be an update |
28 |
| - jsonpath.apply(spec, a.target, (chunk) => { |
29 |
| - // Deep merge using a module (built-in spread operator is only shallow) |
30 |
| - const merger = mergician({appendArrays: true}) |
31 |
| - return merger(chunk, a.update) |
32 |
| - }); |
33 |
| - } |
34 |
| - catch (ex) { |
35 |
| - process.stderr.write(`Error applying overlay: ${ex.message}\n`) |
36 |
| - //return chunk |
37 |
| - } |
38 |
| - |
39 |
| - } |
40 |
| - }) |
41 |
| - |
42 |
| - return spec; |
| 41 | + return spec |
43 | 42 | }
|
44 | 43 |
|
45 |
| -function sortOpenAPIFields(field1, field2) { |
46 |
| - const orderedKeys = ["info", "servers", "summary", "operationId", "tags", "paths", "components", "description", "parameters", "responses"]; |
| 44 | +function sortOpenAPIFields (field1, field2) { |
| 45 | + const orderedKeys = ['info', 'servers', 'summary', 'operationId', 'tags', 'paths', 'components', 'description', 'parameters', 'responses'] |
47 | 46 |
|
48 |
| - const index1 = orderedKeys.indexOf(field1); |
49 |
| - const index2 = orderedKeys.indexOf(field2); |
| 47 | + const index1 = orderedKeys.indexOf(field1) |
| 48 | + const index2 = orderedKeys.indexOf(field2) |
50 | 49 |
|
51 |
| - if (index1 === -1 || index2 === -1) { |
52 |
| - return 0; |
53 |
| - } else if (index1 > index2) { |
54 |
| - return 1; |
55 |
| - } else { |
56 |
| - return -1; |
57 |
| - } |
| 50 | + if (index1 === -1 || index2 === -1) { |
| 51 | + return 0 |
| 52 | + } else if (index1 > index2) { |
| 53 | + return 1 |
| 54 | + } else { |
| 55 | + return -1 |
| 56 | + } |
58 | 57 | }
|
59 | 58 |
|
60 |
| -export function applyOverlay(definition, overlay) { |
61 |
| - return applyOverlayToOpenAPI(definition, overlay); |
| 59 | +export function applyOverlay (definition, overlay) { |
| 60 | + return applyOverlayToOpenAPI(definition, overlay) |
62 | 61 | }
|
63 | 62 |
|
64 |
| -export function overlayFiles(openapiFile, overlayFile) { |
65 |
| - // Parse the "input" OpenAPI document |
66 |
| - const specraw = fs.readFileSync(openapiFile, 'utf8'); |
67 |
| - var spec = parseWithPointers(specraw).data; |
| 63 | +export function overlayFiles (openapiFile, overlayFile) { |
| 64 | + // Parse the "input" OpenAPI document |
| 65 | + const specraw = fs.readFileSync(openapiFile, 'utf8') |
| 66 | + let spec = parseWithPointers(specraw).data |
68 | 67 |
|
69 |
| - // Parse the "overlay" document |
70 |
| - const overlayraw = fs.readFileSync(overlayFile, 'utf8'); |
71 |
| - const overlay = parseWithPointers(overlayraw).data; |
| 68 | + // Parse the "overlay" document |
| 69 | + const overlayraw = fs.readFileSync(overlayFile, 'utf8') |
| 70 | + const overlay = parseWithPointers(overlayraw).data |
72 | 71 |
|
73 |
| - spec = applyOverlayToOpenAPI(spec, overlay); |
| 72 | + spec = applyOverlayToOpenAPI(spec, overlay) |
74 | 73 |
|
75 |
| - // Return the new spec |
76 |
| - return safeStringify(spec, {"sortKeys": sortOpenAPIFields}); |
| 74 | + // Return the new spec |
| 75 | + return safeStringify(spec, { sortKeys: sortOpenAPIFields }) |
77 | 76 | }
|
78 |
| - |
79 |
| - |
0 commit comments