-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: use 'standard' eslint rules to have a consistent code style
This commit adds eslint library with the standard ruleset and applies all rules to the existing codebase. This will help have a unique code style accross the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true, | ||
"mocha": true, | ||
"jest": true | ||
}, | ||
"extends": "standard", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ jobs: | |
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm run lint | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
#!/usr/bin/env node | ||
import arg from 'arg'; | ||
import {overlayFiles} from './src/overlay.js' | ||
import arg from 'arg' | ||
import { overlayFiles } from './src/overlay.js' | ||
|
||
function showHelp() { | ||
console.log("Usage: overlayjs --openapi FILEPATH --overlay FILEPATH"); | ||
console.log(" use --help to see this help"); | ||
function showHelp () { | ||
console.log('Usage: overlayjs --openapi FILEPATH --overlay FILEPATH') | ||
console.log(' use --help to see this help') | ||
} | ||
|
||
try { | ||
const args = arg({ | ||
'--openapi': String, | ||
'--overlay': String, | ||
'--help': String | ||
}); | ||
|
||
if(args['--overlay'] && args['--openapi']) { | ||
const openapiFile = args['--openapi']; | ||
const overlayFile = args['--overlay']; | ||
var spec = overlayFiles(openapiFile, overlayFile); | ||
console.log(spec); | ||
} else { | ||
showHelp() | ||
} | ||
const args = arg({ | ||
'--openapi': String, | ||
'--overlay': String, | ||
'--help': String | ||
}) | ||
|
||
if (args['--overlay'] && args['--openapi']) { | ||
const openapiFile = args['--openapi'] | ||
const overlayFile = args['--overlay'] | ||
const spec = overlayFiles(openapiFile, overlayFile) | ||
console.log(spec) | ||
} else { | ||
showHelp() | ||
} | ||
} catch (err) { | ||
if (err.code === 'ARG_UNKNOWN_OPTION') { | ||
console.warn(err.message); | ||
showHelp() | ||
} else { | ||
throw err; | ||
} | ||
if (err.code === 'ARG_UNKNOWN_OPTION') { | ||
console.warn(err.message) | ||
showHelp() | ||
} else { | ||
throw err | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,76 @@ | ||
import fs from 'fs'; | ||
import jsonpath from 'jsonpath'; | ||
import {safeStringify,parse,parseWithPointers} from "@stoplight/yaml"; | ||
import mergician from 'mergician'; | ||
import fs from 'fs' | ||
import jsonpath from 'jsonpath' | ||
import { safeStringify, parseWithPointers } from '@stoplight/yaml' | ||
import mergician from 'mergician' | ||
|
||
function applyOverlayToOpenAPI(spec, overlay) { | ||
// Use jsonpath.apply to do the changes | ||
if (overlay.actions && overlay.actions.length >= 1) overlay.actions.forEach((a)=>{ | ||
// Is it a remove? | ||
if (a.hasOwnProperty('remove')) { | ||
while(true) { | ||
var path = jsonpath.paths(spec, a.target) | ||
if (path.length == 0) { | ||
break | ||
} | ||
var parent = jsonpath.parent(spec, a.target) | ||
const thingToRemove = path[0][path[0].length - 1] | ||
if (Array.isArray(parent)) { | ||
parent.splice(thingToRemove, 1); | ||
} else { | ||
delete parent[thingToRemove]; | ||
} | ||
} | ||
function applyOverlayToOpenAPI (spec, overlay) { | ||
// Use jsonpath.apply to do the changes | ||
if (overlay.actions && overlay.actions.length >= 1) { | ||
overlay.actions.forEach((a) => { | ||
// Is it a remove? | ||
if (Object.prototype.hasOwnProperty.call(a, 'remove')) { | ||
while (true) { | ||
const path = jsonpath.paths(spec, a.target) | ||
if (path.length === 0) { | ||
break | ||
} | ||
const parent = jsonpath.parent(spec, a.target) | ||
const thingToRemove = path[0][path[0].length - 1] | ||
if (Array.isArray(parent)) { | ||
parent.splice(thingToRemove, 1) | ||
} else { | ||
delete parent[thingToRemove] | ||
} | ||
} | ||
} else { | ||
try { | ||
// It must be an update | ||
jsonpath.apply(spec, a.target, (chunk) => { | ||
// Deep merge using a module (built-in spread operator is only shallow) | ||
const merger = mergician({ appendArrays: true }) | ||
return merger(chunk, a.update) | ||
}) | ||
} catch (ex) { | ||
process.stderr.write(`Error applying overlay: ${ex.message}\n`) | ||
// return chunk | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} else { | ||
try { | ||
// It must be an update | ||
jsonpath.apply(spec, a.target, (chunk) => { | ||
// Deep merge using a module (built-in spread operator is only shallow) | ||
const merger = mergician({appendArrays: true}) | ||
return merger(chunk, a.update) | ||
}); | ||
} | ||
catch (ex) { | ||
process.stderr.write(`Error applying overlay: ${ex.message}\n`) | ||
//return chunk | ||
} | ||
|
||
} | ||
}) | ||
|
||
return spec; | ||
return spec | ||
} | ||
|
||
function sortOpenAPIFields(field1, field2) { | ||
const orderedKeys = ["info", "servers", "summary", "operationId", "tags", "paths", "components", "description", "parameters", "responses"]; | ||
function sortOpenAPIFields (field1, field2) { | ||
const orderedKeys = ['info', 'servers', 'summary', 'operationId', 'tags', 'paths', 'components', 'description', 'parameters', 'responses'] | ||
|
||
const index1 = orderedKeys.indexOf(field1); | ||
const index2 = orderedKeys.indexOf(field2); | ||
const index1 = orderedKeys.indexOf(field1) | ||
const index2 = orderedKeys.indexOf(field2) | ||
|
||
if (index1 === -1 || index2 === -1) { | ||
return 0; | ||
} else if (index1 > index2) { | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
if (index1 === -1 || index2 === -1) { | ||
return 0 | ||
} else if (index1 > index2) { | ||
return 1 | ||
} else { | ||
return -1 | ||
} | ||
} | ||
|
||
export function applyOverlay(definition, overlay) { | ||
return applyOverlayToOpenAPI(definition, overlay); | ||
export function applyOverlay (definition, overlay) { | ||
return applyOverlayToOpenAPI(definition, overlay) | ||
} | ||
|
||
export function overlayFiles(openapiFile, overlayFile) { | ||
// Parse the "input" OpenAPI document | ||
const specraw = fs.readFileSync(openapiFile, 'utf8'); | ||
var spec = parseWithPointers(specraw).data; | ||
export function overlayFiles (openapiFile, overlayFile) { | ||
// Parse the "input" OpenAPI document | ||
const specraw = fs.readFileSync(openapiFile, 'utf8') | ||
let spec = parseWithPointers(specraw).data | ||
|
||
// Parse the "overlay" document | ||
const overlayraw = fs.readFileSync(overlayFile, 'utf8'); | ||
const overlay = parseWithPointers(overlayraw).data; | ||
// Parse the "overlay" document | ||
const overlayraw = fs.readFileSync(overlayFile, 'utf8') | ||
const overlay = parseWithPointers(overlayraw).data | ||
|
||
spec = applyOverlayToOpenAPI(spec, overlay); | ||
spec = applyOverlayToOpenAPI(spec, overlay) | ||
|
||
// Return the new spec | ||
return safeStringify(spec, {"sortKeys": sortOpenAPIFields}); | ||
// Return the new spec | ||
return safeStringify(spec, { sortKeys: sortOpenAPIFields }) | ||
} | ||
|
||
|
Oops, something went wrong.