Created
February 14, 2020 18:57
-
-
Save RodriguezLucha/1dda9637d32fc085533774cf63dd3c29 to your computer and use it in GitHub Desktop.
Sort Swagger YML
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
yaml = require("js-yaml"); | |
fs = require("fs"); | |
try { | |
var doc = yaml.safeLoad(fs.readFileSync("./input.yml", "utf8")); | |
let res = yaml.safeDump(doc, { | |
styles: { | |
"!!null": "canonical" // dump null as ~ | |
}, | |
lineWidth: 120, | |
sortKeys: function(a, b) { | |
const sortOrder = [ | |
"swagger", | |
"info", | |
"host", | |
"basePath", | |
"schemes", | |
"paths", | |
"name", | |
"type", | |
"summary", | |
"description" | |
]; | |
let index_a = sortOrder.indexOf(a); | |
let index_b = sortOrder.indexOf(b); | |
if (index_a > -1 && index_b > -1) { | |
return index_a > index_b ? 1 : index_a < index_b ? -1 : 0; | |
} | |
if (index_a != -1 && index_b == -1) { | |
return -1; | |
} | |
if (index_a == -1 && index_b != -1) { | |
return 1; | |
} | |
return a > b ? 1 : a < b ? -1 : 0; | |
} | |
}); | |
console.log(res); | |
} catch (e) { | |
console.log(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment